I am trying add a new server variable
Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value @{name="HTTP_COOKIE"}
but I am getting following error
Add-WebConfigurationProperty : Filename:
Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'Test'
At line:1 char:1
+ Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Add-WebConfigurationProperty], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.IIs.PowerShell.Provider.AddConfigurationPropertyCommand
I could suppress using try catch block but would like to check if the variable already exists and skip Adding if it already exists.
Can anyone let me know how I can do this check?
Try adding the following check for example:
$path = "/system.webServer/rewrite/allowedServerVariables"
$value = "HTTP_COOKIE"
if ((Get-WebConfiguration $path).Collection.Name -notcontains $value) {
Add-WebConfiguration $path -AtIndex 0 -Value @{ name = $value }
}
@marsze way has done using Get-WebConfiguration.
my answer is using Get-WebConfigurationProperty. Both will work.
Write-Host "Getting allowed server variables..."
$allowedServerVariables = Get-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST" -filter "system.webServer/rewrite/allowedServerVariables/add" -Name name
Write-Host "Found $($allowedServerVariables.Length)..."
if ( ($allowedServerVariables -eq $null) -or ( $allowedServerVariables | ?{ $_.Value -eq "HTTP_COOKIE1" } ).Length -eq 0 ) {
#Configure IIS To Allow 'HTTPS' as a server variable - Must be done at a applicationhosts.config level
Write-Host "Adding HTTPS to allowed server variables..."
Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST" -filter "system.webServer/rewrite/allowedServerVariables" -name "." -value @{name='HTTP_COOKIE1'}
}
Write-Host "Getting allowed server variables...Finished"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With