【问题标题】:Powershell: script to find allowedServerVariables in applicationHost.config check for duplicatePowershell:在 applicationHost.config 中查找 allowedServerVariables 的脚本检查重复项
【发布时间】:2019-06-25 02:27:51
【问题描述】:

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

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 块,但想检查变量是否已经存在,如果它已经存在则跳过添加。

谁能告诉我如何进行这项检查?

【问题讨论】:

标签: c# asp.net .net windows powershell


【解决方案1】:

例如尝试添加以下检查:

$path = "/system.webServer/rewrite/allowedServerVariables"
$value = "HTTP_COOKIE"
if ((Get-WebConfiguration $path).Collection.Name -notcontains $value) {
    Add-WebConfiguration $path -AtIndex 0 -Value @{ name = $value }
}

【讨论】:

  • 如果我有多个服务器变量,这会起作用吗?
  • @RajGan 是的(如果你没有超旧的 PS 版本)。但你可以简单地尝试一下。
  • 我们可以用 Get-WebConfigurationProperty 做类似的事情吗?
  • 找到这个 gist.github.com/ducas/b16dfd1d9ba91da7fd53 以通过 Get-WebConfigurationProperty 进行检查
【解决方案2】:

@marsze 方式已经使用 Get-WebConfiguration 完成。

我的答案是使用 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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 2021-12-11
    • 1970-01-01
    • 2012-09-27
    相关资源
    最近更新 更多