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
  • The character u0027 is a unicode apostrophe. The file you are using may be set to be unicode and not UTF-8. Open the file with notepad and then use Menu : File SaveAs. In the save box there is an encoding dropdown in the bottom right side that will specify the type of encoding. – jdweng Commented Mar 17 at 21:36
  • The file is created by DSC and this procedure works on other instances, i.e. setting up iScheduled Tasks. I did check the rewriterules.config though, and it's UTF-8. – Dennis Commented Mar 18 at 7:43
  • 2 Did you see : github/dsccommunity/WebAdministrationDsc/issues/543 – jdweng Commented Mar 18 at 12:37
Add a comment  | 

2 Answers 2

Reset to default 1

Update:

  • 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 the WebSitePath 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 the Get-IISSite cmdlet from the IISAdministration 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.
  • Alternatively, for backward compatibility, an 'IIS:\...' path, based on the IIS: 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.

本文标签: powershellHow do I configure URL Rewrite for IIS using Desired State Configuration (DSC)Stack Overflow