In 2013 – Make it personal, learn about PowerShell.

Here’s a response a gave in Linkedin this month and I thought I should blogged about it.

There’s no excuse for not wanting to learn about PowerShell

First, There’s really the only requirement would be to have PowerShell installed on your machine. If you have Windows 7 then you are set to go. Next look for free resource material which you can find on the internet:

http://powershell.org/wp/powershell-books/http://powershell.com/cs/

http://www.maxtblog.com/2012/07/windows-powershell-v3-0-resource-links-start-now/

http://www.maxtblog.com/2010/07/free-powershell-resources/

Also, PowerShell include an extensive help documentation at your fingertips. Just type the following command to access all the “About_*” topics you can read on:
Ps C:> help About_*

Start first reading about the “about_execution_policies“:

PS C:> help about_execution_policies

There’s some TechNet 2011 and 2012 recorded sessions that are very informative. *Check them out:

Channel 9 – Look at the PowerShell videos (many areas): http://channel9.msdn.com/tags/PowerShell/

TechEd North America 2012 “PowerShell” videos: http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012?sort=sequential&direction=desc&term=PowerShell

* I recommend to start learning everything about PowerShell version 2.0.

Then, you can use forums and even use the Twitter hash tag #PowerShell and #PShelp so the community can give assits those in need.

PowerShell V2.0 -> PowerShell v3.0 -> PowerShell V2.0…

There one thing to take into consideration when working with PowerShell V3.0. Most companies might still being in process of adopting V2.0 and if is not already in place. Version 3.0 brings a lot to the table but can cause some headaches if you forget the script you built (using 3.0) might not work in Version 2.0. Keep a closer look at the v3.0 member enumerator enhancement (which I love), and the behavior changes on some of the existing(updated) cmdlets.

So, with this in mind. Ask yourself, will my script work in PowerShell V2.0?. Then test the script loading the “PowerShell -version 2.0” at the PowerShell 3.0 console prompt to execute the script. This way you can verify, and at the same time learn to work with both V2 and V3 difference.

PowerShell v3.0 is a great .NET scripting language to learn! Once you start using PowerShell you’ll never stop.

🙂

Building PowerShell Custom Objects Quick Tips

Let’s get right to the point. Here are a total of 4 simple tips to help you build custom PowerShell objects in both Version 2.0 and 3.0:

[sourcecode language=”powershell”]
##
## Tip 1 – You don’t need to use single quotes the labels:
##

$value01 = ‘Max’; $value02 = ‘Theresa’;
$hashobj = @{ `
label1 = $value01; `
label2 = $value02; `
};
$hashobj;

[/sourcecode]

[sourcecode language=”powershell”]
##
## Tip 2 – In PowerShell version 2.0, you can use the New-Object to convert the
## – Hash Object to a PSobject:
##

$NewPSObj = New-Object PSObject -Property $hashobj;
$NewPSObj.GetType();

$NewPSObj;

[/sourcecode]

[sourcecode language=”powershell”]
##
## Tip 3 – Better yet, you can include the Hash object code in the property
## parameter as a Scriptblock:
##

$NewPSObj2 = New-Object PSObject -Property @{ `
label1 = $value01; `
label2 = $value02; `
};

$NewPSObj2.GetType();

$NewPSObj2;

[/sourcecode]

[sourcecode language=”powershell”]
##
## Tip 4 – In PowerShell version 3.0, it gets better because you just add the
## accelator type [PSCustomObject] to the hash object and this will
## convert to a PSobject:
##

$value01 = ‘Max’; $value02 = ‘Theresa’;
$hashobj2 = [pscustomobject] @{ `
label1 = $value01; `
label2 = $value02; `
};
$hashobj2.GetType();

$hashobj2;

[/sourcecode]

Also, in this code you’ll see the use of the space follow by a tick ” `” which is a “line continuation”, and the semi-colon “;” which is commonly use as a line terminator. Both are optional but useful.

PowerShell – Start getting your Computer Last BootUp Time

The PowerShell community has done an Excellent job in providing LOTs of sample codes. But sometime can be very intimidating for a beginner to understand. Well, PowerShell can be both simple, and complicated but not impossible to learn.

Is good to know there are other ways other to get the same or similar result. And I’m not against it because you can even integrate those solutions into PowerShell.

BUT, you need to realized this is a SKILL you need nowadays. PowerShell can’t be ignore any more. If you don’t build up this skill then you’re probably going to lose a job opportunity.

Now, here’s a series of oneliners to assist you in getting the computer BootUp time information:

[sourcecode language=”powershell”]
## – Simple way to start knowing your PSobject
$x = get-wmiObject -Class Win32_OperatingSystem -Computername "YourComputerName";

## – Discovering you PSobject content:
$x | get-member;

## – Sample getting the PSObject ‘Property’ names for computername and Lastboottime:
$x | Select csname, LastBootUpTime;

## – Need to use the "ConvertToDateTime" script method which is in your PSobject $x:
$x.ConvertToDateTime($x.LastBootUpTime);

## – Wee need to create a one-liner to display the date/time correctly
## – using scriptblock expression:
$x | Select csname, `
@{label=’MyLastBootUpTime’;Expression={$_.ConvertToDateTime($_.LastBootUpTime)};} `
| format-table -autosize;

## – Now, pay attention to the Get-member results and you’ll find the following methods:
# Reboot()
# SetDateTime()
# Shutdown()
# Win32Shutdown()
# Win32ShutdownTracker()
## – You will realized the PSObject you just created can be use to perform
## – task such as: reboot or shutdown.

## – This is an example to create an Array list of servernames:
$srvList = @("Server1","Server2","Server3");

###### BUILDING A SCRIPT with above onliners ########
## – Now we need to loop through each of the servers in the array with ‘Foreach’
## – and display the results on screen:

$srvList = @("Server1","Server2","Server3");

foreach($server in $Srvlist)
{
$x = get-wmiObject -Class Win32_OperatingSystem -Computername $server;
$x | Select csname, `
@{label=’MyLastBootUpTime’;Expression={$_.ConvertToDateTime($_.LastBootUpTime)};} `
| format-table -autosize;
};

## OR, the next sample will create a PSObject from you looping results:

$srvList = @("Server1","Server2","Server3");

[array] $myUpTimeList = foreach($server in $Srvlist)
{
$x = get-wmiObject -Class Win32_OperatingSystem -Computername $server;
$x | Select csname, `
@{label=’MyLastBootUpTime’;Expression={$_.ConvertToDateTime($_.LastBootUpTime)};};
};

$myUpTimelist | ft -auto;

## – End of scripts

[/sourcecode]

This is your starting code block. This code can be improved to suite your need by adding more PSobject properties and/or routing the results to an output file.

I hope this can give some insight on how PowerShell can help in your admin tasks.