Search code examples
powershellproxyoutputinvoke-command

PowerShell List Results


I'm trying to list in a CSV or txt the results from the following PowerShell command:

Show_Proxy.ps1:

Invoke-Expression "netsh winhttp show proxy"

Server_Name.txt:

Server01
Server02
Server03

$ServerList = Get-Content C:\temp\Server_Names.txt
Invoke-Command -FilePath C:\temp\Show_Proxy.ps1 -CN $ServerList

I do see the results, but would like to have a file listing the server name and weather it has a proxy server or not.

PS C:\Windows> $ServerList= Get-Content C:\temp\Server_Names.txt
PS C:\Windows> Invoke-Command -FilePath C:\temp\Show_Proxy.ps1 -CN $ServerList

Current WinHTTP proxy settings:

    Proxy Server(s) :  Proxy_Server_Name:8080
    Bypass List     :  

Current WinHTTP proxy settings:

    Proxy Server(s) :  Proxy_Server_Name:8080
    Bypass List     :  

Current WinHTTP proxy settings:

    Direct access (no proxy server).

Solution

  • Parse the command output into custom objects (you don't need Invoke-Expression for running netsh):

    $output = netsh winhttp show proxy
    $null, $proxy = $output -like '*proxy server(s)*' -split ' +: +', 2
    
    New-Object -Type PSObject -Property @{
        'ComputerName' = $env:COMPUTERNAME
        'Proxy'        = if ($proxy) {$proxy} else {'direct'}
    }
    

    The result can then be exported to a CSV:

    Invoke-Command -FilePath 'C:\path\to\your.ps1' -ComputerName $ServerList |
        Export-Csv 'C:\path\to\output.csv'
    

    or processed in whatever other way you like.