Home > PowerCLI, PowerShell, vSphere > How to use Try – Catch in PowerCLI the easy way

How to use Try – Catch in PowerCLI the easy way

Sometimes you need to check if something exists.  For example, let’s check if a specific user exists on our ESX host (you need to connect directly to the ESX host for this example to work).

 

We do this by running:

Get-VMHostAccount -Id UserName

If the user exists, you get the user object back.  But if it doesn’t exist, the command gives you an error and your script continues with its operation.  This is because the default $ErrorActionPreference is set to Continue.

image

Let’s say we want to check if a user Test exists and if not we’ll create one.

We could do a Get-VMHostAccount –User:$true and loop the result to check if our User Test exists.  But this isn’t very efficient as we will fetch tons of users which we basically don’t need.

So it’s better to look for the user Test in the first place without fetching all those unnecessary users.  This is done with the Get-VMHostAccount –Id “Test” cmdlet.  This way of looking for a user’s existence is about 15 times faster than looping through all existing users!

If we build a Try – Catch loop around it, it’s easy to create the user if the Get-VMHostAccount produces an error:

Try

{

    Get-VMHostAccount -Id "Test"

    Write-Host "Found"

}

Catch

{

    Write-Host "Not Found, let's create one"

    New-VMHostAccount -Id "Test" -Password "Test" -WhatIf:$true

}

Let’s run it on our ESX server which has no Test user:

image

The Get-VMHostAccount produces an error (which is normal), but then the script writes the output Found!!!  This seems illogical at first sight (you would expect the Error be catched and have the user created), but actually makes sense.

A Try – Catch only works on Terminating errors.  Note that the default $ErrorActionPreference for PowerShell is set to Continue, which means that by default, errors are non-terminating (ensuring your scripts continues to run at all times).

So we’ll have to make sure the Get-VMHostAccount produces a Terminating error.  This is done by adding the –ErrorAction Stop parameter to the cmdlet.

Try

{

    Get-VMHostAccount -Id "Test" -ErrorAction Stop

    Write-Host "Found"

}

Catch

{

    Write-Host "Not Found, let's create one"

    New-VMHostAccount -Id "Test" -Password "Test" -WhatIf:$true

}

Let’s run this piece of code again:

image

Bingo!  Exactly what we need!

Fast and clean code!

 

You can use this approach for all kinds of things to check for:

– Check if a file exists (instead of looping through a directory)

– Check if a regkey exists

– Check if a VM exists (run Get-VM “Test” and catch the error)

– …

Advertisements
Categories: PowerCLI, PowerShell, vSphere
  1. No comments yet.
  1. No trackbacks yet.

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: