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: