WebAdministrationDsc
WebAdministrationDsc copied to clipboard
xWebConfigPropertyCollection: support single property collection
I am trying to add a Server Variable to rewrite. In the C:\Windows\System32\inetsrv\config\applicationHost.config, the desired outcome looks like this:
<rewrite>
<allowedServerVariables>
<add name="RESPONSE_Server" />
</allowedServerVariables>
</rewrite>
I have a script that creates the variable:
Add-WebConfigurationProperty -pspath $IISPath -filter "system.webServer/rewrite/allowedServerVariables" -name "." -value @{name="RESPONSE_Server"}
So far, I can't figure out the way to do the same with xWebConfigProperty. This won't work:
xWebConfigProperty RESPONSE_Server
{
Ensure = "Present"
Filter = "system.webServer/rewrite/allowedServerVariables"
WebsitePath = "MACHINE/WEBROOT/APPHOST"
PropertyName = "."
Value = "RESPONSE_Server"
}
I would need something of that sort:
Value = @{name="RESPONSE_Server"}
But there is no way to pass a hashtable into the Value parameter
Hello @SergeDubovsky. Anytime you need to set a web configuration property using a hash table, that indicates it is a collection, which would normally require xWebConfigPropertyCollection to manage it. In this case, the collection only has one property, so xWebConfigPropertyCollection will not work. However you can still manage a one property collection like this with xWebConfigProperty:
Configuration Rewrite
{
Import-DscResource -ModuleName xWebAdministration
node localhost
{
xWebConfigProperty allowedServerVariables
{
WebsitePath = 'MACHINE/WEBROOT/APPHOST'
Filter = "/system.webServer/rewrite/allowedServerVariables/add"
PropertyName = 'Name'
Value = 'RESPONSE_Server'
Ensure = 'Present'
}
}
}
The main difference is collections require the 'add' element.
Maybe we should add this as an example? Labeled it as documentation, and help wanted so that anyone in the community can run with this.
There are some more issues. When I have several variables in the collection, the contents of
$targetResource.Value in this line MSFT_xWebConfigProperty\MSFT_xWebConfigProperty.psm1: 224
if ( ($null -eq $targetResource.Value) -or ($targetResource.Value.ToString() -ne $Value) )
if following:
ItemXPath : /system.webServer/rewrite/allowedServerVariables/add[@name='RESPONSE_X-POWERED-BY']
IsInheritedFromDefaultValue : False
IsProtected : False
Name : name
TypeName : System.String
Schema : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value : RESPONSE_X-POWERED-BY
IsExtended : False
ItemXPath : /system.webServer/rewrite/allowedServerVariables/add[@name='RESPONSE_X-ASPNET-VERSION']
IsInheritedFromDefaultValue : False
IsProtected : False
Name : name
TypeName : System.String
Schema : Microsoft.IIs.PowerShell.Framework.ConfigurationAttributeSchema
Value : RESPONSE_X-ASPNET-VERSION
IsExtended : False
It would be better to use something of this sort to check if there is an entry in the collection:
($targetResource.Value | select -ExpandProperty Value) -contains $value
@SergeDubovsky Could you open a new issue for this? It seems it not the same issue as before?
The issue is "evolved" :) I am trying to use the suggested example for the task, I was trying to implement. I don't think, the xWebConfigProperty resource will work for it, as @regedit32 suggested. If there are items in the collection already, Set-TargetResource would fail with this error:
Set-WebConfigurationProperty : Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)
At C:\Program Files\WindowsPowerShell\Modules\xWebAdministration\2.0.0.0\DscResources\MSFT_xWebConfigProperty\MSFT_xWebConfigProperty.psm1:144 char:9
If there are no items in the collection, it would fail with this:
Target configuration object 'system.webServer/rewrite/allowedServerVariables/add is not found at path 'MACHINE/WEBROOT/APPHOST'
In both cases, Set-WebConfigurationProperty is not what we need. It should be Add-WebConfigurationProperty
I would open a new issue. Thank you.
@regedit32 Could you possible assist here please? 🙂 Is it functionality missing so this should be an enhancement?
@johlju Yes, after testing some more I can see it is not going to work as-is with xWebConfigProperty. Since this is a collection with a single property that we're trying to solve here, it seems like the solution should go in the xWebConfigPropertyCollection resource to be able to handle this type of collection. Otherwise we'd be adding logic to xWebConfigProperty to conditionally handle single property collections, which doesn't seem to be the intended scope of that resource.
xWebConfigPropertyCollection resource schema.mof defines ItemPropertyName as a key property in order to support collections with 2 or more properties, for example this collection: /system.webServer/security/requestFiltering/FilteringRules.
Without ItemPropertyName defined as a key, there would be resource conflicts for that collection, but at the same time it blocks the ability to manage a single property collection like: /system.webServer/rewrite/allowedServerVariables
Unless I'm overlooking something, in either case, I'm not sure if it that qualifies as missing functionality or a desired enhancement.
So basically we change this issue so that the enhancement is to support single property collection in the resource xWebConfigPropertyCollection?
I haven't put much thought into a potential solution yet, but yes, that makes the most sense to me.
Renamed this issue. Labeling this as an enhancement, and help wanted.
I just got this working using the following syntax:
invoke-dscresource -name xWebConfigPropertyCollection -Method Set -ModuleName xWebAdministration -Property @{
Ensure = "Present"
WebsitePath = "Iis:\"
Filter = "/system.webServer/rewrite"
CollectionName = "allowedServerVariables"
ItemName = "add"
ItemKeyName = "*"
ItemKeyValue = "RESPONSE_Server"
ItemPropertyName = "name"
ItemPropertyValue = "RESPONSE_Server"
}
I just got this working using the following syntax:
invoke-dscresource -name xWebConfigPropertyCollection -Method Set -ModuleName xWebAdministration -Property @{ Ensure = "Present" WebsitePath = "Iis:\" Filter = "/system.webServer/rewrite" CollectionName = "allowedServerVariables" ItemName = "add" ItemKeyName = "*" ItemKeyValue = "RESPONSE_Server" ItemPropertyName = "name" ItemPropertyValue = "RESPONSE_Server" }
Worked for me with xWebAdministration 2.7.0.0.
I suggest that we add that to an example then.
I suggest that we add that to an example then.
That would be great, the current documentation for xWebAdministration is not always super straightforward. I was happy to find the above example by browsing the issues :)
@rnsc agree, this resource module needs more love (help) from the community in many areas.
I believe this issue also occurs when trying to add another default document.
# Add maintenance.html to the default documents
xWebConfigPropertyCollection 'default-doc' {
WebsitePath = 'MACHINE/WEBROOT/APPHOST'
Filter = 'system.webServer/defaultDocument/files'
CollectionName = 'Collection'
ItemName = 'add'
ItemKeyName = 'value'
ItemKeyValue = 'maintenance.html'
ItemPropertyName = 'Entry Path'
ItemPropertyValue = 'MACHINE/WEBROOT/APPHOST'
Ensure = 'Present'
}
So there's a workaround to set a single field collection, the resource was set up to be able to do two field collections, how about collections that have three or more fields, like system.applicationHost/sites/siteDefaults/logFile/customFields which has properties logFieldName, sourceName, SourceType?
Shouldn't this implementation have a hashtable parameter rather than key/property? Like so -
xWebConfigPropertyCollection 'SetXForwardedForLog' {
WebsitePath = 'MACHINE/WEBROOT/APPHOST'
Filter = 'system.applicationHost/sites/siteDefaults/logFile/customFields'
CollectionName = 'CustomFields'
ItemName = 'Add'
KeyValSet = @{logFieldName = 'ClientIP'; sourceName = 'X-Forwarded-For'; sourceType = 'RequestHeader'}
Ensure = 'Present'
}
I just got this working using the following syntax:
invoke-dscresource -name xWebConfigPropertyCollection -Method Set -ModuleName xWebAdministration -Property @{ Ensure = "Present" WebsitePath = "Iis:\" Filter = "/system.webServer/rewrite" CollectionName = "allowedServerVariables" ItemName = "add" ItemKeyName = "*" ItemKeyValue = "RESPONSE_Server" ItemPropertyName = "name" ItemPropertyValue = "RESPONSE_Server" }Worked for me with xWebAdministration 2.7.0.0.
Can't figure out a way to do this on site level instead of machine level. Does anyone have any idea?