admin管理员组文章数量:1384595
When automating a setup of an IIS site, I run into an issue that required me to use URL Rewrite.
However, I don't seem to get the grip of how to use the DSC Resource command WebConfigProperty
correctly.
I've tried using
Configuration ConfigureRewriteRules {
Import-DscResource -ModuleName WebAdministrationDsc -ModuleVersion 4.1.0
Import-DscResource -ModuleName PSDesiredStateConfiguration
node localhost {
File 'Create_RewriteRulesFile' {
Ensure = 'Present'
Type = 'File'
DestinationPath = 'c:\inetpub\wwwroot\rewriterules.config'
Contents = '<!-- some rules -->'
Force = $true
MatchSource = $true
}
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'c:\inetpub\wwwroot'
Filter = 'system.WebServer/rewrite/rules'
Value = 'configSource = "rewriterules.config"'
DependsOn = '[File]Create_RewriteRulesFile'
}
}
}
ConfigureRewriteRules
Start-DscConfiguration -Path .\ConfigureRewriteRules -Wait -Verbose -Force
[WebConfigProperty]Create_RewriteSection
will generate the error
Path doesn't belong to 'WebAdministration' provider.
So obviously I've got the parameter WebSitePath
wrong.
According to dsccommunity/WebAdministrationDsc, the resource command should support both IIS and WebAdministration format paths.
Note: I've specified Default Website
for ease of use in this example, but I've verified that the specified path do contain the web.config
file.
Edit: To allow you as a reader to better track my findings, I've rolled back the question to the original wordings and will add any findings below instead.
Finding 1
The DSC resource requires PropertyName
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'c:\inetpub\wwwroot'
Filter = 'system.WebServer/rewrite/rules'
PropertyName = 'rules'
Value = 'configSource = "rewriterules.config"'
DependsOn = '[File]Create_RewriteRulesFile'
}
Finding 2
The WebSitePath isn't the physical path of web.config
.
The possible paths was found when using the IIS
path and got the WebAdministration
path back in an error message about the Filter.
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'MACHINE/WEBROOT/APPHOST/Default Web Site'
#or WebSitePath = 'IIS:\inetpub\wwwroot'\Sites\Default WebSite\'
Filter = 'system.WebServer/rewrite/rules'
PropertyName = 'rules'
Value = 'configSource = "rewriterules.config"'
DependsOn = '[File]Create_RewriteRulesFile'
}
This will change the error returned to
Target configuration object 'system.WebServer/rewrite/rules' is not found
at path 'MACHINE/WEBROOT/APPHOST/Default Web Site'.
Finding 3
PropertyName should not be part of the Filter
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'MACHINE/WEBROOT/APPHOST/Default Web Site'
#or WebSitePath = 'IIS:\Sites\Default Web Site\'
Filter = 'system.WebServer/rewrite/rules'
PropertyName = 'configSource'
Value = 'rewriteRules.config'
DependsOn = '[File]Create_RewriteRulesFile'
}
This will still generate the error
Target configuration object 'system.WebServer/rewrite/rules' is not found
at path 'MACHINE/WEBROOT/APPHOST/Default Web Site'.
Finding 4
Creating the section in web.config
using XML-manipulation instead will still result in errors using WebAdministrationDsc
to set properties.
$webxml = [xml](Get-Content c:\intetpub\wwwroot\web.config)
$rewrite = $webxml.CreateElement('rewrite')
$rules = $rewrite.AppendChild($webxml.CreateElement('rules'))
$webxml.configuration.'system.webServer'.AppendChild($rewrite)
$webxml.Save('c:\intetpub\wwwroot\web.config')
When automating a setup of an IIS site, I run into an issue that required me to use URL Rewrite.
However, I don't seem to get the grip of how to use the DSC Resource command WebConfigProperty
correctly.
I've tried using
Configuration ConfigureRewriteRules {
Import-DscResource -ModuleName WebAdministrationDsc -ModuleVersion 4.1.0
Import-DscResource -ModuleName PSDesiredStateConfiguration
node localhost {
File 'Create_RewriteRulesFile' {
Ensure = 'Present'
Type = 'File'
DestinationPath = 'c:\inetpub\wwwroot\rewriterules.config'
Contents = '<!-- some rules -->'
Force = $true
MatchSource = $true
}
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'c:\inetpub\wwwroot'
Filter = 'system.WebServer/rewrite/rules'
Value = 'configSource = "rewriterules.config"'
DependsOn = '[File]Create_RewriteRulesFile'
}
}
}
ConfigureRewriteRules
Start-DscConfiguration -Path .\ConfigureRewriteRules -Wait -Verbose -Force
[WebConfigProperty]Create_RewriteSection
will generate the error
Path doesn't belong to 'WebAdministration' provider.
So obviously I've got the parameter WebSitePath
wrong.
According to dsccommunity/WebAdministrationDsc, the resource command should support both IIS and WebAdministration format paths.
Note: I've specified Default Website
for ease of use in this example, but I've verified that the specified path do contain the web.config
file.
Edit: To allow you as a reader to better track my findings, I've rolled back the question to the original wordings and will add any findings below instead.
Finding 1
The DSC resource requires PropertyName
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'c:\inetpub\wwwroot'
Filter = 'system.WebServer/rewrite/rules'
PropertyName = 'rules'
Value = 'configSource = "rewriterules.config"'
DependsOn = '[File]Create_RewriteRulesFile'
}
Finding 2
The WebSitePath isn't the physical path of web.config
.
The possible paths was found when using the IIS
path and got the WebAdministration
path back in an error message about the Filter.
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'MACHINE/WEBROOT/APPHOST/Default Web Site'
#or WebSitePath = 'IIS:\inetpub\wwwroot'\Sites\Default WebSite\'
Filter = 'system.WebServer/rewrite/rules'
PropertyName = 'rules'
Value = 'configSource = "rewriterules.config"'
DependsOn = '[File]Create_RewriteRulesFile'
}
This will change the error returned to
Target configuration object 'system.WebServer/rewrite/rules' is not found
at path 'MACHINE/WEBROOT/APPHOST/Default Web Site'.
Finding 3
PropertyName should not be part of the Filter
WebConfigProperty 'Create_RewriteSection' {
Ensure = 'Present'
WebSitePath = 'MACHINE/WEBROOT/APPHOST/Default Web Site'
#or WebSitePath = 'IIS:\Sites\Default Web Site\'
Filter = 'system.WebServer/rewrite/rules'
PropertyName = 'configSource'
Value = 'rewriteRules.config'
DependsOn = '[File]Create_RewriteRulesFile'
}
This will still generate the error
Target configuration object 'system.WebServer/rewrite/rules' is not found
at path 'MACHINE/WEBROOT/APPHOST/Default Web Site'.
Finding 4
Creating the section in web.config
using XML-manipulation instead will still result in errors using WebAdministrationDsc
to set properties.
$webxml = [xml](Get-Content c:\intetpub\wwwroot\web.config)
$rewrite = $webxml.CreateElement('rewrite')
$rules = $rewrite.AppendChild($webxml.CreateElement('rules'))
$webxml.configuration.'system.webServer'.AppendChild($rewrite)
$webxml.Save('c:\intetpub\wwwroot\web.config')
Share
Improve this question
edited Mar 26 at 21:42
Dennis
asked Mar 17 at 20:14
DennisDennis
1,9052 gold badges17 silver badges40 bronze badges
3
|
2 Answers
Reset to default 1Update:
The premise of the question was changed after this answer was posted.
The original, immediate problem was a mistaken attempt to use a file-system path (
'c:\inetpub\wwwroot'
) as theWebSitePath
parameter value, and this is what the answer below addresses.
The documentation you link to (of the WebConfigProperty
DSC resource) states (emphasis added):
Path to website location (IIS or WebAdministration format).
This suggests that it isn't a file-system path that the WebSitePath
parameter expects, but one expressed in IIS terms, in (what I infer - untested by me) one of two supported formats:
Preferably, an IIS-internal path, such as reported by the
.Path
property of an IIS site-information object returned by theGet-IISSite
cmdlet from theIISAdministration
module, e.g.'/'
for the default site, or something like'/blog'
- I presume that you can also glean the path of interest from the IIS Manager GUI (
Start-Process InetMgr
) ahead of time, so you don't strictly need to install a module.
- I presume that you can also glean the path of interest from the IIS Manager GUI (
Alternatively, for backward compatibility, an
'IIS:\...'
path, based on theIIS:
PowerShell drive published by the - obsolete -WebAdministration
module, e.g.'IIS:\Sites\blog'
I managed to find a solution by side stepping WebAdministrationDsc
and using the DSC resource xXMLConfigFile
instead.
The drawback is that the web application will be restarted if web.config
is altered from outside of the IIS. So this solution is best suited using service windows.
Configuration Set_RewriteRules {
Import-DscResource -ModuleName xXMLConfigFile
Import-DscResource -ModuleName PSDesiredStateConfiguration
node localhost {
File 'Create_RewriteRulesFile' {
Ensure = 'Present'
Type = 'File'
DestinationPath = 'c:\inetpub\wwwroot\rewriteRules.config'
Contents = '<!-- some rules -->'
Force = $true
MatchSource = $true
}
XMLConfigFile 'Create_RewriteSection' {
Ensure = 'Present'
ConfigPath = 'c:\inetpub\wwwroot\web.config'
XPath = 'system.webServer'
Name = 'rewrite'
isElementTextValue = $true
}
XMLConfigFile 'Set_configSource' {
Ensure = 'Present'
ConfigPath = 'c:\inetpub\wwwroot\web.config'
XPath = 'system.webServer/rewrite/rules'
Name = 'configSource'
Value = 'rewriteRules.config'
isAttribute = $false
DependsOn = '[File]Create_RewriteRulesFile',
'[XMLConfigFile]Create_RewriteSection'
}
}
}
Set_RewriteRules
Start-DscConfiguration -Path .\Set_RewriteRules -Wait -Verbose -Force
Note: XPath is case sensitive.
版权声明:本文标题:powershell - How do I configure URL Rewrite for IIS using Desired State Configuration (DSC)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537020a2611377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rewriterules.config
though, and it's UTF-8. – Dennis Commented Mar 18 at 7:43