如何将Invoke-Command的结果保存到文件中

时间:2016-09-16 11:53:12

标签: powershell

我从文件中读取服务器列表:

$servidores = Get-Content "C:\ServerList.txt"

为每个服务器执行以下命令

foreach ($server in $servidores){
    Invoke-Command -ComputerName $server -ScriptBlock {Select-String -Pattern "something" -Path C:\*.txt -AllMatches}
}

我需要将结果存储在变量中,但无法找到最佳方法。

1 个答案:

答案 0 :(得分:1)

我希望你能尝试一下 -

$output = @()
foreach ($server in $servidores)
{
    $singleOutput = Invoke-Command -ComputerName $server -ScriptBlock {Select-String -Pattern "a" -Path C:\*.txt -AllMatches}
    $output+= $singleOutput
}
$output