-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImportMasterVMs.ps1
49 lines (40 loc) · 1.43 KB
/
ImportMasterVMs.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
44
45
46
47
48
49
$rootPath = 'C:\HyperV\VMs\Virtual Machines\'
$currentVMs = (Get-VM | % { Write-Output $_.Id.ToString().ToLower() })
if ($currentVMs.GetType().ToString() -ne 'System.Object[]') { $currentVMs = @($currentVMs) }
Function Import-Master($VMConfig) {
try {
$tempVM = (Compare-VM -Copy -Path $VMConfig.FullName -GenerateNewID -ErrorAction Stop).VM
} catch {
Write-Host "Unable to query ${VMconfig.FullName}" -ForegroundColor Red
return
}
$descriptiveName = "(${VMConfig}) $($tempVM.VMName)"
write-Host "Checking ${descriptiveName}..." -ForegroundColor Yellow
# Check the disks
$disksAreReadyOnly = $true
$tempVM.HardDrives | % {
$disk = $_
If (-Not (Test-Path -Path $disk.Path)) {
$disksAreReadyOnly = $false
Write-Host "Missing disk file $($disk.Path)" -ForegroundColor Red
} else {
if (-Not (Get-Item $disk.Path).IsReadOnly) {
$disksAreReadyOnly = $false
Write-Host "Disk file $($disk.Path) is not ReadOnly" -ForegroundColor Red
}
}
}
if (-not $disksAreReadyOnly) { return }
# Import the VM
Write-Host "Importing the VM" -ForegroundColor Cyan
Import-VM -Path $VMConfig.FullName -Confirm:$false
}
Get-ChildItem -Path $rootPath -File |
? { $_.Extension.ToUpper() -eq '.VMCX'} |
% {
if ($currentVMs -contains $_.BaseName.ToLower()) {
Write-Host "${_} is already imported. Ignoring" -ForegroundColor Green
} else {
Import-Master $_
}
}