Wednesday, March 12, 2014

VMware PowerCLI tips and tricks

Instead of using the GUI for various vCenter/VMware tasks, it's much easier to download and install PowerCLI (you may want to do the following after installing it:

PowerCLI C:\>Set-ExecutionPolicy RemoteSigned

Also, if you're working on several vCenters like I do, you may want to change the window title to something like this:
$host.ui.RawUI.WindowTitle = "SET WINDOW TITLE HERE"

Here are some tasks you can easily do with the PowerCLI:

To find all the VM's with the name abcd1234 in their name:

1. Connect to the vCenter, open up PowerCLI and type:   Connect-VIServer <name of vCenter>
2. type  PowerCLI C:\> get-vm abcd1234*

You will see something like this

(sorry about the blackouts- this is from a production vCenter)

now let's say we wanted to power off all these VM's in order to change the memory to 7Gb of RAM and the CPU to 1 vCPU, we would do this:

1. PowerCLI C:\> get-vm abcd1234* | stop-vm -RunAsync -confirm:$false
2. PowerCLI C:\> get-vm abcd1234* | set-vm -MemoryGB 7 -NumCpu 1 -Notes "H/W modified $(Get-Date)" -confirm:$false

You may have a case where you have too many VM's show up, but your VM's are in a folder (as they should!)  you would then use:

PowerCLI C:\>get-vm -Location "foldermisc" | set-vm -MemoryGB 25 -NumCpu 1 -confirm:$false

If we wanted now to power all these VM's on, we would run:

PowerCLI C:\> get-vm abcd1234* | start-vm -RunAsync -confirm:$false

Another needed command is if you wanted to change a whole bunch of VM's to a different PortGroup (vlan) you would then do this for example:

Find out what the exact name of all the port groups are:

PowerCLI C:\> Get-VirtualPortGroup

Then set the vlan for the relevant VM's:

PowerCLI C:\> Get-VM pglap-* | Get-NetworkAdapter | Set-NetworkAdapter -N
etworkName dvPort-vlan1

If you wanted to avoid the confirmation of course, just add  -confirm:$false at the end, I forgot it above (!)

PowerCLI C:\> Get-VM pglap-* | Get-NetworkAdapter | Set-NetworkAdapter -N
etworkName dvPort-vlan1 -confirm:$false

Moving VM's from one datastore to another (Storage vMotion)

For example, I want to move all the VM's or just VM's with a certain name to another datastore.  I would use the same expressions as above, and do the following:

PowerCLI C:\> Get-VM -Location "NAME_OF_FOLDER" | Move-VM -DiskStorageFormat
Thin -Datastore "DATASTORE_2"

We have now moved all the VM's in "NAME_OF_FOLDER" (Obviously substitute that for the name of your actual folder, or do something like "get-vm abcd1234*")  to the new datastore "DATASTORE_2"

To check if they have in fact moved, we could run this command (this would show us all the VM's in vCenter, again you could do a get-vm just for the subset you want)

PowerCLI C:\>Get-VM | Select Name, @{N="Datastore";E={$_ | Get-Datastore}}

No comments:

Post a Comment