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

PowerShell – Run script in new console window

Have you ever had to run powershell script in a new console window with your for example admin account?

param (
[Parameter(Mandatory=$true)]
[string]$command
)
runas /user:domain\user_name "powershell.exe -noExit -command $command"

I have that saved as “run-powershel_command.ps1

usage:
run-powershell_command.ps1 "get-aduser bob"

this will ask you for the password for the hardcodded username once that is provided it will open new window and execute the command.

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 ?"
pause
$vmView = Get-vm $vm | get-view
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.changeTrackingEnabled = $true
$vmView.reconfigVM($vmConfigSpec)

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

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)

ESXi – How to configure and enable SNMP

Just a quick PowerShell script that sets up SNMP on ESXI hosts

 

#Script to enable and configure SNMP on ESXI host
Get-VMHost | Select Name
$host_name = Read-Host "Host name:"
$location="London HQ"
$esxcli = get-esxcli -vmhost $host_name
$esxcli.system.snmp.set($null,"public","true",$null,$null,$null,$null,$null,$null,$null,$null,$null,$location)
$esxcli.system.snmp.get()
Write-host "Now go to the host and start the service"

Once that is done you still need to go the ESXi host and enable/start the SNMP service.