Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xWebConfigPropertyCollection: support single property collection #379

Closed
SergeDubovsky opened this issue Jul 5, 2018 · 19 comments · Fixed by #644
Closed

xWebConfigPropertyCollection: support single property collection #379

SergeDubovsky opened this issue Jul 5, 2018 · 19 comments · Fixed by #644
Labels
enhancement The issue is an enhancement request.

Comments

@SergeDubovsky
Copy link

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

@regedit32
Copy link
Member

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.

@johlju johlju added help wanted The issue is up for grabs for anyone in the community. documentation The issue is related to documentation only. good first issue The issue should be easier to fix and can be taken up by a beginner to learn to contribute on GitHub labels Jul 6, 2018
@johlju
Copy link
Member

johlju commented Jul 6, 2018

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.

@SergeDubovsky
Copy link
Author

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

@johlju
Copy link
Member

johlju commented Jul 6, 2018

@SergeDubovsky Could you open a new issue for this? It seems it not the same issue as before?

@SergeDubovsky
Copy link
Author

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.

@johlju
Copy link
Member

johlju commented Jul 6, 2018

@regedit32 Could you possible assist here please? 🙂 Is it functionality missing so this should be an enhancement?

@johlju johlju added needs more information The issue needs more information from the author or the community. and removed documentation The issue is related to documentation only. good first issue The issue should be easier to fix and can be taken up by a beginner to learn to contribute on GitHub help wanted The issue is up for grabs for anyone in the community. labels Jul 6, 2018
@regedit32
Copy link
Member

@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.

@johlju
Copy link
Member

johlju commented Jul 10, 2018

So basically we change this issue so that the enhancement is to support single property collection in the resource xWebConfigPropertyCollection?

@regedit32
Copy link
Member

I haven't put much thought into a potential solution yet, but yes, that makes the most sense to me.

@johlju johlju changed the title xWebConfigProperty Value is string. No way to pass the hashtable in it. xWebConfigPropertyCollection: support single property collection Jul 18, 2018
@johlju
Copy link
Member

johlju commented Jul 18, 2018

Renamed this issue. Labeling this as an enhancement, and help wanted.

@johlju johlju added enhancement The issue is an enhancement request. help wanted The issue is up for grabs for anyone in the community. and removed needs more information The issue needs more information from the author or the community. labels Jul 18, 2018
@gpduck
Copy link

gpduck commented Apr 8, 2019

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"
}

@rnsc
Copy link

rnsc commented Aug 21, 2019

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.

@johlju
Copy link
Member

johlju commented Aug 21, 2019

I suggest that we add that to an example then.

@rnsc
Copy link

rnsc commented Aug 26, 2019

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 :)

@johlju
Copy link
Member

johlju commented Aug 26, 2019

@rnsc agree, this resource module needs more love (help) from the community in many areas.

@nlupica1
Copy link

nlupica1 commented Sep 19, 2019

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'
        }

@Igor-X
Copy link

Igor-X commented Oct 23, 2020

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'
}

@ngwwm
Copy link

ngwwm commented Mar 21, 2024

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?

@Borgquite
Copy link
Contributor

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?

@Igor-X Your issue is already perhaps raised in #534 - see examples there?

@johlju johlju removed the help wanted The issue is up for grabs for anyone in the community. label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement The issue is an enhancement request.
Projects
None yet
9 participants