/
New-CisVMPractice.ps1
457 lines (380 loc) · 17.3 KB
/
New-CisVMPractice.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#Requires -Version 3
Function New-CisVMPractice {
<#
.SYNOPSIS
Connects to vCenter 6.5 and creates a Virtual Machine using the vSphere Automation SDK API.
.DESCRIPTION
Connects to vCenter 6.5 and creates a Virtual Machine using the vSphere Automation SDK API.
The VM is built with no hard disk or network adapter. We leave that for you to learn.
This script is intended for those that want a basic framework to practice with the
SDK API from PowerCLI. This script builds on Example #2 from 'help Get-CisService -examples'.
This cmdlet makes two connections to your vCenter 6.5 Server:
1. Connect-VIServer (Typical PowerCLI connection to VC like normal)
2. Connect-CisServer (Connection to the same VC)
If you are already connected, we use the existing sessions. Upon script exit, we leave your
pre-exisiting sessions connected. We disconnect any sessions that we create at runtime.
.NOTES
Script: New-CisVMPractice
Author: Mike Nisk
tested on: PowerShell 5.1
Tested on: PowerCLI 6.5.1
Requires: VMware vCenter Server 6.5 or later
.EXAMPLE
$credsVC = Get-Credential
New-CisVMPractice -Computer vcva02.lab.local -Credential $credsVC -Name TestVM100 -GuestId 'DARWIN_10_64' -Datastore 'vsanDatastore' -Verbose
#This example saves a credential to a variable.
#Then, we create a VM on the desired datastore name.
.EXAMPLE
$credfile = 'C:\Creds\CredsVC1.enc.xml'
New-CisVMPractice -Computer vcva02.lab.local -PathBasedCredential $credfile -Name TestVM200 -Datastore 'vsanDatastore' -Verbose
#In this example we point to a saved credential file from disk.
#Since the GuestId parameter is not populated, the default 'WINDOWS_7_64' is used.
.EXAMPLE
$vc = 'vcva02.lab.local'
Connect-VIServer -Server $vc
Connect-CisServer -Server $vc
$ds = get-datastore 'vsanDatastore'
New-CisVMPractice -Name TestVM300 -GuestID 'DARWIN_10_64' -Datastore $ds
# In this example, we manually connect to VIServer and CisServer before starting.
# Because we are already connected to VC, the Computer parameter optional.
# Also, we can we pass the datastore as an object instead of string.
# Since no runtime credentials are provided, the script uses existing VIServer and CisServer connections.
.EXAMPLE
New-CisVMPractice -Computer vcva02.lab.local -Name TestVM400 -GuestID 'DARWIN_10_64'
# Since no the Datastore parameter was not provided, the script chooses the datastore with the most free space available.
# Remember, by default this script does not add hard disk, so only the .vmx and virtual machine folder go on the datastore (i.e. no vmdk).
# Tip: Feel free to add logic to create HDD, etc. for VMs (encouraged for API practice).
.EXAMPLE
$vc = 'vcva02.lab.local'
Connect-VIServer -Server $vc
Connect-CisServer -Server $vc
$ds = get-datastore 'vsanDatastore'
PS C:\> New-CisVMPractice -Name TestVM500 -GuestID 'DARWIN_10_64' -Datastore $ds -Verbose
VERBOSE: Starting New-CisVMPractice
VERBOSE: Using provided datastore object vsanDatastore
VERBOSE: 6/9/2017 3:02:00 PM Get-CisService Started execution
VERBOSE: 6/9/2017 3:02:00 PM Get-CisService Finished execution
VERBOSE: Successfully retrieved service for VM management
VERBOSE: 6/9/2017 3:02:00 PM Get-VMHost Started execution
VERBOSE: 6/9/2017 3:02:00 PM Get-VMHost Finished execution
VERBOSE: Using random host esx04.lab.local with access to vsanDatastore
VERBOSE: 6/9/2017 3:02:00 PM Get-Folder Started execution
VERBOSE: 6/9/2017 3:02:00 PM Get-Folder Finished execution
VERBOSE: ..Attempting to deploy virtual machine TestVM500
VERBOSE: Successfully deployed virtual machine TestVM500 with Cis Identifier of vm-773
VERBOSE: Performing session cleanup
VERBOSE: Initial VIServer Connection state was Connected
VERBOSE: Existing connection to vcva02.lab.local will remain
VERBOSE: Initial CisServer Connection state was Connected
VERBOSE: Existing connection to vcva02.lab.local will remain
VERBOSE: Ending New-CisVMPractice
Value
-----
vm-773
.EXAMPLE
PS C:\> Get-Item Function:\New-CisVMPractice | Remove-Item -Force
PS C:\> . C:\temp\New-CisVMPractice.ps1
PS C:\> New-CisVMPractice -Name TestVM600
Value
-----
vm-775
#This example removed the function from memory and then reloaded it by dot sourcing it.
#You might do this if you made changes to the script.
#Then, we deployed a VM taking all defaults.
.INPUTS
.OUTPUTS
VMware.VimAutomation.Cis.Core.Types.V1.ID
#>
[CmdletBinding(DefaultParameterSetName='Default')]
Param(
#String. IP Address or DNS Name of a vCenter Server to connect to (optional if already connected)
[Parameter(ParameterSetName='Default')]
[Parameter(ParameterSetName='Credential', Mandatory)]
[Parameter(ParameterSetName='PathBasedCredential', Mandatory)]
[string]$Computer,
## PSCredential. Login info for vCenter using PSCredential.
[Parameter(ParameterSetName='Credential',HelpMessage='PSCredential for vCenter Login. Only mandatory in the Credential parameterset', Mandatory)]
[pscredential]$Credential,
## String. Path to an encryted xml credential file. Optionally use this instead of providing PSCredential directly.
[Parameter(ParameterSetName='PathBasedCredential',HelpMessage='Path to an encrypted xml credential file with ability to login to vCenter Server. Only mandatory in the PathBasedCredential parameterset', Mandatory)]
[ValidateScript({Test-Path -Path $_})]
[string]$PathBasedCredential,
#String. Name for new virtual machine
[Parameter(Mandatory,HelpMessage='Specifies a name for the new virtual machine')]
[Parameter(ParameterSetName='Default', Mandatory)]
[Parameter(ParameterSetName='Credential')]
[Parameter(ParameterSetName='PathBasedCredential')]
[ValidateNotNullOrEmpty()]
[string]$Name,
#String. Type of guest operating system to deploy (i.e. 'DARWIN_10_64'). The default is 'WINDOWS_7_64'.
[Parameter(ParameterSetName='Default')]
[Parameter(ParameterSetName='Credential')]
[Parameter(ParameterSetName='PathBasedCredential')]
[string]$GuestId,
#String or datastore object. Datastore on which to register the VM.
[Parameter(ParameterSetName='Default')]
[Parameter(ParameterSetName='Credential')]
[Parameter(ParameterSetName='PathBasedCredential')]
[PSObject]$Datastore
)
Begin {
#Startup message
Write-Verbose -Message "Starting $($MyInvocation.Mycommand) at $(Get-Date) local time"
Write-Debug -Message "Using parameter set $($PSCmdlet.ParameterSetName)"
Write-Debug -Message ($PSBoundParameters | Out-String)
} #End Begin
Process {
#region credential from file (optional)
Function Import-PSCredential {
<#
.DESCRIPTION
Imports a PSCredential from an encrypted xml file on disk.
.NOTES
Script: Import-PSCredential.ps1
Type: Function
Author: Hal Rottenberg
Organization: vmkdaily
Updated: 05April2017
.EXAMPLE
Import-PSCredential -Path <path to cred file>
#>
[CmdletBinding()]
param (
[ValidateScript({Test-Path -Path $_})]
[string]$Path = 'credentials.enc.xml' )
Process {
if($Path) {
# Import credential file
$import = Import-Clixml -Path $Path
# Test for valid import
if(!$import.UserName -or !$import.EncryptedPassword) {
Throw 'Input is not a valid ExportedPSCredential object, exiting.'
}
$Username = $import.Username
# Decrypt the password and store as a SecureString object for safekeeping
$SecurePass = $import.EncryptedPassword | ConvertTo-SecureString
# Build the new credential object
$CredObj = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $SecurePass
Write-Output -InputObject $CredObj
}
Else {
Write-Warning -Message ('Problem importing credential from path {0}' -f $Path)
}
} #End process
} #End Function
## If using encrypted xml credential from file
If($PathBasedCredential){
try {
$Credential = Import-PSCredential -Path $PathBasedCredential -ErrorAction Stop
Write-Verbose -Message ('Using Credential of {0}' -f ($Credential.GetNetworkCredential().UserName))
}
catch {
Write-Error -Message $Error[0].exception.Message
}
} #End If
#endregion
#region connections
#If Computer parameter is not populated, use existing VIServer Name
If((-Not($Computer)) -and ($Global:DefaultVIServer.IsConnected)){
[string]$Computer = $Global:DefaultViserver | Select-Object -ExpandProperty Name
}
# Regular VC Connection
If(-Not($Global:DefaultVIServer.IsConnected)) {
[string]$InitialVIServerConState = 'NotConnected'
If($Computer){
try {
$null = Connect-VIServer -Server $Computer -Credential $Credential -ErrorAction Stop
}
catch {
Write-Error -Message $Error[0].exception.Message
Throw 'Problem connecting to VIServer!'
}
}
Else {
Write-Warning -Message 'Please connect to vCenter VIServer before running script, or populate the Computer parameter at runtime'
throw 'vCenter connection required!'
}
}
# Connect to the vSphere Automation SDK API Server Service
If((-Not($Global:DefaultCisServers)) -or (-Not($Global:DefaultCisServers.IsConnected))){
[string]$InitialCisConState = 'NotConnected'
If($Credential){
If($Computer) {
try {
$null = Connect-CisServer -Server $Computer -Credential $Credential -Verbose -ErrorAction Stop
}
catch {
Write-Error -Message $Error[0].exception.Message
Throw 'Problem connecting to vCenter CisServer!'
}
}
Else {
try {
$null = Connect-CisServer -Server $Global:DefaultVIServer -Credential $Credential -Verbose
}
catch {
Write-Error -Message $Error[0].exception.Message
Throw 'Problem connecting to vCenter CisServer!'
}
}
}
Else {
<#
If there is no runtime credential, and the CisServer is not connected, the user will be
prompted for both user name and password (unless they are a credential store user).
#>
Write-Warning -Message 'Login for vCenter CisServer required!'
try {
$null = Connect-CisServer -Server ($Global:DefaultVIServer | Select-Object -ExpandProperty Name) -User ($global:DefaultVIServer | Select-Object -ExpandProperty User)
}
catch {
Write-Error -Message $Error[0].exception.Message
throw 'vCenter CisServer login required!'
}
} #End Else
} #End If
#Confirm VIServer and CisServer names match
If(($Global:DefaultVIServer | Select-Object -ExpandProperty Name) -notmatch ($Global:DefaultCisServers | Select-Object -ExpandProperty Name)){
Throw 'VIServer and CisServer are different!'
}
#endregion
#region manual settings
<#
Here we add things like datastore to use and VMHost to choose, etc.
You may consider replacing these with the api.
replace using api instead of cmdlets (optional).
#>
If($Datastore) {
If($Datastore -is [string]){
try{
$dsImpl = Get-Datastore -Name $Datastore -ErrorAction Stop
Write-Verbose -Message "Using datastore of $($dsImpl)"
}
catch {
Write-Error -Message $Error[0].exception.Message
throw 'Cannot enumerate datastore from provided string!'
}
}
Else{
if($Datastore -is [VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.StorageResource]){
$dsImpl = $Datastore
Write-Verbose -Message "Using provided datastore object $($dsImpl)"
}
}
}
Else {
try {
$ds = Get-Datastore -ErrorAction Stop
$dsImpl = ($ds | Sort-Object -Property $_.FreeSpaceGB -Descending)[0]
Write-Verbose -Message "Using first enumerable datastore $($dsImpl)"
}
catch {
Write-Error -Message $Error[0].exception.Message
throw 'Unable to enumerate datastore!'
}
}
## Choose VMHost
If($dsImpl) {
$EsxImpl = $dsImpl | Get-VMHost | Where-Object { $_.ConnectionState -eq 'Connected'} | Get-Random
Write-Verbose -Message "Using random host $($EsxImpl) with access to $($dsImpl)"
}
Else {
$EsxImpl = (Get-VMHost)[0]
Write-Verbose -Message "Using first enumerated host $($EsxImpl)"
}
#endregion
#region guest os ID
#Set default selection for GuestId in case user does not populate
If(-Not($GuestId)) {
[string]$GuestId = 'WINDOWS_7_64'
Write-Verbose -Message "No GOS type provided, using script default of $($GuestId)"
}
#endregion
#region api
# Get the service for VM management
try {
$vmService = Get-CisService -Name 'com.vmware.vcenter.VM' -ErrorAction Stop
Write-Verbose -Message 'Successfully retrieved service for VM management'
}
catch {
Write-Warning -Message 'Problem retrieving the service for VM management'
Write-Error -Message $Error[0].exception.Message
}
# Create a VM creation specification
try {
$createSpec = $vmService.Help.create.spec.CreateExample()
}
catch {
Write-Error -Message $Error[0].exception.Message
}
# Fill in the creation details
$createSpec.name = $Name
$createSpec.memory.size_MiB = 2048
$createSpec.guest_os = $GuestId
$createSpec.placement.folder = (Get-Folder -Name 'vm').ExtensionData.MoRef.Value
$createSpec.placement.host = $EsxImpl.ExtensionData.MoRef.Value
$createSpec.placement.datastore = $DsImpl.ExtensionData.MoRef.Value
$createSpec.placement.cluster = $null
$createSpec.placement.resource_pool = $null
#endregion api
#region private function
Function Invoke-DeployVmSpec {
<#
.DESCRIPTION
Quick function to deploy VM with SDK API.
#>
[CmdletBinding()]
Param()
Process {
# Call the create method passing the specification
try {
Write-Verbose -Message "..Attempting to deploy virtual machine $($Name)"
[VMware.VimAutomation.Cis.Core.Types.V1.ID]$NewCisVmId = $vmService.create( $createSpec )
}
catch {
Write-Warning -Message 'Problem calling create method to deploy VM!'
Write-Error -Message $Error[0].exception.Message
}
If($NewCisVmId) {
Write-Verbose -Message "Successfully deployed virtual machine $($Name) with Cis Identifier of $($NewCisVmId)"
return $NewCisVmId
} #End If
} #End Process
} #End Function
#endregion
#region deploy virtual machine
try{
$DeployVM = Invoke-DeployVmSpec -ErrorAction Continue
}
catch {
Write-Error -Message $Error[0].exception.Message
} #End Catch
#endregion
#region results
If($DeployVM -is [VMware.VimAutomation.Cis.Core.Types.V1.ID]) {
return $DeployVM
}
#endregion
} #End Process
End {
#Disconnect the VIServer runtime connection, if needed
Write-Verbose -Message 'Performing session cleanup'
If($InitialVIServerConState -eq 'NotConnected'){
$null = Disconnect-VIServer -Server $Computer -Confirm:$false -Force
}
Else {
Write-Verbose -Message 'Initial VIServer Connection state was Connected'
Write-Verbose -Message ('Existing connection to {0} will remain' -f ($Global:DefaultVIServer).Name)
}
#Disconnect the CisServer runtime connection, if needed
If($InitialCisConState -eq 'NotConnected'){
$null = Disconnect-CisServer -Server $Computer -Confirm:$false -Force
}
Else {
Write-Verbose -Message 'Initial CisServer Connection state was Connected'
Write-Verbose -Message ('Existing connection to {0} will remain' -f ($Global:DefaultCisServers).Name)
}
#Completion message
Write-Verbose -Message "Ending $($MyInvocation.Mycommand) at $(Get-Date) local time"
} #End End
} #End Function