【发布时间】:2017-11-02 18:55:38
【问题描述】:
我继承了一个脚本,它遍历服务器列表中的一组服务器,然后为每个服务器输出一些内容。它使用 StringBuilder 将内容附加到变量,然后吐出结果......我如何让脚本存储内容,以便我可以在最后显示它并显示整个 foreach 的结果,而不是让它打印(然后覆盖)每次迭代?
目前我的结果如下所示:
ServerName1
Text1
下一次运行:
ServerName2
Text 2
我如何让它存储数据,然后在最后输出以下内容以便我可以通过电子邮件发送它?
ServerName1
Text1
ServerName2
Text2
我的代码:
foreach($Machine in $Machines)
{
Invoke-Command -ComputerName $Machine -ScriptBlock{param($XML1,$XML2,$XML3,$URL)
[System.Text.StringBuilder]$SB = New-Object System.Text.StringBuilder
$X = $SB.AppendLine($env:COMPUTERNAME)
if (Test-Path <path>)
{
$PolResponse = <somestuff>
$PolResponse2 = <somestuff>
Write-Host "[1st] $PolResponse" -ForegroundColor Magenta
Write-Host "[2nd] $PolResponse2" -ForegroundColor Magenta
$X = $SB.AppendLine($PolResponse)
$X = $SB.AppendLine($PolResponse2)
}
else
{
$PolResponse = "[1st] No Response"
$PolResponse2 = "[2nd] No Response"
Write-Host $PolResponse -ForegroundColor Red
Write-Host $PolResponse2 -ForegroundColor Red
$X = $SB.AppendLine($PolResponse)
$X = $SB.AppendLine($PolResponse2)
}
} -ArgumentList $XML1, $XML2, $XML3, $URL
}
# Sending result email
<<I want to send the TOTALITY of $SB here>>
【问题讨论】:
标签: powershell loops foreach