Sunday, June 22, 2014

One-liner to list names, NAA ids of the boot luns in a cluster

One-liner to list hosts, datastore names, and NAAs of the boot luns in a cluster.
get-cluster "cluster01" | get-vmhost | sort-object name | get-datastore | where-object {$_.CapacityGB -lt 100} | select @{n="host";e={($_ | get-vmhost | select name).name}}, name, @{n="NAA";e={($_ | get-scsilun | select canonicalname).canonicalname}}


Broken down:
  1. Get-cluster "cluster01" - start by getting the cluster
  2. get-vmhost - get all the ESXi hosts in the cluster
  3. sort-object name - optional to sort the hosts by name
  4. get-datastore - get the datastores attached to the hosts
  5. where-object {$_.CapacityGB -lt 10} - filters only the small datastores*.
  6. Select-object - pick the properties to display
  7. @{n="host";e={($_ | get-vmhost | select name).name}} - shows the name of the vmhost
  8. name - shows the name of the datastore object
  9. @{n="NAA";e={($_ | get-scsilun | select canonicalname).canonicalname}} - shows the naa of the datastore
*Adjust this where clause depending on how you can identify the boot luns in your environment. For example, if you name all your boot luns with 'boot' in them, you could use where-object {$_.name -like "*boot*"}. I pick all the small datastores because if the datastore is less than 10GB in our environment, it's always a boot lun.

Not all the fields may display completely on your screen. You can always pipe to out-file to dump the info to a text file or pipe to format-list

Sample output:
host     Name          NAA
----     ----          ---
hostab01 hostab01-boot naa.6005076801818...
hostab02 hostab02-boot naa.6005076801818...
hostab03 hostab03-boot naa.6005076801808...
hostab04 hostab04-boot naa.6005076801808...
hostab05 hostab05-boot naa.6005076801808...

Sunday, June 8, 2014

Storage vMotion just one hard disk using PowerCLI

I was looking for a quick way to storage vMotion some of a VMs hard disks but not all of them. I found a pretty quick one-liner at ict-freak.nl
Get-HardDisk -vm | Where {$_.Name -eq "Hard disk <#>"} | `
% {Set-HardDisk -HardDisk $_ -Datastore "" -Confirm:$false}
However, I found that Set-HardDisk is deprecated. Move-HardDisk is the preferred method. The help file for Move-Hard disk had some simple examples.
$vm = get-vm "MyVM"
$mydisk = $vm | get-harddisk -name "Hard Disk 3"
$mydatastore = get-datastore -name "SAN2"
move-harddisk -harddisk $mydisk -datastore $mydatastore


These can be combined into a one-liner like so:
Move-HardDisk -harddisk (get-vm "MyVM" | get-harddisk -name "Hard Disk 3") `
-datastore (get-datastore -name "SAN2") -confirm:$false -runasync

Worked perfectly for me.