Friday, April 29, 2011

Change path selection policy using PowerShell

So I've discovered that I can do bulk changes to the path selection policy settings within vSphere using PowerShell and the VI Toolkit. The is very useful because I cannot set a default path selection policy with vSphere 4.1. So presentation of several new LUN's can be quite a pain without this script.

Get-VMHost | where {$_.State -eq “Maintenance”} | '
Get-ScsiLun -LunType "disk" | where {$_.MultipathPolicy –ne "RoundRobin"} | `
Set-ScsiLun -MultipathPolicy "RoundRobin"

The above script will; Select all hosts in maintenance mode, select each LUN that is not using RoundRobin and then configure each LUN to use RoundRobin for the path selection policy.

The ' in the syntax is used to move to the next line in the console so you don't have to scroll to the right or word wrap. Leave in or take out, either should allow it to work.

We can modify the above script so that it will work with a specified host instead of any host in maintenance mode;

Get-VMHost "hostname" | '
Get-ScsiLun -LunType "disk" | where {$_.MultipathPolicy –ne "RoundRobin"} | '
Set-ScsiLun -MultipathPolicy "RoundRobin"


It is important to note that both of the above commands will modify the path selection policies for each LUN presented to the host, including those that have not yet been set up as a datastore with a VMFS partition.

Thursday, April 28, 2011

My first blog and it's on PowerCLI...

So this is my first ever blog and I'm using it to record some PowerCLI syntax for storage vMotion automation. Is that cool or boring? I'm sure it must be boring in the extreme to anyone who doesn't have to spend hours migrating LUN's like me.

What I really wanted to acheive with these scripts/commands was to migrate all virtual machines from an old LUN to a new LUN. I have found out a simple way to do this with the following.

First command:

Get-Datastore "datastorename" | Get-VM | '
Move-VM -Datastore (Get-Datastore "datastorename") -DiskStorageFormat "Thin" -RunAsync

This script will get all virtual machines on a specified named datastore then sequentially send a vMotion command to each virtual machine and convert all disks to thin provisioning at the same time.
The -RunAsync command will tell the command line not to wait for the task to finish, but to have it processed by vSphere so I can continue working on the command line.

Second command:

Get-VM "vm_name" | '
Move-VM -Datastore (Get-Datastore "datastorename") -DiskStorageFormat "Thin" -RunAsync

This script is similar to the above but will do a single named virtual machine which is handy for targeting a couple of virtual machines that I want sent to a different datastore before the bulk of the moves are kicked off.

More posts to come in the future, mostly to do with my work on VMware solutions.