forked from scriptrunner/ActionPacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-VMHVVirtualMachine.ps1
201 lines (170 loc) · 7.92 KB
/
New-VMHVVirtualMachine.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
#Requires -Version 5.0
# Requires -Modules VMware.PowerCLI
<#
.SYNOPSIS
Creates a new virtual machine
.DESCRIPTION
.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 VMware.PowerCLI
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/VMware/VMs
.Parameter VIServer
[sr-en] Specifies the IP address or the DNS name of the vSphere server to which you want to connect
[sr-de] IP Adresse oder DNS des VSphere Servers
.Parameter VICredential
[sr-en] Specifies a PSCredential object that contains credentials for authenticating with the server
[sr-de] Benutzerkonto für die Ausführung
.Parameter HostName
[sr-en] Specifies the name of the host on which you want to create the new virtual machine
[sr-de] Host in dem die neue virtuellen Maschine erstellt wird
.Parameter VMName
[sr-en] Specifies a name for the new virtual machine
[sr-de] Name der virtuellen Maschine
.Parameter Notes
[sr-en] Provides a description of the new virtual machine
[sr-de] Beschreibung
.Parameter Cpus
[sr-en] Specifies the number of the virtual CPUs of the new virtual machine
[sr-de] Anzahl der virtuellen CPUs
.Parameter CoresPerSocket
[sr-en] Specifies the number of virtual CPU cores per socket
[sr-de] Anzahl der virtuellen CPUs pro Socket
.Parameter MemoryGB
[sr-en] Specifies the memory size in gigabytes (GB) of the new virtual machine
[sr-de] Größe des Arbeitsspeichers in Gigabyte (GB)
.Parameter DiskGB
[sr-en] Specifies the size in gigabytes (GB) of the disks that you want to create and add to the new virtual machine
[sr-de] Größe der Festplatte in Gigabyte (GB)
.Parameter DiskStorageFormat
[sr-en] Specifies the storage format of the disks of the virtual machine
[sr-de] Dateisystem der Festplatte
.Parameter CD
[sr-en] Indicates that you want to add a CD drive to the new virtual machine
[sr-de] CD-Laufwerk zur neuen virtuellen Maschine hinzufügen
.Parameter Floppy
[sr-en] Indicates that you want to add a floppy drive to the new virtual machine
[sr-de] Diskettenlaufwerk zur neuen virtuellen Maschine hinzufügen
.Parameter Network
[sr-en] Specifies the network to which you want to connect the new virtual machine
[sr-de] Netzwerk der neuen virtuellen Maschine
.Parameter GuestId
[sr-en] Specifies the guest operating system of the new virtual machine
[sr-de] Identifier des Betriebssystems der neuen virtuellen Maschine
.Parameter OSCustomizationSpec
[sr-en] Specifies a customization specification that is to be applied to the new virtual machine.
This works only in 32-bit mode
[sr-de] Benutzerdefinierte Einstellungen der neuen virtuellen Maschine
Nur für 32Bit
.Parameter HardwareVersion
[sr-en] Specifies the version of the new virtual machine.
By default, the new virtual machine is created with the latest available version
[sr-de] Hardware Version der neuen virtuellen Maschine
.Parameter Location
[sr-en] Specifies the folder where you want to place the new virtual machine
[sr-de] Ordner der neuen virtuellen Maschine
.Parameter Datastore
[sr-en] Specifies the datastore where you want to place the new virtual machine
[sr-de] Datastore der neuen virtuellen Maschine
.Parameter VMSwapfilePolicy
[sr-en] Specifies the swapfile placement policy
[sr-de] Swapfile Placement Policy
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$VIServer,
[Parameter(Mandatory = $true)]
[pscredential]$VICredential,
[Parameter(Mandatory = $true)]
[string]$HostName,
[Parameter(Mandatory = $true)]
[string]$VMName,
[string]$Notes,
[int32]$Cpus = 1,
[int32]$CoresPerSocket = 1,
[decimal]$MemoryGB,
[decimal]$DiskGB,
[ValidateSet("Thin", "Thick","EagerZeroedThick")]
[string]$DiskStorageFormat = "Thick",
[switch]$Floppy,
[switch]$CD,
[string]$Network,
[ValidateSet("e1000","Flexible","Vmxnet","EnhancedVmxnet","Vmxnet3")]
[string]$NetworkAdapterType = "e1000",
[string]$GuestId,
[string]$OSCustomizationSpec,
[string]$HardwareVersion,
[string]$Location,
[string]$Datastore,
[ValidateSet("WithVM","Inherit","InHostDatastore")]
[string]$VMSwapfilePolicy = "Inherit"
)
Import-Module VMware.PowerCLI
try{
[string[]]$Properties = @('Name','Id','NumCpu','CoresPerSocket','Notes','GuestId','MemoryGB','VMSwapfilePolicy','ProvisionedSpaceGB','Folder')
if([System.String]::IsNullOrWhiteSpace($Notes) -eq $true){
$Notes = " "
}
$Script:vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
$vmHost = Get-VMHost -Server $Script:vmServer -Name $HostName -ErrorAction Stop
[hashtable]$cmdArgs = @{'ErrorAction' = 'Stop'
'Server' = $Script:vmServer
'Name' = $VMName
'VMHost' = $vmHost
'Notes' = $Notes
'Confirm' = $false
'NumCpu' = $Cpus
'CoresPerSocket' = $CoresPerSocket
'MemoryGB' = $MemoryGB
'DiskGB' = $DiskGB
'Floppy' = $Floppy
'CD' = $CD
'DiskStorageFormat' = $DiskStorageFormat
'VMSwapfilePolicy' = $VMSwapfilePolicy
}
if([System.String]::IsNullOrEmpty($Datastore) -eq $false){
$store = Get-Datastore -Server $Script:vmServer -Name $Datastore -ErrorAction Stop
$cmdArgs.Add('Datastore' ,$store)
}
if([System.String]::IsNullOrEmpty($Location) -eq $false){
$folder = Get-Folder -Server $Script:vmServer -Name $Location -ErrorAction Stop
$cmdArgs.Add('Location' ,$Folder)
}
if($PSBoundParameters.ContainsKey('OSCustomizationSpec') -eq $true){
$spec = Get-OSCustomizationSpec -Name $OSCustomizationSpec -Server $Script:vmServer
$cmdArgs.Add('OSCustomizationSpec' ,$spec)
}
$Script:machine = New-VM @cmdArgs
if($PSBoundParameters.ContainsKey('GuestId') -eq $true){
$null = Set-VM -Server $Script:vmServer -VM $Script:machine -GuestId $GuestId -Confirm:$False -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('HardwareVersion') -eq $true){
$null = Set-VM -Server $Script:vmServer -VM $Script:machine -HardwareVersion $HardwareVersion -Confirm:$False -ErrorAction Stop
}
if($PSBoundParameters.ContainsKey('Network') -eq $true){
$adapter = Get-NetworkAdapter -Server $Script:vmServer -VM $Script:machine
$null = Set-NetworkAdapter -NetworkName $Network -NetworkAdapter $adapter -Type $NetworkAdapterType -Confirm:$false -ErrorAction Stop
}
$result = Get-VM -Server $Script:vmServer -Name $VMName | Select-Object $Properties
if($SRXEnv) {
$SRXEnv.ResultMessage = $result
}
else{
Write-Output $result
}
}
catch{
throw
}
finally{
if($null -ne $Script:vmServer){
Disconnect-VIServer -Server $Script:vmServer -Force -Confirm:$false
}
}