Automation

 View Only
  • 1.  Disable DRS rule then enable it

    Posted Apr 18, 2023 05:35 AM

    I'm writing a VM migration script to storage migrate VMs form old to new datastores. In the situation where a VM has its swap file on a datastore which is designated as a host swap file (Configure | Swap File Location | Default swap file location),and you change the host's swap file location to a new datastore, the only way to migrate the VMs off the old swap datastore to the new is to host-migrate the VMs.

    My script will host-migrate the VM from host1 to host2 back to host1 which effectively moves the VM's swap file to the newly setup host swap file. One issue is that if there are DRS Host Rules pinning the VM to a specific host, my code fails. I thus need to disable any rules which that VM is a part of, host-migrate the VM then re-enable those rules.

    So far I've been able to get this code to work, which gets the DRS host rules which the respective VM is part of:

    $VMHostGroup = Get-VM -Name $VMName | Select Name,@{N='DRSGroup';E={$script:group = Get-DrsClusterGroup -VM $_; $script:group.Name}},@{N='GroupType';E={$script:group.GroupType}}
    $DRSRule = Get-DrsVMHostRule -Cluster $MyCluster -VMGroup $VMHostGroup.drsgroup

    How do I disable the rules, do my thing with the VM, then enable the rules afterwards?

    Major Minor Build Revision
    ----- ----- ----- --------
    5 1 14393 5582

     

     



  • 2.  RE: Disable DRS rule then enable it

    Posted Apr 18, 2023 05:49 AM

    Sorry, for some reason this was not working earlier but is working OK now:

    $DRSRuleDisabled = $DRSRule | Set-DRSVMHostRule -Enabled $False
    $DRSRuleDisabled = $DRSRule | Set-DRSVMHostRule -Enabled $True

     

    Done!