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

ESXi – How to list OS discrepancies

As VMs get upgraded to newer OSes the VM Guest OS setup on the ESXi hosts migth no longer be the same as the OS version the VM is running (i.e. VM got upgraded from Win 7 to 10 but ESXi host still thinks it is running 7).

Here is a one liner that shows what is set and what is actually there:
Get-View -ViewType "VirtualMachine" -Property @("Name", "Config.GuestFullName", "Guest.GuestFullName") | Where-Object {($_.Config.GuestFullName -ne $_.Guest.GuestFullName) -and ($_.Guest.GuestFullName -ne $null)} | Select-Object -Property Name, @{N="Configured OS";E={$_.Config.GuestFullName}}, @{N="Running OS";E={$_.Guest.GuestFullName}} | Format-Table -AutoSize

The output look something like that:

Name                                  Configured OS                       Running OS
----                                  -------------                       ----------
win2K                                 Microsoft Windows 2000 Server       Microsoft Windows 2000 Professional
RH3                                   Red Hat Enterprise Linux 3 (32-bit) Red Hat Enterprise Linux 3 (64-bit)
suse51                                SUSE Linux Enterprise 11 (64-bit)   SUSE Linux Enterprise 12 (64-bit)

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