Using Multiple Wildcards with -like Operator

152 Views Asked by At

I'm attempting to parse the output of netsh winhttp show proxy to ensure the correct proxy settings have been applied with my script. To start, this is the expected output of the command:

Current WinHTTP proxy settings:

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

Below is a cut down version of what I'm doing. The if statement is evaluating to false when it should be true (I know the proxy settings are correct before running). I'm suspecting there's an issue with how I'm utilizing multiple wildcards with the -like operator, as evaluating only $ProxyServer or $BypassListEscaped on their own work, but I'm not sure. Any ideas? Thanks!

$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"
}
2

There are 2 best solutions below

0
Santiago Squarzon On

Output from netsh command is an array of strings, you need to convert that array into a single string before your comparison. You can also use WildcardPattern.Escape method for escaping wildcard characters.

$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'
}
1
Dheeraj On

Try this:

$output = netsh winhttp show proxy $output -like "Proxy Server:" -and $output -like ""