Thursday, March 13, 2014

Deploying multiple VM's using VMware PowerCLI




Today we will detail how to deploy multiple VM's ( I would say the minimum number you want to do this is at 10 VM's, however up to you)

The question is not to just deploy 10+ VM's of the same configurations, then you dont need this, you can do a simple one liner, or you could follow this post.

We are talking about deploying let's say 25 VM's, but from a certain template, and into a specific folder and each VM has a different hostname, memory allocation and CPU, as well as a static IP.

In this case, you need to create an excel file, which you'll save as CSV, with the following columns:

VMName
VMHost
Datastore
Template
Customization
IPAddress
Subnetmask
DefaultGateway
DNS

You could add some more columns in there if you wanted, for example for the memory and CPU.

Then you would need to install PowerCLI of course, I have another post talking about that briefly, 

You would then put this CSV file in the directory of your choice, in my case it's at C:\Scripts\
and modify this command below to your likings.  I highlighted the parts you need to change:


Import-Csv "C:\Scripts\NewVMsCR2.csv" -UseCulture | %{
    Get-OSCustomizationSpec $_.Customization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $_.IPAddress -SubnetMask $_.Subnetmask -DefaultGateway $_.DefaultGateway
    $vm=New-VM -Name $_.VMName -Template $_.Template -Host $_.VMHost -Location "WEB SERVERS" -Datastore $_.Datastore -Confirm:$false -RunAsync -OSCustomizationSpec $_.Customization
}

You do need to have the template that's referenced in the excel sheet, as well as the customization profile, and in the case of the command above, also the folder "WEB SERVERS"  otherwise it will error out.

However once all is ready, you are now ready to deploy as many VM's as you need.

Here is a picture of it in action (some IP's changed for security reasons)


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}}