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.


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
  • Jake can you explain to me how the '$AllVMsInCluster | Save-VM -Confirm:$false' is looping through each VM, I am trying to get the second part of my task done, the '$AllVMsInCluster | Set-VM -SnapshotFileLocation C:\ClusterStorage\Volume1\ ' but I cannot figure out how to set the Directory at the end, ie. 'C:\ClusterStorage\Volume1\VM1', 'C:\ClusterStorage\Volume1\VM2' – Anthony Fornito Sep 7 '17 at 20:33
  • You need to read a book on PowerShell. The fundamental concept is the "pipeline," and objects flow through it. A "cmdlet" acts on each of the objects that passes to it on the pipeline. If you want to vary the behavior as things pass through, you probably want a construct like: $AllVMsInCluster | Foreach-Object {Set-VM -SnapshotFileLocation C:\ClusterStorage\Volume1\$_.Name} – Jake Oshins Sep 8 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.