The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

Nice to know – Basic PowerShell building blocks to create scripts

Posted by Mikael Nystrom on December 9, 2013

When using PowerShell you will after a while create a small collection of “stuff you can user than one time snippets” and as a speaker I use them as samples. During one of the 60 minutes of OSD with Johan and Mike I did a promise, to publish them, so here it is, a very basic but practical collection of snippets to use in your scripts.

Step One: Doing the “Get”

Using get is SAFE and a very practical way to start using powershell, I mean just finding the things you need to change/manage is “handy”, in this case, we use Get-VM to get all VMs in Hyper-V, but what we really need is to start the once not running, or something like that.

First line will get me ALL VMs, Second will get all that is off, third will give the same, but it will exclude the VM called TEST, fourth will do the same but also start all of them. Compare that line to “click-like-a-maniac” in Hyper-V manager…

Get-VM 
Get-VM | Where-Object -Property State -EQ -Value "Off" 
Get-VM | Where-Object -Property State -EQ -Value "Off" | Where-Object -Property Name -NE "TEST"| 
Get-VM | Where-Object -Property State -EQ -Value "Off" | Where-Object -Property Name -NE "TEST"| Start-VM

Step Two: Using parameters as input in your script

The Param bloc can be used to make a script act like a cmdlet. We will present some questions that we the “convert” into variables that can be used in the script:

Note: The Param block needs to be in the beginning of the script to work…

The first Param will ask for the name, and that is mandatory so we really need to fill that out, also the name needs to be at least 3 characters long, but not longer then 20 characters

The second param is the same, but that will be stored in $lastname instead of $firstname

The Third is a bit different, first it is not mandatory, second it has a default value and third it only allows Sales, Store or Management as valid options. Because we do that we can also tab thoose values on the command line when we run the script which is very nice

Param( 
[parameter(mandatory=$True,HelpMessage="First Name")] 
[ValidateLength(3,20)] 
$FirstName,

[parameter(mandatory=$true,HelpMessage="Last Name")] 
[ValidateLength(3,20)] 
$LastName,

[parameter(mandatory=$false,HelpMessage="Department")] 
[ValidateSet("Sales","Store","Managment")] 
$Department = "Store" 
)

$FirstName 
$LastName 
$Department

Step Three: If’s and But’s

Same story, but I just added a “if” and a “switch” block here. The If will check if you work at sales and the switch will check all different countries for various information. If I need an exclusion, I find If to the easiest way, but if there is a bunch of different scenarios, the switch is easier to use.

Param( 
[parameter(mandatory=$True,HelpMessage="First Name")] 
[ValidateLength(3,20)] 
$FirstName,

[parameter(mandatory=$true,HelpMessage="Last Name")] 
[ValidateLength(3,20)] 
$LastName,

[parameter(mandatory=$true,HelpMessage="Department")] 
[ValidateSet("Sales","Store","Management")] 
$Department,

[parameter(mandatory=$true,HelpMessage="Department")] 
[ValidateSet("Sweden","Norway","Finland")] 
$Country 
)

$FirstName 
$LastName 
$Department

If($Department -like "Sales") 
  {Write-Host "Sales is boring"} 
Else 
  {Write-Host "You are working in $Department"}

Switch($Country){ 
Sweden{ 
Write-Host "Contact Johan for salary discussions" 
} 
Norway{ 
Write-Host "Contact Harold for salary discussions" 
} 
Finland{ 
Write-Host "Contact Linus for salary discussions" 
} 
Default{Write-Host "Epic fail..." 
Break 
} 
}

Step Four: Reading data from XML file instead of typing everything on the command line…

Even if its nice to have all on the command line and using param, you will soon find that typing 192,168.1.1 as the default gateway every time you build a new VM using PowerShell is tidies (or boring), therefore you can store parameters that seldom change in a an XML file and read in the data like this:

$SettingsFile = ".\XMLSettings.xml" 
[xml]$Settings = Get-Content $SettingsFile -Verbose

$name = $Settings.Settings.Defaults.Name 
Write-Host $name

 

From the file that looks like this:

<Settings>
<Defaults>
  <Name>Frank</Name>
</Defaults>
</Settings>

As you can see, it is rather easy.

Step Five: Where am i?

Wrinting scripts is fun, but after a while you realize that hardcoding location is kind of bad, especially when you need to move the script and the data with it and of course there is a solution for that, just use the builtin variable called $MyInvocation and Split-Path to get the location script is running from and/or the name of the script it self.

$RunningFromFolder = $MyInvocation.MyCommand.Path | Split-Path -Parent 
Write-Host “Running this from $RunningFromFolder”

$ScriptName = $MyInvocation.MyCommand.Path | Split-Path –Leaf 
Write-Host “I can feel my name is…$ScriptName”

/mike

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: