I need output of variable from Invoke-Command
, but when printing it is showing empty, below is the sample code:
$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
Here, I want to capture $BodyContent
and $ErrorStat