c# - Powershell:在 applicationHost.config 中查找 allowedServerVariables 的脚本检查是否重复

标签 c# asp.net .net windows powershell

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

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/

相关文章:

c# - 如何在 Quartz.net 中随时开始工作?

c# - 使用 ASP.NET MVC 5,如何获取 View 中枚举值的本地化 DisplayAttribute 字符串?

c# - asp.net 缓存限制?

javascript - 如何使用 JavaScript、React、Angular、Vue 或 ASP.NET MVC 隐藏 Kendo UI 网格列

c# - Xamarin Studio : Cannot copy System. Diagnostics.Tools.dll,因为源文件不存在

c# - 在 InputField 中检测复制和粘贴

c# - 如何选择一个里面可能包含相同图案的图案?

c# - 将普通电子邮件地址解析为两部分

c# - 相当于C#中php的list()

.net - 如何通过网络客户端下载图像 (jpg) 并保存到 Windows Phone 7 上的独立存储?