How to use powershell to administer Hyper-v Cluser

I have 2 hosts both with Server 2016 Datacenter

On each host I have 2 VM's currently Dev using shared storage

I can do all the cluster things ie., Live migrate, move storage,

I have been looking all over google for a script to be able to save all the vm's in the cluster at the same time.

If i run this:

$clusterNodes = Get-ClusterNode;
$Name = ForEach($item in $clusterNodes){Get-VM -ComputerName $item.Name; }
{save-VM -ComputerName $item.Name; }

It will save the vm's however It will prompt me twice for the names of the VM's once for each host.

My over all goal is to be able to reconfigure all the VM's in the cluster from one PC, also the vm's move around a lot so they are not always on the same host.

Example:

Save all the vm's

Move the off os the shared storage:

Set-VM pv02 -SnapshotFileLocation C:\ClusterStorage\Volume1\pv02
Set-VM pv02 -SmartPagingFilePath C:\ClusterStorage\Volume1\pv02
Set-VMHardDiskDrive -VMName  pv02 -ControllerType SCSI -Path C:\ClusterStorage\Volume1\pv02\pv02.vhdx

Start the VM's

If i log into each host individually the command will run fine, however in my production I have 7 hosts with about 100 VM's.


If you want to address the entire cluster from PowerShell, the best solution is probably the PowerShell module in System Center Virtual Machine Manager. It was built for this.

The PowerShell module that you're using was intended for single-host scripting. It can still be made to work. You can restructure your code above. As an example:

$clusterNodeNames = (Get-ClusterNode -Cluster ClusterName).Name
$AllVMsInCluster = Get-VM -ComputerName $clusterNodeNames
$AllVMsInCluster | Save-VM -Confirm:$false