-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathNew-MSHVVirtualMachine.ps1
228 lines (198 loc) · 8.81 KB
/
New-MSHVVirtualMachine.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
#Requires -Version 5.0
#Requires -Modules Hyper-V
<#
.SYNOPSIS
Creates a new virtual machine
.DESCRIPTION
Use "Win2K12R2 or Win8.x" for execution on Windows Server 2012 R2 or on Windows 8.1,
when execute on Windows Server 2016 / Windows 10 or newer, use "Newer Systems"
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module Hyper-V
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/Hyper-V/VMs
.Parameter VMHostName
[sr-en] Name of the Hyper-V host
.Parameter VMName
[sr-en] Name of the new virtual machine. The default name is New virtual machine
.Parameter HostName
[sr-en] Name of the Hyper-V host
.Parameter AccessAccount
[sr-en] User account that have permission to perform this action
.Parameter VHDPath
[sr-en] Path to a existing virtual hard disk file
.Parameter NewVHDPath
[sr-en] Creates a new virtual hard disk with the specified path and connects it to the new virtual machine, e.g. C:\vhds\myvm.vhdx.
Absolute paths are allowed.
If only a file name is specified, the virtual hard disk is created in the default path configured for the host
.Parameter NewVHDSize
[sr-en] Size of the dynamic virtual hard disk that is created and attached to the new virtual machine, in bytes
.Parameter NoVHD
[sr-en] Creates a virtual machine without attaching any virtual hard disks
.Parameter BootDevice
[sr-en] Device to use as the boot device for the new virtual machine
.Parameter Generation
[sr-en] Generation, as an integer, for the virtual machine
.Parameter StartupMemory
[sr-en] Amount of memory, in bytes, to assign to the virtual machine, in bytes
.Parameter FilePath
[sr-en] Directory to store the files for the new virtual machine
.Parameter SwitchName
[sr-en] Friendly name of the virtual switch if you want to connect the new virtual machine to an existing virtual switch to provide connectivity to a network
.Parameter ProcessorCount
[sr-en] Number of virtual processors for the virtual machine
.Parameter DynamicMemory
[sr-en] Dynamic memory is to be enabled on the virtual machine to be configured
.Parameter MemoryMinimum
[sr-en] Minimum amount of memory that the virtual machine is to be allocated, in bytes
.Parameter MemoryMaximum
[sr-en] Maximum amount of memory that the virtual machine is to be allocated, in bytes
.Parameter Notes
[sr-en] Notes to be associated with the virtual machine
#>
param(
[Parameter(Mandatory = $true,ParameterSetName = "Win2K12R2 or Win8.x")]
[string]$VMHostName,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[string]$VMName,
[Parameter(ParameterSetName = "Newer Systems")]
[string]$HostName,
[Parameter(ParameterSetName = "Newer Systems")]
[PSCredential]$AccessAccount,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[string]$VHDPath,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[string]$NewVHDPath,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[uint64]$NewVHDSize,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[switch]$NoVHD,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[ValidateSet('Floppy', 'CD', 'IDE', 'LegacyNetworkAdapter', 'NetworkAdapter', 'VHD')]
[string]$BootDevice = "VHD",
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[ValidateSet(1,2)]
[int16]$Generation = 2,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[uint64]$StartupMemory = 536870912 ,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[string]$FilePath,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[string]$SwitchName,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[int]$ProcessorCount = 1,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[switch]$DynamicMemory,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[uint64]$MemoryMinimum,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[uint64]$MemoryMaximum,
[Parameter(ParameterSetName = "Win2K12R2 or Win8.x")]
[Parameter(ParameterSetName = "Newer Systems")]
[string]$Notes
)
Import-Module Hyper-V
try {
$Script:output
[string[]]$Properties = @('VMName','VMID','State','PrimaryOperationalStatus','PrimaryStatusDescription','CPUUsage','MemoryDemand','SizeOfSystemFiles','IntegrationServicesVersion')
if($PSCmdlet.ParameterSetName -eq "Win2K12R2 or Win8.x"){
$HostName=$VMHostName
}
if([System.String]::IsNullOrWhiteSpace($HostName)){
$HostName = "."
}
if([System.String]::IsNullOrWhiteSpace($VMName)){
$VMName = " "
}
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'}
if($null -eq $AccessAccount){
$cmdArgs.Add('ComputerName' ,$HostName)
}
else {
$Script:Cim = New-CimSession -ComputerName $HostName -Credential $AccessAccount
$cmdArgs.Add('CimSession' ,$Script:Cim)
}
if([System.String]::IsNullOrWhiteSpace($FilePath)){
$vmHost = Get-VMHost @cmdArgs | Select-Object *
$FilePath = $vmHost.VirtualMachinePath
}
$cmdArgs.Add('Name', $VMName )
$cmdArgs.Add('MemoryStartupBytes', $StartupMemory )
$cmdArgs.Add('Generation', $Generation)
$cmdArgs.Add('BootDevice', $BootDevice)
$cmdArgs.Add('Path', $FilePath)
$cmdArgs.Add('Force', $null)
if(($PSBoundParameters.ContainsKey('NewVHDPath') -eq $true) -and (-not [System.String]::IsNullOrWhiteSpace($NewVHDPath))){
if($NewVHDSize -lt 1048576){ # lower then 1 MB
$NewVHDSize = 1073741824 # default 1 GB
}
$Script:VM = New-VM @cmdArgs -NewVHDPath $NewVHDPath -NewVHDSizeBytes $NewVHDSize
}
elseif(($PSBoundParameters.ContainsKey('VHDPath') -eq $true) -and (-not [System.String]::IsNullOrWhiteSpace($VHDPath))){
$Script:VM = New-VM @cmdArgs -VHDPath $VHDPath
}
else{
$Script:VM = New-VM @cmdArgs -NoVHD
}
$cmdArgs = @{'ErrorAction' = 'Stop'}
if($null -eq $AccessAccount){
$cmdArgs.Add('ComputerName' ,$HostName)
}
else {
$Script:Cim = New-CimSession -ComputerName $HostName -Credential $AccessAccount
$cmdArgs.Add('CimSession' ,$Script:Cim)
}
if($PSBoundParameters.ContainsKey('SwitchName') -eq $true){
Connect-VMNetworkAdapter @cmdArgs -VMName $Script:VM.VMName -SwitchName $SwitchName
}
if($ProcessorCount -gt 1){
Set-VM -VM $Script:VM -ProcessorCount $ProcessorCount -ErrorAction Stop
}
if($DynamicMemory -eq $true){
Set-VMMemory -VM $Script:VM -DynamicMemoryEnabled $true -ErrorAction Stop
if($PSBoundParameters.ContainsKey('MemoryOnStartup') -eq $true ){
Set-VMMemory -VM $Script:VM -StartupBytes $StartupMemory -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('MemoryMinimum') -eq $true ){
Set-VMMemory -VM $Script:VM -MinimumBytes $MemoryMinimum -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('MemoryMaximum') -eq $true ){
Set-VMMemory -VM $Script:VM -MaximumBytes $MemoryMaximum -ErrorAction Stop
}
}
$output = Get-VM @cmdArgs -Id $Script:VM.Id | Select-Object $Properties
if($SRXEnv) {
$SRXEnv.ResultMessage = $output
}
else {
Write-Output $output
}
}
catch {
throw
}
finally{
if($null -ne $Script:Cim){
Remove-CimSession $Script:Cim
}
}