1

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.

Mystagogue
  • 343
  • 1
  • 3
  • 8
  • I've noticed that I can redirect the verbose stream to the output stream using 4>&1 and capture the output in the results collection. However, I would really like to just get the real verbose stream from the Invoke-Command scriptblock. – Mystagogue Oct 20 '16 at 18:30
  • http://stackoverflow.com/questions/1146575/how-to-capture-a-powershell-cmdlets-verbose-output-when-the-cmdlet-is-programma You can access the verbose stream $ps.streams.verbose. – superaitaotao Mar 06 '17 at 05:59

1 Answers1

0

Your script block has its own verbose preference ($VerbosePreference). Set it to Continue inside script block then it should work.

Adarsha
  • 2,267
  • 22
  • 29