0

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')
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.com/dsccommunity/WebAdministrationDsc/issues/543
    – jdweng
    Commented Mar 18 at 12:37

2 Answers 2

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'

7
  • @Dennis, the WebAdministration format is the IIS:\... format, from what I can tell. Use the other format, then (as mentioned in the first bullet point, e.g. '/' for the default website, or something like '/blog')
    – mklement0
    Commented Mar 18 at 16:35
  • 1
    @Dennis, using the format described in the first bullet point does not require the provider for the IIS: drive. It also doesn't strictly need any module, as long as you know the path of interest. I was using Get-IISSite as just an example of how to obtain the path; presumably you can also use the IIS Manager GUI - I've updated the answer accordingly.
    – mklement0
    Commented Mar 18 at 16:43
  • WebSitePath = 'MACHINE/WEBROOT' is accepted, but I have no idea what that points to. But then the Filter will fail instead, which is also a pure guess on my part :)
    – Dennis
    Commented Mar 18 at 16:53
  • @Dennis, my understanding is that '/' represents the default site. If you need a different site, I suggest using the IIS Manager GUI to locate the site of interest, open its properties dialog and look a the Path: field.
    – mklement0
    Commented Mar 18 at 16:56
  • 1
    I reverted my question to the original wording not to mess up any following answers...
    – Dennis
    Commented Mar 26 at 18:55
0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.