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

Developing PowerShell modules for REST APIs – Part1

Over the years I have developed different PowerShell modules for different web APIs. I thought it would be a good idea to write a 2 series post about how you could go about to do this. This will be a 2 part blog series where we will run through the entire process of building a module for a REST API. I will try my best to keep this as simple as possible and leave more advanced stuff for a follow up post if the interest is there.What you needDepending on your experience with source control and PowerShell in general, you might want to use GIT or some other software repro for the code. In addition we are going to create a test REST API using the splendid UniversalDashboard PowerShell module created by Adam Driscoll. It is available on the PowershellGallery. Other prerequisites are built-in to Powershell. I will assume that you will be following along using at least PowerShell version 5 or greater.
What is HTTP metods for REST API.The primary or most common HTTP verbs used are POST, GET, PU…

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 …

Developing PowerShell modules for REST APIs – Part2

This is part 2 of the REST API blogpost. In part1 we successfully setup two REST API endpoints using the UniversalDashboard PowerShell module. In this part we are going to create a simple module that support some CRUD operation against our API. As we are trying to keep things as simple as possible, we will not use any fancy framework (like Plaster) to build our module. We are also going to skip a very important step you should familiarize yourself with, Pester tests. Lets get to it.


The moduleWe will build a module called FilesAPI. The module folder will look like this:



In the functions folder I have already added the 2 helper functions from part 1, Get-AuthorizationHeader and ConvertTo-Base64. The other folders are just placeholders for important stuff like classes, private functions that you do not want to make available for the module consumer and tests for Pester tests. For such a small module that we are going to create, one could argue that it is much easier to just add the functi…