Skip to main content

Write-Host – Will the puppies survive

Just updated my virtual computer running Win10. Yes, I am a chicken and have not upgraded my computer from 8.1 yet. Some time in the future I will find time to upgrade, however in the mean time I am quite happy with just running it as a VM.

My current build of powershell is 10041 (yes, I know I am falling behind). I was tweeting with Jan Egil (@JanEgilRing). He mentioned a new cmdlet and a couple of new common parameters that has become available in this build.

image

We have two new common parameters:
  • InformationAction
  • InformationVariable

Using my previous get all common parameters, we can indeed confirm that these are new common parameters:

image

There is also a new cmdlet called Write-Information which has 2 parameters:

  • [object]MessageData
  • [string[]]Tags

There is a new $InformationPreference global variable set to “Continue” by default.

image




In action


So here is the first attempt to write something in the console:

image

Write-Information produced no information in the console. Turns out it is not a bug, however a feature. The output/stream from the cmdlet is filtered out by the console and not shown by default. Before we get into a little bit more advanced stuff, remember that InformationVariable is a new common parameter for all cmdlets and advanced functions.

How can we use InformationVariable and Write-Information?


Glad you asked. Turns out there is a couple of nice use cases for this. First we need to get out hands on that information stream, it has to be there somewhere:


(Get-PowershellProcess)

What happens if we run Get-PowershellProcess function and assign the InformationVariable to a variable:

image

Nothing gets written to the console, however what is the content of our $Info variable?

image

A couple of things to note here. First off the $Info variable is an ArrayList which makes sense. Secondly each item is of type InformationRecord. It is not an new type, it has been around since powershell 2.0, however it has changed a bit. Currently it supports these properties:


  • Computer
  • ManagedThreadId
  • MessageData
  • NativeThreadId
  • ProcessId
  • Source
  • Tags
  • TimeGenerated
  • User


image

This is the InformationRecord produced by our single write-information line. We get the time it was generated, the source (script) where it originated from, the user, computer and thread information for free. In addition the tags will be stored in the tags property as an collection and finally the messagedata is stored in the messageData property.

Now just for the fun of it, lets change our function to include a Write-Host statement.

(Get-PowershellProcess with write-host)

If you check the info variable:

image

That is interesting. We now have to (2) informationRecords in our information variable.

image

Yes, pigs are flying here. The Write-Host cmdlet is now producing output by writing to the information stream. Things to note here is that the messagedata contains the object/text you wrote using write-host. Secondly every Write-Host statement is tagged with PSHOST.

If you don’t know about streams and redirect in powershell, June Blender has written a nice post about it on the Scripting Guy’s blog, link here. With the release of WMF5 this will have to be updated since the information stream is number 6, previously we had 5:
  1. Output/Success
  2. Error
  3. Warning
  4. Verbose
  5. Debug
  6. Information
So if you need to redirect the information stream you should do:

$outputWithStreams = Get-Powershellprocess 6>&1

This will capture all output and the information stream including write-host "output".

How could you use this new cmdlet


On the top of my head this can be used in a couple of scenarios. First and foremost I will probably be using it for extra verbose(/information logging and with the tags option, I can really do filtering on the records being produced by the Write-Information cmdlet.

Secondly if you combine the output of Write-Information with the Export-CliXML cmdlet, you could build a “log-file” that contains real objects that you can import with Import-CliXML and filter the result.

Thirdly it makes no sense in not creating advanced powershell functions since this new powerful feature will be available for those. Start adding [cmdletBinding()] to your functions with an Param() block (yes it can be empty) and you are ready to go. If you are on twitter and missed the first #PSblogWeek you should look it up and see all the help the community and MVP's are producing to help you write advanced functions and help in powershell. 

Fourthly I will most likely move my excess verbose logging over to Write-Information due to the increased possibility of filtering using tags and datetime.



My thoughts


If you have been following the debate around use/do not use Write-Host because it kills puppies, this looks like a nice save by Microsoft and the powershell team. I really like the possibility to tag the information stream and later us it for filtering. Also I appreciate the source and timegenerated properties that will help if you are chasing bugs in a module or big script.

This will also most likely replace many of the "logging-modules/functions" people have been using since it is normally more convenient to use built-in stuff than to roll your own tool.

Even if Write-Host now writes to the information stream, I think you should still avoid using it in your scripts and relay on all the other streams to produce logging, verbose, warning and error records. My worry is that this most likely will not convince people to change the default behavior of using Write-Host for feedback to the console user. Time will show, however we still need to educate current and new users of the powershell kingdom to embrace the possibilities lying before their feet.

Cheers

Tore

Comments

Popular posts from this blog

Serialize data with PowerShell

Currently I am working on a big new module. In this module, I need to persist data to disk and reprocess them at some point even if the module/PowerShell session was closed. I needed to serialize objects and save them to disk. It needed to be very efficient to be able to support a high volume of objects. Hence I decided to turn this serializer into a module called HashData.



Other Serializing methods

In PowerShell we have several possibilities to serialize objects. There are two cmdlets you can use which are built in:
Export-CliXmlConvertTo-JSON
Both are excellent options if you do not care about the size of the file. In my case I needed something lean and mean in terms of the size on disk for the serialized object. Lets do some tests to compare the different types:


(Hashdata.Object.ps1)

You might be curious why I do not use the Export-CliXML cmdlet and just use the [System.Management.Automation.PSSerializer]::Serialize static method. The static method will generate the same xml, however we …

Build your local powershell module repository - ProGet

So Windows Powershell Blog released a blog a couple of days ago (link). Not too long after, a discussion emerged about it being to complicated to setup. Even though the required software is open source (nugetgalleryserver), it looks like you need to have Visual Studio Installed to compile it. I looked into doing it without visual stuidio, however I have been unable to come up with a solution. I even tweeted about it since I am not an developer. Maybe someone how is familiar with “msbuild” could do a post on how to do it without VS.

Anyhow one of my twitter-friends (@sstranger) came to the rescue and pointed me in the direction of ProGet, hence the title of this post. ProGet comes in 2 different licensing modes
Free (reduced functionality)Enterprise (paid version with extra features)The good news is that the free version supports hosting a local PowershellGet repository which was my intention anyway. So off we go and create a Configration that can install ProGet for us. This is the conf…

Monitoring Orchestrator runbook events from Operations Manager

Today I will follow up on my colleague’s post Mr ITblog (Knut Huglen) about monitoring Orchestrator Runbook events.  He has build a nice double up SNMP loopback feature that does self monitoring in Orchestrator resulting in entries written to a special Windows Eventlog. Now we need to raise alerts in SCOM when one of his runbooks fails or sends a platform event, who knows there could be trouble lurking in his paradise.

We are not going to do anything fancy, however these are the steps we will be focusing on today:
Create a Management Pack for our customizations Create rules that collects the events from the orchestrator serverOff we go then and fire up the SCOM console and a powershell window. First we create a MP, I am going to use powershell to do this, however you may use the SCOM console as well (Administration – ManagementPacks – Action: Create Management Pack):



Import the Management Pack into SCOM and move on to the Authoring section in the SCOM console. Create a new rule:



Give the…