So you want to Pin a VM to a host huh?
Maybe for some network mirror or span port entertainment?

Here is how you do it pcli style:

#set your vars:

$thecluster = “pcli-cluster1”
$thevmhost = “”
$thevm = “vm1”
$VMGroupName = “MyVMGroupName”
$HostGroupName = “MyHostGroupName”
$rulename = “MyAffinityRuleName”

#paste the code:

$cluster = Get-Cluster $thecluster | Get-View
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$group = New-Object VMware.Vim.ClusterGroupSpec
$group.operation = “add”
$group.Info = New-Object VMware.Vim.ClusterVmGroup
$group.Info.Name = $VMGroupName
Get-VM -Name $theVM | %{$group.Info.VM += $_.Extensiondata.MoRef}
$spec.GroupSpec += $group
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$group = New-Object VMware.Vim.ClusterGroupSpec
$group.operation = “add”
$group.Info = New-Object VMware.Vim.ClusterHostGroup
$group.Info.Name = $HostGroupName
Get-VMHost -Name $TheVMHost | %{$group.Info.Host += $_.Extensiondata.MoRef}
$spec.GroupSpec += $group
#Make the Rule
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$rule = New-Object VMware.Vim.ClusterRuleSpec
$rule.operation = “add”
$ = New-Object VMware.Vim.ClusterVmHostRuleInfo
$ = $true
$ = $RuleName
$ = $true
$ = $VMGroupName
$ = $HostGroupName
$spec.RulesSpec += $rule

I’m sure someone can reduce the code but I left it broken out here to provide the section view.
IE : If you only want to create a Host group, take the middle part of the script.

#Looks like playing with vFlash can cause some vCenter Webclient issues….
#KB =

#If you want to “remove” all vflash from your hosts so you can get things working again, you can use PowerCLI.
#1 – Download the “Extensions” for vflash (and vsan) here
– –
#2 – Place that folder into your powershell > Modules directory.
– – The most common places is : C:\Windows\System32\WindowsPowerShell\v1.0\Modules
#3 – Import the Extensions : Import-Module VMware.VimAutomation.Extensions
#4 – Verify that the Extensions loaded : get-command -Module VMware.VimAutomation.Extensions
– – Output should show ten new command-lets
#5 – Review your current vFlash config :
– – $hosts = get-cluster | Get-VMHost
– – Get-VMHostVFlashConfiguration -VMHost $hosts
– – Output will look close to this:
– – – Name CapacityGB SwapCacheReservationGB Extents

#6 – Remove the config with this command (you can make it smaller but this command worked best for me)
– – get-vmhost| get-VMHostVFlashConfiguration | Set-VMHostVFlashConfiguration -RemoveVFlashResource
– – get-vmhost| get-VMHostVFlashConfiguration | Set-VMHostVFlashConfiguration -RemoveVFlashResource

#7 – Then to verify that things are removed, throw another Get-VMHostVFlashConfiguration -VMHost $hosts


This One Line of code will list the count of VMs on each datastore in the Datastore Cluster
Just edit the datastore cluster name below:

foreach($a in (get-datastorecluster MyDataStoreCluster | get-datastore)){write-host $a ($a | get-vm).count}

List will look like this:

Datastore-1 12
Datastore-2 10
Datastore-3 9
Datastore-4 1
Datastore-5 42

This will check your ESXi host and tell you which LUNs support VAAI primitives.

$hostname = “”

$report2 = @()
$Datastorelist = get-vmhost $hostname | Get-Datastore
foreach ($ds in $datastorelist) {
$reportrow = “” | Select DatastoreName,CName
$reportrow.DatastoreName = $ds.Name
$reportrow.CName = $ | %{$_.diskname}
$report2 += $reportrow
$temp = get-esxcli -vmhost $hostname | %{$}
$report = @()
foreach($a in $temp){
$ReportRow = “” | Select-Object Hostname,LunName,Device,ATSStatus,CloneStatus,DeleteStatus,VAAIPluginName,ZeroStatus
$ReportRow.hostname = $hostname
$ReportRow.LunName = $report2 | where {$_.cname -eq $a.Device} | %{$_.datastorename}
$ReportRow.Device = $a.Device
$ReportRow.ATSStatus = $a.ATSStatus
$ReportRow.CloneStatus = $a.CloneStatus
$ReportRow.DeleteStatus = $a.DeleteStatus
$ReportRow.VAAIPluginName = $a.VAAIPluginName
$ReportRow.ZeroStatus = $a.ZeroStatus
$report += $reportrow
$report | Ft -a

This code will fully migrate a powered on VM to another cluster (On the same vcenter)
This will migrate the VMs storage and the memory state with one command.

### Set your vars
$vmname = “MyVM”
$going2Host = “”
$datastorename = “Datastore-1”

Get-VM $vmname | Move-VM -Destination $going2Host -Datastore (get-datastore “$datastorename”) -confirm:$false -RunAsync:$true


####### ####### ####### ####### ####### ####### #######
You can use this code if you want to select the datastore cluster instead of a single LUN.
This may error out if you have SDRS enabled on the VM.
Check this code to disable it before running the migration line below or just disable it via the GUI :

$vmname = “MyVM”
$going2Host = “”
$datastoreclustername = “MyDataStoreCluster”

Get-VM $vmname | Move-VM -Destination $going2Host -Datastore (get-datastorecluster “$datastoreclustername”) -confirm:$false -RunAsync:$true

VMware Links:

This code will reconfigure your Storage DRS Cluster Settings for a single VM.

### Set your vars ###
$theVM = “MyVM”
$theDatastoreCluster = “MyStorageCluster”

$SRMan = Get-View StorageResourceManager
$DSCluster = Get-DatastoreCluster -Name $theDatastoreCluster
$spec = New-Object VMware.Vim.StorageDrsConfigSpec
$DSCluster.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig |
where {(Get-View $_.VM | Select -ExpandProperty Name) -like $theVM} | %{
$entry = New-Object VMware.Vim.StorageDrsVmConfigSpec
$entry.Operation = “edit”
$entry.Info = $_
$entry.Info.Enabled = $false
$spec.vmConfigSpec += $entry

This code will aid in the automation of deleting Host Affinity rules via PowerCLI.

Currently the Get-DRSRule and Remove-DRSRule command does not allow the removal of VM-Host Affinity rules.
This code block will get a “VMHost Affinity” rule based on a string filter then delete it along with the VM group + Host group attached to it.
If you don’t want to delete the VM and host group, just remove the sub group specs below.

Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer “” -User “XX” -password “XX”

$Mycluster = get-cluster “MyCluster”
$therules = get-drsrule -cluster $mycluster -type vmhostaffinity | where {$ -match “RuleNameHere”}

foreach($rule in $therules){
$spec = New-Object VMware.Vim.ClusterConfigSpecEx
$spec.rulesSpec = New-Object VMware.Vim.ClusterRuleSpec[](1)
$spec.rulesSpec[0] = New-Object VMware.Vim.ClusterRuleSpec
$spec.rulesSpec[0].operation = “remove”
$spec.rulesSpec[0].removeKey = $rule.key
$spec.rulesSpec[0].info = New-Object VMware.Vim.ClusterVmHostRuleInfo
$spec.rulesSpec[0] = $
$spec.rulesSpec[0].info.vmGroupName = $rule.extensiondata.vmgroupname
$spec.rulesSpec[0].info.affineHostGroupName = $rule.extensiondata.AffineHostgroupname
$spec.groupSpec = New-Object VMware.Vim.ClusterGroupSpec[](2)
$spec.groupSpec[0] = New-Object VMware.Vim.ClusterGroupSpec
$spec.groupSpec[0].operation = “remove”
$spec.groupSpec[0].removeKey = $rule.extensiondata.vmgroupname
$spec.groupSpec[0].info = New-Object VMware.Vim.ClusterVmGroup
$spec.groupSpec[0] = $rule.extensiondata.vmgroupname
$spec.groupSpec[1] = New-Object VMware.Vim.ClusterGroupSpec
$spec.groupSpec[1].operation = “remove”
$spec.groupSpec[1].removeKey = $rule.extensiondata.AffineHostgroupname
$spec.groupSpec[1].info = New-Object VMware.Vim.ClusterHostGroup
$spec.groupSpec[1] = $rule.extensiondata.AffineHostgroupname
$_this = Get-View (get-cluster $Mycluster)
$_this.ReconfigureComputeResource_Task($spec, $true)

Here are some extra vars if you want to make a report from each rule”
#The name of the Rule

#name of the VM group and host group assigned to the Rule
$vmgroupname = $rule.extensiondata.vmgroupname
$hostgroupname = $rule.extensiondata.AffineHostgroupname

#list of VMs by name in the VM Group
$vmlist = get-vm -id $rule.vmids | %{$}

#list of hosts by name in the Host group
$hostlist = get-vmhost -id $rule.AffineHostIds | %{$}

VMware Links:

This code will grab the vmotion count that you see on the summary page of your cluster object. This counter is total vmotions to include system and manual migrations. If you want the manual ones you will need to parse out your vievents.

Get-Cluster | Select Name,@{N=”vMotion”;E={$_.ExtensionData.Summary.NumVmotions}}


This code will give you a list of all VM names and the OS configured for it. This code does not tell you what OS is running within your VM. You need vmware tools running within the guest to pull that info. You could you this script combined with the guest OS check to make sure they match.
**note – I added an if statement to filter if you only want Windows OS. Edit as needed.

connect-viserver “myvCenter”
$vmlist = @()
$GuestOSCheck = Get-VM | Get-View | Select name, @{N=”OS”;E={$_.Summary.Config.GuestFullName}}
foreach($single in $guestOSCHeck){
if($single.os -match “Windows”){
$vmlist += $single
Disconnect-viserver -confirm:$False

This will enable lockdown mode for your host.
Be careful with this because it will disable your ability to use the root account when you SSH to your host.

(get-vmhost “” | get-view).EnterLockdownMode()

To disable use:
(get-vmhost “” | get-view).ExitLockdownMode()