-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHyperV_Import.ps1
More file actions
138 lines (115 loc) · 4.66 KB
/
Copy pathHyperV_Import.ps1
File metadata and controls
138 lines (115 loc) · 4.66 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.SYNOPSIS
Importiert eine virtuelle Maschine (VM) aus einem Backup-Verzeichnis oder listet alle verfügbaren VMs auf, mit Optionen für Standardpfade.
.DESCRIPTION
Dieses Skript kann entweder eine oder mehrere VMs aus einem angegebenen Backup-Verzeichnis importieren oder alle im Backup-Verzeichnis verfügbaren VMs auflisten. Der Modus wird über den Parameter -Type gesteuert. Standardpfade werden verwendet, wenn keine spezifischen Pfade angegeben sind.
.AUTHOR
Erhard Rainer
http://erhard-rainer.com
.DATE
2024-04-04
.EXAMPLE
PS> .\HyperV_Import.ps1 -Type Import -ImportPath "\\192.168.0.185\Backup\HyperV\"
.EXAMPLE
PS> .\HyperV_Import.ps1 -Type List -ImportPath "\\192.168.0.185\Backup\HyperV\"
.PARAMETER ImportPath
Pfad zum Verzeichnis, in dem das VM-Backup gespeichert ist.
.PARAMETER VmPath
Optionaler Parameter. Zielverzeichnis für die importierte VM. Wenn nicht gesetzt, wird der Standardpfad verwendet.
.PARAMETER VhdPath
Optionaler Parameter. Zielverzeichnis für die VHDs der importierten VM. Wenn nicht gesetzt, wird der Standardpfad verwendet.
.PARAMETER Type
Der Modus des Skripts: 'List' für das Auflisten aller VMs im Backup-Verzeichnis, 'Import' für das Importieren einer oder mehrerer VMs.
.PARAMETER Name
Optionaler Parameter. Der Name der spezifischen VM, die importiert werden soll. Nur relevant, wenn Type auf 'Import' gesetzt ist.
.VERSIONHISTORY
2024-04-03 - 1.0 - Initiale Version
2024-04-04 - 1.1 - Hinzufügen des Type Parameters und erweiterte Funktionalität
2024-04-04 - 1.2 - Integration der Standardpfade für VmPath und VhdPath
2024-04-04 - 1.3 - Administator Prüfung eingebaut
.NOTES
Lizenz: Creative Commons Attribution 4.0 International License (CC BY 4.0)
#>
param(
# ImportPath ist verpflichtend
[Parameter(Mandatory=$true)]
[string]$ImportPath,
# VmPath ist optional
[Parameter(Mandatory=$false)]
[string]$VmPath,
# VhdPath ist optional
[Parameter(Mandatory=$false)]
[string]$VhdPath,
# Type ist verpflichtend und muss entweder 'List' oder 'Import' sein
[Parameter(Mandatory=$true)]
[ValidateSet('List', 'Import')]
[string]$Type,
# Name ist optional
[Parameter(Mandatory=$false)]
[string]$Name
)
# Funktion zum Importieren von VM(s) basierend auf einem Pfad
function Import-VMFromPath {
param(
[string]$VmConfigPath,
[string]$VmPath,
[string]$VhdPath
)
try {
$importedVm = Import-VM -Path $VmConfigPath -Copy -VhdDestinationPath $VhdPath -VirtualMachinePath $VmPath
Write-Host "VM wurde erfolgreich importiert: $($importedVm.VMName)"
} catch {
Write-Error "Fehler beim Importieren der VM aus dem Pfad VmConfigPath: $_"
}
}
# Überprüfung, ob das Skript als Administrator ausgeführt wird
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Error "Dieses Skript muss als Administrator ausgeführt werden."
exit
}
# Ermittlung der Standardpfade, falls notwendig
if (-not $VmPath) {
$VmPath = (Get-VMHost).VirtualMachinePath
}
if (-not $VhdPath) {
$VhdPath = (Get-VMHost).VirtualHardDiskPath
}
# Initialisierung des Rückgabewertes
$success = $false
switch ($Type) {
'List' {
# Listet alle verfügbaren VMs im Import-Path auf
try {
$vms = Get-ChildItem -Path $ImportPath -Directory
Write-Host "Verfügbare VMs im Verzeichnis $ImportPath"
$vms | ForEach-Object { Write-Host $_.Name }
$success = $true
} catch {
Write-Error "Fehler beim Auflisten der VMs: $_"
}
}
'Import' {
try {
if ($Name) {
# Importiere spezifische VM
$specificVmPath = Join-Path -Path $ImportPath -ChildPath $Name
$vmcx = Get-ChildItem -Path $specificVmPath -Recurse -Filter "*.vmcx" | Select-Object -First 1
if ($vmcx) {
Import-VMFromPath -VmConfigPath $vmcx.FullName -VmPath $VmPath -VhdPath $VhdPath
} else {
Write-Error "Keine VM-Konfiguration (.vmcx) im Pfad $specificVmPath gefunden."
}
} else {
# Importiere alle VMs
$allVmcxFiles = Get-ChildItem -Path $ImportPath -Recurse -Filter "*.vmcx"
foreach ($vmcx in $allVmcxFiles) {
Import-VMFromPath -VmConfigPath $vmcx.FullName -VmPath $VmPath -VhdPath $VhdPath
}
}
} catch {
Write-Error "Fehler beim Importieren der VM(s): $_"
}
}
}
# Rückgabe, ob das Skript erfolgreich war oder nicht
# return $success