0

Ghost In The (Power)Shell

After taking an existing Exchange PowerShell script, and running on a newer version of Exchange, the output was not as expected.    This is a pretty simple script that just iterates through all of the Exchange virtual directories and writes the output to the screen.  Yes it uses Write-Host and some consider that to be evil.  Others say "Friends do not let Friends use Write-Host".  Oh well. This is a super-simple script that is just used to quickly verify a few configuration elements.

Write-Host does allow you to easily add colour to highlight the output, which can be useful.  Especially if you really like magenta.  You may notice this has been one of my favourite colours...

The below is an example of a script which heavily uses Write-Host, and such scripts have been around since the Exchange 2007 days. Well, maybe not the -ADPropertiesOnly bit, or the MAPI/HTTP but you get the point.  The base cmdlets have been around for a while 🙂

PowerShell Script Using Write-Host

Original Script Execution Behaviour

Exchange 2007 and 2010 used older versions of PowerShell.

The below is an example of output on an Exchange 2010 server on Windows Server 2008 R2.

PowerShell Script Output Using Write-Host on Exchange 2010

The output is synchronous and is as expected.  Note that the additional lines are due to the Exchange 2013 and 2016 servers with the Front End and Back End sites.  Exchange 2010 does not know or care about that.

Exchange 2010 is not aware of this, so it shows both.

What's New in Windows PowerShell 5.0 - PowerShell | Microsoft Docs

Weekend Scripter: Welcome to the PowerShell Information Stream | Scripting Blog (microsoft.com)

Understanding Streams, Redirection, and Write-Host in PowerShell | Scripting Blog (microsoft.com)

Current Script Execution Behaviour

We see the ame behaviour on Exchange 2013 on Windows Server 2012 R2.  Same for Exchange 2016 Management tools on Windows Server 2012.

Exchange 2010 on Windows Server 2012 R2 does the same thing.

 

New Script Execution Behaviour

However, running Exchange on a newer version of Windows Server changes the behaviour. The same script on Exchange 2016 server does not produce the expected result.

Note that information is not present where it would have been previously, and it is either clumped in a different section or data is not visible at all.

Note that there is nothing in the SCP section, and a multitude of URLs are in the EWS section.  If you look at the full URL, not that most things that are in the EWS section are not actuall EWS.

PowerShell Script Output Using Write-Host on Exchange 2016

Why?  This is not due to Exchange, this is due to PowerShell changes.

PowerShell  has made some changes over the years.  Exchange 2010 added the requirement for PowerShell 2.0 as remoting is used to connect using the management tools.

PowerShell 5 Write-Host Behaviour

In PowerShell 5, a change was introduced to how Write-Host is handled.  Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

The PSVersion information for the Exchange 2016 server is shown below, and yes it has PowerShell 5 installed.

Exchange 2016 Server With PowerShell 5 Installed

This means that scripts may need to be modified, and certain features taken into consideration.

The $InformationPreference preference variable value determines whether the message you provide to Write-Information is displayed at the expected point in a script's operation. Because the default value of this variable is SilentlyContinue, by default, informational messages are not shown. If you don't want to change the value of $InformationPreference, you can override its value by adding the InformationAction common parameter to your command. For more information, see about_Preference_Variables and about_CommonParameters.

This was covered by the Scripting Guys previously Weekend Scripter: Welcome to the PowerShell Information Stream.   The below image illustrates the changes to output streams. Firstly they illustrate the older stream options:

PowerShell Output Streams Versions 1 -> 4

Note that in PowerShell 5.0 there is now an additional stream, number 6, the Information stream.

PowerShell Output Streams Versions 5

This is what the Write-Host cmdlet is now wrapped around.  Since it uses the information stream, whether or not the output is displayed is no longer guaranteed.

Time to update that script…

Cheers,

Rhoderick

Rhoderick Milne [MSFT]

Leave a Reply

Your email address will not be published. Required fields are marked *