You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell -like运算符多通配符匹配问题排查

问题描述

我正尝试解析netsh winhttp show proxy的输出,验证脚本是否已正确应用代理设置。该命令的预期输出如下:

Current WinHTTP proxy settings:

    Proxy Server(s) :  proxy.website.com:8080
    Bypass List     :  *.example1.com;*.sample-site.com;

我编写了简化版脚本,已知代理设置正确,但if语句却返回false。单独匹配$ProxyServer或$BypassListEscaped时结果正常,怀疑是-like运算符的多通配符使用方式存在问题,求解决思路:

$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"
}
解决思路

核心问题有两个:

  1. 数组类型的输出匹配逻辑错误:netsh winhttp show proxy的输出是数组(每一行对应数组的一个元素),用-like对数组操作时,会返回所有匹配的元素而非布尔值,导致if判断逻辑失效。
  2. 匹配模式不符合输出结构:代理服务器和绕过列表在输出中分两行存在,但你的模式要求它们出现在同一行的任意位置,自然无法匹配成功。

针对这些问题,给出三种可行方案:

方案1:分别验证对应行内容

直接检查代理服务器行和绕过列表行是否存在预期内容,逻辑直观且稳定:

$NetshOutput = (netsh winhttp show proxy)

$ProxyServer = "proxy.website.com:8080"
$ProxyBypass = "*.example1.com;*.sample-site.com;"

# 用系统方法自动转义所有通配符,比手动替换更可靠
$BypassListEscaped = [WildcardPattern]::Escape($ProxyBypass)

# 分别匹配两行内容
$hasCorrectProxy = $NetshOutput -like "*Proxy Server(s) :  $ProxyServer*"
$hasCorrectBypass = $NetshOutput -like "*Bypass List     :  $BypassListEscaped*"

if ($hasCorrectProxy -and $hasCorrectBypass) {
    Write-Host "correct"
} else {
    Write-Host "incorrect"
}

方案2:合并输出为单字符串后匹配

先把数组格式的输出合并成单个字符串,再用包含换行符的模式匹配:

$NetshOutput = (netsh winhttp show proxy) -join "`n"

$ProxyServer = "proxy.website.com:8080"
$ProxyBypass = "*.example1.com;*.sample-site.com;"

$BypassListEscaped = [WildcardPattern]::Escape($ProxyBypass)

# 模式中加入换行符的通配匹配,`*`可匹配包括换行在内的任意字符
if ($NetshOutput -like "*$ProxyServer*`n*$BypassListEscaped*"){
    Write-Host "correct"
}else{
    Write-Host "incorrect"
}

注:该方式依赖输出的换行格式,稳定性略逊于方案1。

方案3:正则提取精确值对比

直接用正则提取输出中的代理和绕过列表值,再与预期值精确对比,完全规避通配符问题:

$NetshOutput = (netsh winhttp show proxy) -join "`n"

$expectedProxy = "proxy.website.com:8080"
$expectedBypass = "*.example1.com;*.sample-site.com;"

# 正则提取代理服务器值
$actualProxy = [regex]::Match($NetshOutput, 'Proxy Server\(s\) :\s*(.*)').Groups[1].Value
# 正则提取绕过列表值
$actualBypass = [regex]::Match($NetshOutput, 'Bypass List\s*:\s*(.*)').Groups[1].Value

if ($actualProxy -eq $expectedProxy -and $actualBypass -eq $expectedBypass) {
    Write-Host "correct"
} else {
    Write-Host "incorrect"
}

该方式精准度最高,推荐优先使用。

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:02:43