I have instantiated a C# PowerShell object and added scripts like so:
var ps = PowerShell.Create();
ps.AddScript(@"& ""filename.ps1"" ""machine5"" ""stop"" ");
Collection<PSObject> results = ps.Invoke();
For reference, here are the contents of filename.ps1:
param([string] $middleTier, [string] $mode)
Write-Verbose "This does appear"
if($mode -eq "stop") {
Invoke-Command -ComputerName $middleTier -ScriptBlock {
Write-Verbose "This does not appear"
Stop-Service -Name "ARealService" -Force -Verbose
}
iisreset $middleTier /stop
}
However, after a call to ps.Invoke()
, all of my ps.Streams
collections are empty. I know for a fact that this script writes to the verbose stream when run, but the C# Powershell object does not seem to capture any of it. What am I doing wrong?
Edit: I have added some explicit calls to Write-Vebose
to show what does and does not get captured. It is clear that there is a problem with getting the stream output from the Invoke-Command
block.