Find the UUID and MoRef ID of a VM using PowerCLI

UUID

Get-VM VM_Name | %{(Get-View $_.Id).config.uuid}

MoRef ID

Get-VM VM_name | Get-View
Get-VM VM_name | Select Name, ID
Advertisements

Add multiple hosts to vCenter and license them using PowerCLI

If you are going to add multiple hosts to vCenter and license them, it will take a while if you do it manually. Instead you can PowerCLI script it.

Open vSphere PowerCLI console and connect to the vCenter.

Connect-VIServer -Server  $vCenterServerName

Next run the following commands for each host which add them to vCenter and then license them.

Add-VMHost $hostname  -location $cluster -user root -password $root_password  -force:$true
Set-VMHost -VMHost $hostname -LicenseKey $license_key

Continue reading

Create multiple VMs from an existing VMware VM by cloning using PowerCLI

I have a task to deploy 6 Appliance VMs from the same OVF template. I can deploy them manually one after the other which is a lot of labour and time. Instead, I can deploy one and clone it to create the rest. This is how to clone VM using PowerCLI:

Connect-VIServer -server $vCenter
New-VM -Name $NewVMName -VM $SourceVM -VMHost $TargetESXiHost -Datastore $TargetDatastore   -RunAsync

Assign permission to a list of VMs using PowerCLI

Copy the list of VMs to a file C:\vmlist.txt.

$vmlist = Get-Content C:\vmlist.txt
foreach ($i in $vmlist)
{
Get-VM -Name $i | New-VIPermission -Role "ROLE_NAME" -Principal "Domain\GROUP"
OR
Get-VM -Name $i | New-VIPermission -Role "ROLE_NAME" -Principal "Domain\USER"
}

To verify

$vmlist = Get-Content C:\vmlist.txt
foreach ($i in $vmlist)
{
Get-VM -Name $i | Get-VIPermission | Where-Object {$_.Role -like "ROLE_NAME*"}
}