Powershell:用于在应用程序中查找允许的服务器变量的脚本Host.config 检查重复项

我正在尝试添加新的服务器变量


Add-WebConfiguration /system.webServer/rewrite/allowedServerVariables -atIndex 0 -value @{name="HTTP_COOKIE"}

但我得到以下错误


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

我可以使用 try catch 块来抑制,但想检查变量是否已经存在,如果变量已经存在,则跳过 Add。


任何人都可以让我知道我如何进行此检查吗?


慕田峪4524236
浏览 179回答 2
2回答

烙印99

例如,尝试添加以下检查:$path = "/system.webServer/rewrite/allowedServerVariables"$value = "HTTP_COOKIE"if ((Get-WebConfiguration $path).Collection.Name -notcontains $value) {    Add-WebConfiguration $path -AtIndex 0 -Value @{ name = $value }}

www说

我的答案是使用Get-WebConfigurationProperty。两者都可以工作。Write-Host "Getting allowed server variables..."$allowedServerVariables = Get-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST" -filter "system.webServer/rewrite/allowedServerVariables/add" -Name nameWrite-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"
打开App,查看更多内容
随时随地看视频慕课网APP