我正在尝试解析
netsh winhttp show proxy
的输出,以确保我的脚本应用了正确的代理设置。首先,这是命令的预期输出:
Current WinHTTP proxy settings:
Proxy Server(s) : proxy.website.com:8080
Bypass List : *.example1.com;*.sample-site.com;
以下是我正在做的事情的简化版本。 if 语句在应该为真时评估为假(我知道代理设置在运行前是正确的)。我怀疑我如何将多个通配符与
-like
运算符一起使用存在问题,因为仅评估 $ProxyServer
或 $BypassListEscaped
在他们自己的工作中,但我不确定。有任何想法吗?谢谢!
$NetshOutput = (netsh winhttp show proxy)
$ProxyServer = "proxy.website.com:8080"
$ProxyBypass = "*.example1.com;*.sample-site.com;"
$BypassListEscaped = $ProxyBypass.Replace("*", "``*").Replace("`"", "")
if ($NetshOutput -like "*$ProxyServer*$BypassListEscaped*"){
Write-Host "correct"
}else{
Write-Host "incorrect"
}
netsh
命令的输出是一个字符串数组,您需要在比较之前将该数组转换为单个字符串。您还可以使用 WildcardPattern.Escape
方法 转义通配符。
$NetshOutput = (netsh winhttp show proxy) -join [System.Environment]::NewLine
$ProxyServer = 'proxy.website.com:8080'
$ProxyBypass = [WildcardPattern]::Escape('*.example1.com;*.sample-site.com;')
if($NetshOutput -like "*$ProxyServer*$ProxyBypass*") {
Write-Host 'correct'
}
else {
Write-Host 'incorrect'
}