【问题标题】:PowerShell List ResultsPowerShell 列表结果
【发布时间】:2017-09-20 17:20:12
【问题描述】:

我正在尝试以 CSV 或 txt 格式列出以下 PowerShell 命令的结果:

Show_Proxy.ps1:

Invoke-Expression "netsh winhttp show proxy"

Server_Name.txt:

服务器01 服务器02 服务器03
$ServerList = Get-Content C:\temp\Server_Names.txt
Invoke-Command -FilePath C:\temp\Show_Proxy.ps1 -CN $ServerList

我确实看到了结果,但希望有一个文件列出服务器名称和天气它是否有代理服务器。

PS C:\Windows> $ServerList= Get-Content C:\temp\Server_Names.txt PS C:\Windows> Invoke-Command -FilePath C:\temp\Show_Proxy.ps1 -CN $ServerList 当前 WinHTTP 代理设置: 代理服务器:Proxy_Server_Name:8080 旁路列表: 当前 WinHTTP 代理设置: 代理服务器:Proxy_Server_Name:8080 旁路列表: 当前 WinHTTP 代理设置: 直接访问(无代理服务器)。

【问题讨论】:

    标签: powershell proxy output invoke-command


    【解决方案1】:

    将命令输出解析为自定义对象(运行netsh不需要Invoke-Expression):

    $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'}
    }
    

    然后可以将结果导出为 CSV:

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

    或以您喜欢的任何其他方式处理。

    【讨论】:

      【解决方案2】:

      您可以尝试使用 Invoke-Command -filepath C:\temp\Show_Proxy.ps1 -cn $ServerList | Export-Csv -NoTypeInformation -Path results.csv 之类的方法以 CSV 格式生成结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-23
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 2014-03-06
        • 2021-11-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多