我正在尝试添加一个新的服务器变量
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 block ,但我想检查变量是否已经存在,如果它已经存在则跳过添加。
谁能告诉我如何进行此检查?
最佳答案
例如尝试添加以下检查:
$path = "/system.webServer/rewrite/allowedServerVariables"
$value = "HTTP_COOKIE"
if ((Get-WebConfiguration $path).Collection.Name -notcontains $value) {
Add-WebConfiguration $path -AtIndex 0 -Value @{ name = $value }
}
关于c# - Powershell:在 applicationHost.config 中查找 allowedServerVariables 的脚本检查是否重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54463777/