The script as follows (Download from Technet), formatted using https://tohtml.com/powershell/:
$States = ( [Microsoft.HyperV.PowerShell.VMState]::Off, [Microsoft.HyperV.PowerShell.VMState]::OffCritical ) $VMs = Get-VM | ? {$_.State -in $States} $DriveCount = ($VMs | Get-VMHardDiskDrive).Count $Counter = 0 foreach($VM in $VMs) { $Drives = $VM | Get-VMHardDiskDrive foreach($Drive in $Drives) { $Counter += 1 # Some of these values (Path at least) disappear from the $Drive object when we remove it from the machine $ControllerNumber = $Drive.ControllerNumber $ControllerLocation = $Drive.ControllerLocation $Path = $Drive.Path $SupportPersistentReservations = $Drive.SupportPersistentReservations $ControllerType = $Drive.ControllerType Write-Progress -Activity "Reattaching drives" ` -Status "Removing $Path from $($VM.Name)" ` -PercentComplete (100*$Counter/$DriveCount) Remove-VMHardDiskDrive -VMHardDiskDrive $Drive if($SupportPersistentReservations) { # Shared vhdx Add-VMHardDiskDrive -VM $VM ` -ControllerNumber $ControllerNumber ` -ControllerLocation $ControllerLocation ` -Path $Path ` -ControllerType $ControllerType ` -SupportPersistentReservations } else { Add-VMHardDiskDrive -VM $VM ` -ControllerNumber $ControllerNumber ` -ControllerLocation $ControllerLocation ` -Path $Path ` -ControllerType $ControllerType } Write-Progress -Activity "Reattaching drives" ` -Status "Reattached $Path to $($VM.Name)" ` -PercentComplete (100*$Counter/$DriveCount) } }
Ingen kommentarer:
Send en kommentar
Bemærk! Kun medlemmer af denne blog kan sende kommentarer.