Advanced Windows PowerShell Scripting Video Training

Wednesday, February 27, 2013

PowerShell scripters have a variety of methods that they can use to display information on the host. Before we begin this lesson, remember that PowerShell should send an object as it’s output to the PowerShell pipeline. In many cases, the author of a cmdlet may want to display other information. This can include debugging or progress information. Here are a few cmdlets and some information to help you understand their usage.

Write-Output

This is the proper way of sending output into the PowerShell pipeline. Remember that this should be the object that is the end result of the code that was just executed. This is also how we return information from a function to its calling statement.

Write-Host

This is the easiest cmdlet to use to instantly display information on the host. The –ForeGroundColor and –BackGroundColor parameters allow you to use 16 different colors to emphasis your displaying information.

This cmdlet is dependent on the setting of the environmental variable $DebugPreference. By default, it is set to SilentlyContinue. By setting this variable to Continue, you will be able to write debug information to the host. The advantage of this cmdlet is that it does not display information by default. That means you can leave your debug comments in your script for later use and the user will not see them when the cmdlet is executed unless the user sets their $DebugPreference$ to continue.

Write-Host"Here is some code"

$Debugpreference='Continue'

Write-Debug"Here is the debug information"

$Debugpreference='SilentlyContinue'

Write-Debug"This debug message will not display."

Write-Host"End of Script"

Here is some code

DEBUG: Here is the debug information

End of Script

You can see that once the $DebugPreference variable was set to SilentlyContinue, the second debug message is not displayed.

One of the common parameters is –Debug. By using this common parameter, you can turn on debug messages. This switch will override the value of $DebugPreference value. The difference is that it will pause script execution at each debug message. Since the script is still running in memory, you can examine the contents of memory by placing the script in Suspend mode.

FunctionTest

{

[cmdletbinding()]

Param()

Write-Host"Here is some code"

Write-Debug"Here is the debug information"

Write-Debug"This debug message will not display."

Write-Host"End of Script"

}

Write-Verbose

This write cmdlet is controlled by the $VerbosePreference environmental variable. It is also set to SilentlyContinue. This one is actually a common parameter. Talk a look at the help file About_CommonParameters to learn more. This means that you can use the –Verbose switch with your cmdlet to see this information. This cmdlet is best used to provide progress information for the cmdlets execution.

FunctionTest

{

[cmdletbinding()]

Param ([Switch]$PassThru)

Write-host"Hello"

Write-Verbose"World"

}

PS C:\> Test

Hello

PS C:\> Test -Verbose

Hello

VERBOSE: World

Write-Warning

This cmdlet can be used to write a warning to the host. It is controlled by the $WarningPreference variable. It is set to Continue by default.

PS C:\> Write-Warning "This is a Warning Meassage"

WARNING: This is a Warning Meassage

PS C:\> $Var1 = "This is also a message"

PS C:\> $Var2 = "And so is this one."

PS C:\> $Var1, $Var2 | Write-Warning

WARNING: This is also a message

WARNING: And so is this one.

Write-Error

Write-Error is a bit more complex to use. The detailed help file for this cmdlet provides more detailed information on how to use this cmdlet. Write-Error places an object into the Error stream. Look at this example.

Wednesday, February 20, 2013

This is an interesting question that I picked up from my moderator duties on PowerShell.com. The question was how do I know what scripts are being run by my GPOs? The function below will enumerate all of your GPOs and then let you know what scripts are being run.