为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕田峪4524236 2022-08-20 16:00:33
我正在尝试添加新的服务器变量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。任何人都可以让我知道我如何进行此检查吗?
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

例如,尝试添加以下检查:


$path = "/system.webServer/rewrite/allowedServerVariables"

$value = "HTTP_COOKIE"

if ((Get-WebConfiguration $path).Collection.Name -notcontains $value) {

    Add-WebConfiguration $path -AtIndex 0 -Value @{ name = $value }

}


查看完整回答
反对 回复 2022-08-20
?
www说

TA贡献1775条经验 获得超8个赞

我的答案是使用Get-WebConfigurationProperty。两者都可以工作。


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"


查看完整回答
反对 回复 2022-08-20
  • 2 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信