【发布时间】:2018-03-19 11:02:07
【问题描述】:
我需要Invoke-Command的变量输出,但是打印时显示为空,下面是示例代码:
$ServiceName = "Service"
Invoke-Command -ScriptBlock {
try {
iisreset
$BodyContent += "Server: <b>$server</b> IIS reset completed<br>"
}
catch {
$BodyContent += "Server: <b>$server</b> is Failed to restart IIS<br>"
$ErrorStat = 1
}
try {
Stop-Service -DisplayName $using:ServiceName
$BodyContent += "Server: <b>$server</b> is successfully Stopped $using:ServiceName<br>"
}
catch {
$BodyContent += "Server: <b>$server</b> is failed to Stop $using:ServiceName<br>"
$ErrorStat = 1
}
try {
Start-Service -DisplayName $using:ServiceName
$BodyContent += "Server: <b>$server</b> is successfully Started $using:ServiceName<br>"
}
catch {
$BodyContent += "Server: <b>$server</b> is failed to Start $using:ServiceName<br>"
$ErrorStat = 1
}
} -ComputerName $server -Credential $user -ErrorAction Stop
这里,我要捕获$BodyContent和$ErrorStat
【问题讨论】:
标签: powershell scope invoke-command