Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Tuesday, November 25, 2014

Giving Thanks

This post may be a little off topic, so please bare with me.

These past few weeks I have been providing Windows and PowerShell training in Asia and the south Pacific. We took a longer than usual lunch break yesterday so the Sailors of the United States Navy in my class can enjoy the one Thanksgiving meal that they will get this year.

What ever nation you claim as your own, please remember your sons and daughters who have volunteered to spend their time away from their families so you can be free to spend time with yours. Not far from where I am right now, freedom is only a distant light on the horizon. A beacon of hope for a brighter future. Be thankful that you may stand in that light and not gaze at it from a distance.

To all of my Brothers and Sisters who have volunteered to keep us all free,  Happy Thanksgiving.

10670134_10153282105405715_2436738459856340706_n

Monday, November 24, 2014

Adding properties to PowerShell Output

This morning on PowerShell.com, I helped with a question about adding data to an output.  Essentially, there were two data sources and the user needed the data to be presented in one object so they could pipe it to Export-CSV.  The Select-Object cmdlet has the ability to add data to an object.  Take a look at this code.

1

2

3

4

5

6

7

8

9

10

11

12

13

 

$Servers= "Server1", "Server2"

$MasterArray = @()

ForEach($Server in $Servers)

{

 

    If(Test-Connection -quiet -computer $server)

    {

    $Result=Get-Counter -Counter "\\$server\Processor Information(_Total)\% Processor Time"

    $Result | Select-Object -Property *,

        @{N="Online";E={$True}}

    }

 

}

 

This is the complete code.  What was missing was lines 9 and 10.  Lines 9 and 10 are actually the same line.

On line 9, I am using the comma to act as a line continuation character.  Line 10 is where the magic starts.  On line 9, we are piping the object stored in $Results to the Select-Object statement.  We have chosen to retain all of the properties of the object.  The “,” tells PowerShell that I want more.

Line 10 is a hash table.  The value of “N” is going to be the Name of the property.  The value of “E” is what to assign as the value of the property “Online”.  Yes, you need the extra {} as part of the expression.  In other words, let’s say that $Result has two properties; Name and Counter.  In this case, the question was how to add the result of  Test-Connection to the object.  Inside of the IF block is where we add the hash table to the Select-Object statement. The Hash table adds the third property of Online to the object.  If the connection was not made, we would not be getting any information.

For better design, consider using error handling and creating the object with both the computer name and setting the Online value to False.  That way you have a record of which clients were contacted and which ones were not.