-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathICSPPS.psm1
670 lines (560 loc) · 20 KB
/
ICSPPS.psm1
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
function Invoke-I2ICSPAPIRequest{
[CmdletBinding()]
param(
$ICSPServer,
[string]
$Path,
[string]
$Protocol = "https",
[int]
$Port = 443,
[Parameter(Mandatory=$false)]
[ValidateSet("GET","POST","PUT")]
[string]
$Method = "GET",
$Resource,
$Body,
$Query,
$SessionKey = $Global:icsp_sessionkey
)
if($Resource -notlike "/rest/*"){
$Resource = "/rest/" + $Resource
}
$Path += $Resource
if(!$ICSPServer){
$ICSPServer = $global:icsp_server
}
Write-Verbose "ICSP Server : $ICSPServer , GLOBAL ICSP Server : $($global:icsp_server)"
$URIBuilder = New-Object System.UriBuilder -ArgumentList @($Protocol, $ICSPServer, $Port, $Path)
$Uribuilder.Query = $query
if($SessionKey){
Write-Verbose "Building Header with session key $sessionkey"
$headers = @{}
$headers["Auth"] = $sessionkey
$headers["X-Api-Version"] = "200"
}
if($Method -eq "GET"){
Write-Verbose "Method $method against URI $($UriBuilder.Uri)"
Invoke-RestMethod -Method $Method -Uri $URIBuilder.Uri -Body $Body -Headers $headers -ContentType "application/json" #-Verbose
}
else{
Write-Verbose "Method $method against URI $($UriBuilder.Uri)"
Invoke-RestMethod -Method $Method -Uri $URIBuilder.Uri -Body $Body -Headers $headers -ContentType "application/json" #-Verbose
}
}
function New-I2ICSPSessionKey{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
$ICSPServer,
[Parameter(Mandatory=$true, ValueFromPipeline=$false)]
$Username,
[Parameter(Mandatory=$true, ValueFromPipeline=$false)]
$Password,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
$LoginDomain)
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$decryptPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$body = @"
{
"authLoginDomain":"$LoginDomain",
"userName":"$username",
"password":"$decryptPassword"
}
"@
Write-Verbose $body
$response = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Method Post -Resource "login-sessions" -Body $body -Verbose
$sessionkey = $response.sessionid
$global:icsp_sessionkey = $sessionkey
$global:icsp_server = $ICSPServer
}
function Connect-I2ICSP{
<#
.SYNOPSIS
Creates a connection to a ICSP server
.DESCRIPTION
Connects to the specified HPE Insight Control Server Provisioning server and creates a session key for later use
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 28/08-2016
Version : 0.9.1.0
Revised : 05/10-2016
.PARAMETER ICSPServer
The ICSP server to connect to
.PARAMETER Username
Username for the connection
.PARAMETER Password
Password for the given user
.PARAMETER Directory
Directory for the connection, local or Active Directory domain
.EXAMPLE
Connect-I2ICSP -ICSPServer icserver001 -Username user01 -Password (Read-Host -AsSecurestring)
Connects to the OVSERVER001 Oneview server with the given username and password
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
$ICSPServer,
[Parameter(Mandatory=$true, ValueFromPipeline=$false, HelpMessage="Please provide username")]
$Username,
[Parameter(Mandatory=$true, ValueFromPipeline=$false, HelpMessage="Please provide password")]
[SecureString]
$Password,
[Parameter(Mandatory=$false, ValueFromPipeline=$false)]
$Directory
)
New-I2ICSPSessionKey -ICSPServer $ICSPServer -Username $Username -Password $Password -LoginDomain $Directory -Verbose
}
function Get-I2ICSPServer{
<#
.SYNOPSIS
Gets a server from ICSP
.DESCRIPTION
Connects to the specified HPE Insight Control Server Provisioning server and searches for one or more servers
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 28/08-2016
Version : 0.9.2
Revised : 03/01-2017
.PARAMETER ICSPServer
ICSP Server to retrieve servers from
.PARAMETER Serial
Serial number of the server to search for
.PARAMETER ILOIp
ILO IP address of the server to search for
.EXAMPLE
Get-I2ICSPServer
Lists all servers from the connected ICSP server
.EXAMPLE
Get-I2ICSPServer -Serial ABCDE12345
Searchs for a server with the given serial number on the connected ICSP server
#>
[CmdletBinding(DefaultParameterSetName="none")]
param(
[Parameter(Mandatory=$false,ParameterSetName="default",ValueFromPipeline=$true)]
$ICSPServer,
[Parameter(Mandatory=$false,ParameterSetName="serial")]
$Serial,
[Parameter(Mandatory=$false,ParameterSetName="ilo")]
$ILOIp
)
Write-Verbose "Invoking REST request"
if($Serial){
$SrcResource = "index/resources"
$SrcQuery = "category=osdserver&query=osdServerSerialNumber=" + $serial
$search = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Resource $SrcResource -Query $SrcQuery
if($search){
Write-Verbose "Found $($search.members.count) results"
if($search.members.count -gt 1){
throw "Multiple results found"
}
$result = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Resource $search.members.uri -Verbose
$result
}
}
else{
$Resource = "os-deployment-servers"
Write-Verbose "Pulling all servers"
$response = Invoke-I2ICSPAPIRequest -Resource $Resource -Verbose
if($ILOIp){
Write-Verbose "Filtering on iLO IP"
$results = $response.members #| where {$_.peerIP -eq $Server -or $_.serialnumber -eq $Server}
foreach($res in $results){
$result = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Resource $res.uri
if($result.ilo[0].ipAddress -eq $ILOIp){
$result
}
}
}
else{
$result = $response.members
$result
}
}
}
function New-I2ICSPServer{
<#
.SYNOPSIS
Creates a server in ICSP
.DESCRIPTION
Creates a new server resource on the connected ICSP server
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 28/08-2016
Version : 0.9.2
Revised : 03/01-2017
.PARAMETER ICSPServer
ICSP Server to retrieve servers from
.PARAMETER ILOIp
ILO IP address of the server to create
.PARAMETER ILOPort
ILO Port of the server to create
.PARAMETER ILOUser
ILO User with admin credentials on the server to create
.PARAMETER ILOPassword
Password for the ILO User account
.EXAMPLE
New-I2ICSPServer -ILOIp 1.1.1.1 -ILOUser admin01
Prompts for the password of user admin01 and creates a resource on the connected ICSP server
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false,ParameterSetName="default",ValueFromPipeline=$true)]
$ICSPServer,
[Parameter(Mandatory=$true)]
$ILOIP,
[Parameter(Mandatory=$false)]
$ILOPort = 443,
[Parameter(Mandatory=$true)]
$ILOUser,
[Parameter(Mandatory=$true)]
[SecureString]
$ILOPassword
)
$decryptPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($ILOPassword))
$body = @{}
$body["ipAddress"] = $ILOIP
$body["port"] = $ILOPort
$body["username"] = $ILOUser
$body["password"] = $decryptPassword
$body = $body | ConvertTo-Json
Write-Verbose "Body $body"
Write-Verbose "Invoking REST request"
$response = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Method POST -Resource "os-deployment-servers" -Body $body #-Verbose
$response
}
function Get-I2ICSPJob{
<#
.SYNOPSIS
Gets a job from ICSP
.DESCRIPTION
Connects to the specified HPE Insight Control Server Provisioning server and searches for one or more jobs
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 26/09-2016
Version : 0.9.2
Revised : 03/01-2017
.PARAMETER Server
ICSP Server to retrieve jobs from
.PARAMETER Job
Job to search for
.EXAMPLE
Get-I2ICSPJob
Lists all jobs on the connected ICSP server
.EXAMPLE
Get-I2ICSPJob -Job 123456
Gets the job with the specified Job ID
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
$Job,
[Parameter(Mandatory=$false)]
$ICSPServer
)
if($Job -is [object] -and $job.category -eq "os-deployment-jobs"){
Write-Verbose "Job object received"
$Resource = $job.uri
}
elseif($Job -is [pscustomobject] -and $job.uri -ne $null){
$Resource = $job.uri
}
elseif($Job -is [int]){
$Resource = "os-deployment-jobs/" + $job
}
else{
$Resource = "os-deployment-jobs"
}
try{
Write-Verbose "Invoking REST request"
$response = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Resource $Resource
}
catch{
}
if($response.category -eq "os-deployment-jobs"){
$response
}
else{
$response.members
}
}
function Wait-I2ICSPJobCompletion{
<#
.SYNOPSIS
Waits for a ICSP job to complete
.DESCRIPTION
Connects to the specified HPE Insight Control Server Provisioning server, searches for one or more jobs
and waits for it to complete
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 26/09-2016
Version : 0.9.2
Revised : 03/01-2017
.PARAMETER Job
Job to wait for
.EXAMPLE
Get-I2ICSPJob 123456 | Wait-I2ICSPJobCompletion
Gets the job with the specified Job ID and waits for the job to complete
.EXAMPLE
Wait-I2ICSPJobCompletion -Job 123456
Gets the job with the specified Job ID and waits for the job to complete
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[object]
$Job
)
if($job -is [object] -and $job.category -eq "os-deployment-jobs"){
Write-Verbose $job.category
$newStatus = Get-I2ICSPJob -Job $Job
}
else{
throw "Operation not allowed. Please provide a ICSP Job object"
}
$currJob = $null
while($newStatus.running -eq "true"){
if($currJobProg -ne $newStatus.jobProgress[0].currentStepName){
$currJobProg = $newStatus.jobProgress[0].currentStepName
Write-Output "$(get-date) Last update: $($newStatus.modified) : $($newStatus.nameOfJobType) - $($newStatus.jobProgress[0].currentStepName)"
}
else{
Write-Host "." -NoNewline
}
start-sleep -Seconds 5
$newStatus = Get-I2ICSPJob -Job $Job
}
Write-Verbose "Job finished"
$newStatus
}
function Get-I2ICSPBuildPlan{
<#
.SYNOPSIS
Gets a OS Buildplan from ICSP
.DESCRIPTION
Connects to the specified HPE Insight Control Server Provisioning server and searches for one or more buildplans
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 28/08-2016
Version : 0.9.2
Revised : 03/01-2017
.PARAMETER ICSPServer
ICSP Server to get plans from
.PARAMETER BuildPlan
Serial number of the server to search for
.PARAMETER Name
ILO IP address of the server to search for
.PARAMETER Os
ILO IP address of the server to search for
.EXAMPLE
Get-I2ICSPBuildPlan
Lists all buildplans from the connected ICSP server
.EXAMPLE
Get-I2ICSPBuildPlan -Name Buildplan01
Outputs the buildplan "Buildplan01"
#>
[CmdletBinding(DefaultParameterSetName="Default")]
param(
[Parameter(Mandatory=$false,ParameterSetName="Default",ValueFromPipeline=$true)]
$ICSPServer,
[Parameter(Mandatory=$false,ParameterSetName="BuildPlan")]
[object]
$BuildPlan,
[Parameter(Mandatory=$false,ParameterSetName="Name")]
$Name,
[Parameter(Mandatory=$false,ParameterSetName="Os")]
$Os
)
if($BuildPlan){
if($builPlan -is [object] -and $BuildPlan.category -eq "os-deployment-build-plans"){
$Resource = $BuildPlan.uri
}
else{
throw "Operation not supported. Input object not a build plan object"
}
}
else{
$Resource = "os-deployment-build-plans"
}
#Write-Verbose "ICSP Server : $ICSPServer"
$response = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Resource $Resource -Verbose
if($Name){
$result = $response.members | where {$_.name -like "*" + $Name + "*"}
}
elseif($Os){
$result = $response.members | where {$_.os -like "*" + $Os + "*"}
}
else{
$result = $response.members
}
$result
}
function New-I2ICSPDeploymentJob{
<#
.SYNOPSIS
Creates a deploymentjob in ICSP
.DESCRIPTION
Connects to the specified HPE Insight Control Server Provisioning server and creates a deploymentjob
.NOTES
Info
Author : Rudi Martinsen / Intility AS
Date : 28/08-2016
Version : 0.9.2
Revised : 03/01-2017
.PARAMETER Server
ICSP Server object to create deployment job for
.PARAMETER BuildPlan
ICSP BuildPlan object to create deployment job for
.PARAMETER IP
IP address of the server
.PARAMETER SubnetPrefix
Network mask of IP subnet. If omitted a subnet prefix of 24 is used
.PARAMETER IPGateway
Gateway address of the server. If omitted the IP's .1 address is used
.PARAMETER VlanId
Vlan that the IP belongs to
.PARAMETER Hostname
Hostname of the server
.PARAMETER DNSServers
DNS servers to use for the server
.PARAMETER DNSSearch
DNS Search suffix
.EXAMPLE
New-I2ICSPBuildPlan -Server $server01 -Buildplan $plan01 -IP 10.10.10.10 -SubnetSuffix 24 -IPGateway 10.10.10.1 -VlanId 10 -Hostname SERVER01 -DNSServers "8.8.8.8","9.9.9.9"
Creates and runs a new deployment job with the given parameters
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
$ICSPServer,
[Parameter(Mandatory=$true, ValueFromPipeLine=$true)]
[object]
$Server,
[Parameter(Mandatory=$true)]
[object]
$BuildPlan,
[Parameter(Mandatory=$false)]
[string]
$IP,
[Parameter(Mandatory=$false)]
[int]
$SubnetPrefix,
[Parameter(Mandatory=$false)]
[string]
$IPGateway,
[Parameter(Mandatory=$false)]
[int]
$VlanId = 198,
[Parameter(Mandatory=$false)]
[string]
$Hostname,
[Parameter(Mandatory=$false)]
[string[]]
$DNSServers,
[Parameter(Mandatory=$false)]
[string[]]
$DNSSearch
)
if($server -is [object] -and $server.category -eq "os-deployment-servers"){
$destServer = $server
}
else{
try{
$destServer = Get-I2ICSPServer -ICSPServer $ICSPServer -Server $Server.serialNumber
}
catch{
}
}
if(!$destServer -or $destServer -is [array] -or $destServer.category -ne "os-deployment-servers"){
throw "Operation failed. Unable to retrieve a single server object"
}
if($BuildPlan -is [object] -and $BuildPlan.category -eq "os-deployment-build-plans"){
$destBuildPlan = $BuildPlan
}
else{
try{
$destBuildPlan = Get-I2ICSPBuildPlan -ICSPServer $ICSPServer -Name $BuildPlan
}
catch{
}
}
if(!$destBuildPlan -or $destBuildPlan -is [array] -or $destBuildPlan.category -ne "os-deployment-build-plans"){
throw "Operation failed. Unable to retrieve a single build plan object"
}
$Body = @{}
## This can be used to hard code default dns servers (comma-separated)
#if(!$DNSServers){
# $DNSServers = ""
#}
$dnsSearch = @($DNSSearch)
if(!$IPGateway){
$ipSplit = $ip.split(".")
$IPGateway = $ipSplit[0] + "." + $ipSplit[1] + "." + $ipSplit[2] + ".1"
}
$staticNetworks = @()
if(!$SubnetPrefix){
$staticNetworks += $IP + "/24"
}
else{
$staticNetworks += $IP + "/" + $SubnetPrefix
}
$serverdata = @()
$servers = @{}
$osbpuris = @()
$osbpuris += $destBuildPlan.uri
$Personalitydata = @{}
$Interfaces = New-Object System.Collections.Arraylist
foreach($srvInt in $server.interfaces){
$interface = New-Object PSCustomObject
$interface | Add-Member -MemberType NoteProperty -Name macAddress -Value $srvInt.macAddr
$interface | Add-Member -MemberType NoteProperty -Name dhcpv4 -Value $false
$interface | Add-Member -MemberType NoteProperty -Name ipv6Autoconfig -Value $false
$interface | Add-Member -MemberType NoteProperty -Name ipv6gateway -Value $null
if($srvInt.slot -eq "eth0"){
$interface | Add-Member -MemberType NoteProperty -Name enabled -Value $true
$interface | Add-Member -MemberType NoteProperty -Name ipv4gateway -Value $IPGateway
$interface | Add-Member -MemberType NoteProperty -Name vlanid -Value $VlanId
$interface | Add-Member -MemberType NoteProperty -Name staticNetworks -Value $staticNetworks
$interface | Add-Member -MemberType NoteProperty -Name dnsServers -Value $dnsServers #-join ","
$interface | Add-Member -MemberType NoteProperty -Name winsServers -Value $null
$interface | Add-Member -MemberType NoteProperty -Name dnsSearch -Value $dnsSearch
}
else{
}
$interfaces += $Interface
}
$Personalitydata["interfaces"] = $interfaces
$Personalitydata["virtualInterfaces"] = $null
$Personalitydata["hostname"] = $hostname
$Personalitydata["domain"] = $null
$Personalitydata["workgroup"] = $null
$servers["serverUri"] = $destServer.uri
$servers["skipReboot"] = $true
$servers["personalityData"] = $Personalitydata
$serverdata += $servers
$Body["osbpUris"] = $osbpuris
$Body["serverData"] = $serverdata
$body["failMode"] = $null
$body = ConvertTo-Json $body -Depth 6
$resource = "os-deployment-jobs"
Write-Verbose $body
$response = Invoke-I2ICSPAPIRequest -ICSPServer $ICSPServer -Method POST -Resource $resource -Body $Body
$response
}