PowerShell – Set VM permissions

Here is my PS way of granting user access to a VM in vSphere

$vmname = read-host "Enter Virtual Machine name"
$username = read-host "Enter User name (i.e. DOMAIN\username)"
$roles = Get-VIRole | select name | % {$counter = -1} {$counter++; $_ | Add-Member -Name Role_ID -Value $counter -MemberType NoteProperty -PassThru}
$roles | ft -auto
$myRole = read-host "select Role ID"
$role_selection = $roles[$myRole]
get-vm $vmname | New-VIPermission -Role (Get-VIRole -Name $role_selection.name) -Principal $username

VMware – PowerCLI enable Change Block Tracking

Check CBT status for all VMs:

Get-VM | select Name, @{N="CBT";E={(Get-View $_).Config.ChangeTrackingEnabled}}

To enable CBT

Write-Host "This script enables CBT for a VM"
$vm = read-host "Specify VM name"
Get-VM $vm | select Name, @{N="CBT";E={(Get-View $_).Config.ChangeTrackingEnabled}}
write-host "Proceed ?"
$vmView = Get-vm $vm | get-view
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.changeTrackingEnabled = $true

Get-VM $vm | select Name, @{N="CBT";E={(Get-View $_).Config.ChangeTrackingEnabled}}
Write-host "Done.

Managing VMware Tools advanced options in PowerShell

Here is a quick way of selecting the VMware Tools Advanced option: “Check and upgrade Tools during power cycling” for all VMs in a cluster. Here is a screen shot of the option in question:


And here is the script:

$vms = get-cluster "Cluster Name" | get-vm

foreach($vm in $vms) 
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.changeVersion = $vm.ExtensionData.Config.ChangeVersion
$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
$spec.tools.toolsUpgradePolicy = "upgradeAtPowerCycle"
$_this = Get-View -Id $vm.Id

How to find virtual machine name from a MAC address using PowerShell

Stating the obvious:
1. you need vCenter server
2. you need to be connected to your VC server from PowerShell

To get the VM name from MAC address just run this command
get-vm | Get-NetworkAdapter | where {$_.macaddress -eq "00:50:56:b4:3d:12"} | select parent,macaddress

or save this as a script i.e. get-VMfromMAC.ps1
$mac = Read-Host "What mac address do you want to lookup (format:11:22:33:44:55:66 )"
get-vm | Get-NetworkAdapter | where {$_.macaddress -eq "$mac"} | select parent,macaddress