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 (
runas /user:domain\user_name "powershell.exe -noExit -command $command"

I have that saved as “run-powershel_command.ps1

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 ?"
$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.

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

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