forked from BenjaminArmstrong/Hyper-V-PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvacuateDisk.ps1
43 lines (30 loc) · 1.64 KB
/
EvacuateDisk.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
$sourcePath = "F:\"
$destinationPath = "D:\"
(Get-ChildItem $sourcePath -Recurse | Where-Object{$_.Attributes -ne "Directory"}).count
Get-VM | ForEach-Object{
$VM = $_
# Check the configuration path
$currentVMPath = $_.ConfigurationLocation
if ($currentVMPath.StartsWith($sourcePath)) {
$newVMPath = ($destinationPath) + ($currentVMPath.TrimStart($sourcePath))
Move-VMStorage -VM $VM -VirtualMachinePath $newVMPath}
# Check the snapshot path
$currentSnapshotFilePath = $_.SnapshotFileLocation
if ($currentSnapshotFilePath.StartsWith($sourcePath)) {
$newSnapshotFilePath = ($destinationPath) + ($currentSnapshotFilePath.TrimStart($sourcePath))
Move-VMStorage -VM $VM -SnapshotFilePath $newSnapshotFilePath}
# Check the smart paging file path
$currentSmartPagingFilePath = $_.SmartPagingFilePath
if ($currentSmartPagingFilePath.StartsWith($sourcePath)) {
$newSmartPagingFilePath = ($destinationPath) + ($currentSmartPagingFilePath.TrimStart($sourcePath))
Move-VMStorage -VM $VM -SmartPagingFilePath $newSmartPagingFilePath}
# Go over each hard drive
$_.HardDrives | ForEach-Object{
# Check the hard drive
$currentHardDrivePath = $_.Path
if ($currentHardDrivePath.StartsWith($sourcePath)) {
$newHardDrivePath = ($destinationPath) + ($currentHardDrivePath.TrimStart($sourcePath))
Move-VMStorage -VM $VM -VHDs @(@{"SourceFilePath" = $currentHardDrivePath; "DestinationFilePath" = $newHardDrivePath})}
}
}
(Get-ChildItem $sourcePath -Recurse | Where-Object{$_.Attributes -ne "Directory"}).count