{"id":4501,"date":"2009-01-28T21:50:33","date_gmt":"2009-01-28T21:50:33","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/01\/28\/getting-alternate-streams-verbose-progress-debug-with-the-powershell-api\/"},"modified":"2019-02-18T13:12:49","modified_gmt":"2019-02-18T20:12:49","slug":"getting-alternate-streams-verbose-progress-debug-with-the-powershell-api","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/getting-alternate-streams-verbose-progress-debug-with-the-powershell-api\/","title":{"rendered":"Getting Alternate Streams (Verbose, Progress, Debug) with the PowerShell API"},"content":{"rendered":"<p>Dozens of people could write dozens of books on all of the cool stuff we improved in V2 of PowerShell.&#160; One of the nice little things we did was give a more understandable API than Pipeline (which is the class you can use to invoke code from C# in V1).&#160; This API is called the PowerShell API, and one of the improvements it makes is the ability to get to alternate data streams.<\/p>\n<p>Alternate data streams are a particularly fun thing about PowerShell. While you can have a lot of output streams in DOS, it was anyone&#8217;s guess exactly what any of them were.&#160; PowerShell has several alternate data streams that have interesting information.&#160; They are: Verbose, Warning, Debug, Error, and Progress.<\/p>\n<p>Today, Vivek Sharma (a PM in Exchange) wanted to get some samples about how to get at the Verbose Stream in PowerShell.&#160; Here&#8217;s an example of how to get at it with the V2 PowerShell API:<\/p>\n<p class=\"PowerShellColorizedScript\"><span style=\"color: #006400\"># Create a PowerShell Command<\/span>    <br \/><span style=\"color: #ff4500\">$pscmd<\/span>&#160;<span style=\"color: #a9a9a9\">=<\/span>&#160;<span style=\"color: #008080\">[PowerShell]<\/span><span style=\"color: #a9a9a9\">::<\/span><span style=\"color: #000000\">Create<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">)<\/span>    <br \/><span style=\"color: #006400\"># Add a Script to change the verbose preference.<\/span>    <br \/><span style=\"color: #006400\"># Since I want to make sure this change stays around after I run the command I set UseLocalScope to $false.<\/span>    <br \/><span style=\"color: #006400\"># Also note that since AddScript returns the PowerShell command, I can simply call Invoke on what came back.<\/span>    <br \/><span style=\"color: #006400\"># I set the return value to $null to suppress any output<\/span>    <br \/><span style=\"color: #ff4500\">$null<\/span>&#160;<span style=\"color: #a9a9a9\">=<\/span>&#160;<span style=\"color: #ff4500\">$psCmd<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AddScript<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #ff4500\">$VerbosePreference<\/span>&#160;<span style=\"color: #a9a9a9\">=<\/span>&#160;<span style=\"color: #8b0000\">&quot;Continue&quot;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #a9a9a9\">,<\/span><span style=\"color: #ff4500\">$false<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Invoke<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">)<\/span>    <br \/><span style=\"color: #006400\"># If I added more commands, I&#8217;d be adding them to the pipeline, so I want to clear the pipeline<\/span>    <br \/><span style=\"color: #ff4500\">$psCmd<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Commands<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Clear<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">)<\/span>    <br \/><span style=\"color: #006400\"># Now that I&#8217;ve cleared the pipeline, I&#8217;ll add another script that writes out 100 messages to the Verbose stream<\/span>    <br \/><span style=\"color: #ff4500\">$null<\/span>&#160;<span style=\"color: #a9a9a9\">=<\/span>&#160;<span style=\"color: #ff4500\">$psCmd<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AddScript<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #800080\">1<\/span><span style=\"color: #a9a9a9\">..<\/span><span style=\"color: #800080\">100<\/span>&#160;<span style=\"color: #a9a9a9\">|<\/span>&#160;<span style=\"color: #0000ff\">Foreach-Object<\/span>&#160;<span style=\"color: #000000\">{<\/span>&#160;<span style=\"color: #0000ff\">Write-Verbose<\/span>&#160;<span style=\"color: #ff4500\">$_<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Invoke<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #000000\">)<\/span>    <br \/><span style=\"color: #006400\"># Finally, I&#8217;ll output the stream<\/span>    <br \/><span style=\"color: #ff4500\">$psCmd<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Streams<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Verbose<\/span><\/p>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dozens of people could write dozens of books on all of the cool stuff we improved in V2 of PowerShell.&#160; One of the nice little things we did was give a more understandable API than Pipeline (which is the class you can use to invoke code from C# in V1).&#160; This API is called the [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[137],"class_list":["post-4501","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-ctp3"],"acf":[],"blog_post_summary":"<p>Dozens of people could write dozens of books on all of the cool stuff we improved in V2 of PowerShell.&#160; One of the nice little things we did was give a more understandable API than Pipeline (which is the class you can use to invoke code from C# in V1).&#160; This API is called the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4501"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4501\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}