-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathhyperv.ps1
233 lines (206 loc) · 7.12 KB
/
hyperv.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#requires -Version 2 -Modules Hyper-V
#implicitly import hyperv module to avoid powercli cmdlets
if ((Get-Module -Name 'hyper-v') -ne $null) {
Remove-Module -Name hyper-v
Import-Module -Name hyper-v
}
else {
Import-Module -Name hyper-v
}
$ProgressPreference = 'SilentlyContinue'
function New-DifferencingDisk {
[cmdletbinding()]
param (
[parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string[]]$Path,
[parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ParentPath
)
if (-not (Test-Path $Path)) {
$null = new-vhd @psboundparameters -Differencing
}
}
function Assert-VmRunning {
[cmdletbinding()]
param([string]$Id)
if ([string]::IsNullOrEmpty($Id)) {
$Output = [pscustomobject]@{
Name = ''
State = ''
}
}
else {
$Output = Get-VM -Id $Id |
ForEach-Object -Process {
if ($_.State -notlike 'Running') {
$_ |
Start-VM -passthru
}
else {
$_
}
} |
Select-Object -Property Name, Id, State
}
$Output
}
function New-KitchenVM {
[cmdletbinding()]
param (
$Generation = 1,
$DisableSecureBoot,
$MemoryStartupBytes,
$StaticMacAddress,
$Name,
$Path,
$VHDPath,
$SwitchName,
$VlanId,
$ProcessorCount,
$UseDynamicMemory,
$DynamicMemoryMinBytes,
$DynamicMemoryMaxBytes,
$boot_iso_path,
$EnableGuestServices,
$AdditionalDisks
)
$null = $psboundparameters.remove('DisableSecureBoot')
$null = $psboundparameters.remove('ProcessorCount')
$null = $psboundparameters.remove('StaticMacAddress')
$null = $psboundparameters.remove('UseDynamicMemory')
$null = $psboundparameters.remove('DynamicMemoryMinBytes')
$null = $psboundparameters.remove('DynamicMemoryMaxBytes')
$null = $psboundparameters.remove('boot_iso_path')
$null = $psboundparameters.remove('EnableGuestServices')
$null = $psboundparameters.remove('VlanId')
$null = $psboundparameters.remove('AdditionalDisks')
$DisableSecureBoot = [Convert]::ToBoolean($DisableSecureBoot)
$UseDynamicMemory = [Convert]::ToBoolean($UseDynamicMemory)
$null = [bool]::TryParse($EnableGuestServices, [ref]$EnableGuestServices)
$vm = new-vm @psboundparameters |
Set-Vm -ProcessorCount $ProcessorCount -passthru
if ($UseDynamicMemory) {
$vm | Set-VMMemory -DynamicMemoryEnabled $true -MinimumBytes $DynamicMemoryMinBytes -MaximumBytes $DynamicMemoryMaxBytes
}
else {
$vm | Set-VMMemory -DynamicMemoryEnabled $false
}
if (-not [string]::IsNullOrEmpty($boot_iso_path)) {
Mount-VMISO -Id $vm.Id -Path $boot_iso_path
}
if (-not [string]::IsNullOrEmpty($StaticMacAddress)) {
Set-VMNetworkAdapter -VMName $vm.VMName -StaticMacAddress $StaticMacAddress
}
if ($EnableGuestServices -and (Get-command Enable-VMIntegrationService -ErrorAction SilentlyContinue)) {
Enable-VMIntegrationService -VM $vm -Name 'Guest Service Interface'
}
if (($VlanId -ne $null) -and (Get-command Set-VMNetworkAdapterVlan -ErrorAction SilentlyContinue)) {
Set-VMNetworkAdapterVlan -VM $vm -Access -VlanId $VlanId
}
if ($AdditionalDisks -and (Get-command Add-VMHardDiskDrive -ErrorAction SilentlyContinue)) {
foreach ($AdditionalDisk in $AdditionalDisks) {
Add-VMHardDiskDrive -VM $vm -Path $AdditionalDisk
}
}
if ($DisableSecureBoot -and ($Generation -eq 2) -and (Get-command Set-VMFirmware -ErrorAction SilentlyContinue)) {
Set-VMFirmware -VM $vm -EnableSecureBoot Off
}
if ((Get-Command -Name Set-Vm).Parameters["AutomaticCheckpointsEnabled"]) {
Set-VM -Name $vm.VMName -AutomaticCheckpointsEnabled $false
}
$vm | Start-Vm -passthru |
foreach {
$vm = $_
do {
start-sleep -seconds 2
}
while ($vm.state -notlike 'Running')
$vm
} |
select Name, Id, State
}
function Get-VmIP($vm) {
start-sleep -seconds 10
$vm.networkadapters.ipaddresses |
Where-Object {
$_ -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'
} |
Select-Object -First 1
}
Function Set-VMNetworkConfiguration {
[CmdletBinding()]
Param (
[parameter(valuefrompipeline)]
[object]$NetworkAdapter,
[String[]]$IPAddress = @(),
[String[]]$Gateway = @(),
[String[]]$DNSServers = @(),
[String[]]$Subnet = @()
)
$vm = Get-WmiObject -Namespace 'root\virtualization\v2' -Class 'Msvm_ComputerSystem' | Where-Object {
$_.ElementName -eq $NetworkAdapter.VMName
}
$VMSettings = $vm.GetRelated('Msvm_VirtualSystemSettingData') | Where-Object {
$_.VirtualSystemType -eq 'Microsoft:Hyper-V:System:Realized'
}
$VMNetAdapters = $VMSettings.GetRelated('Msvm_SyntheticEthernetPortSettingData')
$NetworkSettings = @()
foreach ($NetAdapter in $VMNetAdapters) {
if ($NetAdapter.Address -eq $NetworkAdapter.MacAddress) {
$NetworkSettings = $NetworkSettings + $NetAdapter.GetRelated('Msvm_GuestNetworkAdapterConfiguration')
}
}
$NetworkSettings[0].IPAddresses = $IPAddress
$NetworkSettings[0].DefaultGateways = $Gateway
$NetworkSettings[0].DNSServers = $DNSServers
$NetworkSettings[0].Subnets = $Subnet
$NetworkSettings[0].ProtocolIFType = 4096
$NetworkSettings[0].DHCPEnabled = $false
$Service = Get-WmiObject -Class 'Msvm_VirtualSystemManagementService' -Namespace 'root\virtualization\v2'
$setIP = $Service.SetGuestNetworkAdapterConfiguration($vm, $NetworkSettings[0].GetText(1))
if ($setIP.ReturnValue -eq 4096) {
$job = [WMI]$setIP.job
while ($job.JobState -eq 3 -or $job.JobState -eq 4) {
Start-Sleep 1
$job = [WMI]$setIP.job
}
if ($job.JobState -ne 7) {
$job.GetError()
}
}
(Get-VM -Id $NetworkAdapter.VmId).NetworkAdapter | Select-Object Name, IpAddress
}
function Get-VmDetail {
[cmdletbinding()]
param($Id)
Get-VM -Id $Id |
ForEach-Object {
$vm = $_
do {
Start-Sleep -Seconds 1
}
while (-not (Get-VmIP $vm))
[pscustomobject]@{
Name = $vm.name
Id = $vm.ID
IpAddress = (Get-VmIP $vm)
}
}
}
function Get-DefaultVMSwitch {
[CmdletBinding()]
param ($Name)
Get-VMSwitch @PSBoundParameters |
Select-Object -First 1 |
Select-Object Name, Id
}
function Mount-VMISO {
[cmdletbinding()]
param($Id, $Path)
if ((Get-VM -Id $Id).Generation -eq 2) {
Add-VMDvdDrive (Get-VM -Id $Id).Name | Set-VMDvdDrive -VMName (Get-VM -Id $Id).Name -Path $Path
}
Set-VMDvdDrive -VMName (Get-VM -Id $Id).Name -Path $Path
}