-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCOrchestratorManagement.psm1
647 lines (635 loc) · 24.9 KB
/
SCOrchestratorManagement.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
Function Get-scoWebFeed
{
<#
.SYNOPSIS
Get Orchestrator web feed
.DESCRIPTION
This function does the work of getting the requested URL from Orchestrator
.PARAMETER scoUri
This is a properly formatted Orchestrator URI, the main functions build
these and pass them here for processing
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Get-scoWebFeed -scoUri http://orch.company.com:81/Orchestrator2012/Orchestrator.svc/Runbooks
Description
-----------
This isn't really intended to be used directly, but this would return an xml document
of all the runbooks on the server
.NOTES
FunctionName : Get-scoWebFeed
Created by : jspatton
Date Coded : 06/05/2014 09:00:48
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoWebFeed
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$scoUri = $null,
[string]$Filter,
[switch]$Paging,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Debug "Create System.Net.WebClient object";
[System.Net.WebClient]$WebClient = New-Object System.Net.WebClient;
if ($Credential -eq $null)
{
Write-Verbose "Using default logged in credentials";
$WebClient.UseDefaultCredentials = $true;
}
else
{
Write-Verbose "Sending credentials to server";
$WebClient.Credentials = $Credential;
}
Write-Debug "Downloading the byte data from the server";
if ($Filter)
{
[byte[]]$WebResponse = $WebClient.DownloadData("$($scoUri)$($Filter)");
}
else
{
[byte[]]$WebResponse = $WebClient.DownloadData($scoUri);
}
Write-Debug "Using System.Text.Encoding to get the string and storing it in a System.Xml.XmlDocument";
[System.Xml.XmlDocument]$WebFeeds = [System.Text.Encoding]::ASCII.GetString($WebResponse);
Write-Verbose "Return just the feed element from the server";
if ($Paging)
{
Write-Verbose "Paginate to see if there are more than 50 runbooks";
$Entries = @();
$Entries += $WebFeeds.feed.entry;
Write-Verbose "Add first 50 runbooks to return collection";
# ?`$skip=50&`$top=50"
$Skip = 50;
do
{
Write-Verbose "Get the next 50 runbooks";
$Paginate = "?`$skip=$($Skip)&`$top=50";
[byte[]]$WebResponse = $WebClient.DownloadData("$($scoUri)/$($Paginate)");
[System.Xml.XmlDocument]$WebFeeds = [System.Text.Encoding]::ASCII.GetString($WebResponse);
$Entries += $WebFeeds.feed.entry;
$Skip += 50;
}
until ($WebFeeds.feed.entry.count -lt 50)
Return $Entries
}
else
{
Return $WebFeeds.feed.entry;
}
}
End
{
}
}
Function Get-scoRunbook
{
<#
.SYNOPSIS
Get one or more Runbooks from Orchestrator
.DESCRIPTION
This function will return one or more Runbooks from the Orchestrator server. To
return a specific Runbook from the server use the Title parameter to pass in the
complete title, or a portion of the title.
.PARAMETER ManagementServer
This is the name of the Orchestrator Management server. This server has the
web service installed and is where the processing takes place.
.PARAMETER Title
If you know the title or portion of the title of a Runbook, you can
enter it here to filter the list of Runbooks returned.
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Get-scoRunbook -ManagementServer orch.company.com
Description
-----------
This example would return all the Runbooks available from the Orchestrator server
.EXAMPLE
Get-scoRunbook -ManagementServer orch.company.com -Title "New Computer"
Description
-----------
This example would return one or more Runbooks that have the phrase, 'New Computer'
in the title.
.NOTES
FunctionName : Get-scoRunbook
Created by : jspatton
Date Coded : 06/05/2014 08:52:03
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoRunbook
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$ManagementServer = $null,
[string]$Title = $null,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Verbose "Build the url string to pass to Get-scoWebfeed";
[string]$WebServiceUrl = "http://$($ManagementServer):81/Orchestrator2012/Orchestrator.svc/Runbooks";
Write-Debug "Store the response for processing";
$Runbooks = Get-scoWebFeed -scoUri $WebServiceUrl -Credential $Credential;
}
End
{
if ($Title)
{
Write-Verbose "Filter out result based on the Title, and return the entry element";
Return $Runbooks |Where-Object {$_.title.InnerText -like "*$($Title)"};
}
else
{
Write-Verbose "Return the entry element";
Return $Runbooks;
}
}
}
Function Get-scoJob
{
<#
.SYNOPSIS
Get one or more Jobs from Orchestrator
.DESCRIPTION
This function will return one or more Jobs from the Orchestrator server. To
return a specific Job from the server use the Id parameter to pass in the
Id of the job.
.PARAMETER ManagementServer
This is the name of the Orchestrator Management server. This server has the
web service installed and is where the processing takes place.
.PARAMETER Title
If you know the title or portion of the title of a Runbook, you can
enter it here to filter the list of Runbooks returned.
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Get-scoJob -ManagementServer orch.company.com
Description
-----------
This example would return all the Jobs available from the Orchestrator server
.EXAMPLE
Get-scoJob -ManagementServer orch.company.com -Id "4112bd1f-1700-4a44-b487-bcf3fc85f1a7"
Description
-----------
This example would return the Job that had '4112bd1f-1700-4a44-b487-bcf3fc85f1a7' as
the Id.
.NOTES
FunctionName : Get-scoJob
Created by : jspatton
Date Coded : 06/05/2014 08:52:03
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoJob
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$ManagementServer = $null,
[string]$Id = $null,
[ValidateSet("Canceled", "Completed", "Running", "Pending")]
[string]$Status,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Verbose "Build the url string to pass to Get-scoWebfeed";
[string]$WebServiceUrl = "http://$($ManagementServer):81/Orchestrator2012/Orchestrator.svc/Jobs";
Write-Debug "Store the response for processing";
$Filter = "`$filter=Status eq '$($Status)'"
$Jobs = Get-scoWebFeed -scoUri "$($WebServiceUrl)/?$($Filter)" -Credential $Credential;
}
End
{
if ($Id)
{
Write-Verbose "Filter out result based on the Id, and return the entry element";
$Jobs |Where-Object {$_.id -like "*$($Id)*"};
}
else
{
Write-Verbose "Return the entry element";
$Jobs;
}
}
}
Function Get-scoFolder
{
<#
.SYNOPSIS
Get one or more Folders from Orchestrator
.DESCRIPTION
This function will return one or more Folders from the Orchestrator server.
.PARAMETER ManagementServer
This is the name of the Orchestrator Management server. This server has the
web service installed and is where the processing takes place.
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Get-scoFolder -ManagementServer orch.company.com
Description
-----------
This example would return all the Folders available from the Orchestrator server
.NOTES
FunctionName : Get-scoFolder
Created by : jspatton
Date Coded : 06/05/2014 08:52:03
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoFolder
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$ManagementServer = $null,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Verbose "Build the url string to pass to Get-scoWebfeed";
[string]$WebServiceUrl = "http://$($ManagementServer):81/Orchestrator2012/Orchestrator.svc/Folders";
Write-Debug "Store the response for processing";
$Folders = Get-scoWebFeed -scoUri $WebServiceUrl -Credential $Credential;
}
End
{
Write-Verbose "Return the entry element";
Return $Folders;
}
}
Function Get-scoActivity
{
<#
.SYNOPSIS
Get Activites from Orchestrator
.DESCRIPTION
This function will return the Activities from the Orchestrator server.
.PARAMETER ManagementServer
This is the name of the Orchestrator Management server. This server has the
web service installed and is where the processing takes place.
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Get-scoActivity -ManagementServer orch.company.com
Description
-----------
This example would return all the Activities available from the Orchestrator server
.NOTES
FunctionName : Get-scoActivity
Created by : jspatton
Date Coded : 06/05/2014 08:52:03
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoActivity
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$ManagementServer = $null,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Verbose "Build the url string to pass to Get-scoWebfeed";
[string]$WebServiceUrl = "http://$($ManagementServer):81/Orchestrator2012/Orchestrator.svc/Activities";
Write-Debug "Store the response for processing";
$Activities = Get-scoWebFeed -scoUri $WebServiceUrl -Credential $Credential;
}
End
{
Write-Verbose "Return the entry element";
Return $Activities;
}
}
Function Get-scoStatistics
{
<#
.SYNOPSIS
Get Statistics from Orchestrator
.DESCRIPTION
This function will return the Statistics from the Orchestrator server.
.PARAMETER ManagementServer
This is the name of the Orchestrator Management server. This server has the
web service installed and is where the processing takes place.
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Get-scoStatistics -ManagementServer orch.company.com
Description
-----------
This example would return all the Statistics from the Orchestrator server
.NOTES
FunctionName : Get-scoStatistics
Created by : jspatton
Date Coded : 06/05/2014 08:52:03
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoStatistics
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$ManagementServer = $null,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Verbose "Build the url string to pass to Get-scoWebfeed";
[string]$WebServiceUrl = "http://$($ManagementServer):81/Orchestrator2012/Orchestrator.svc/Statistics";
Write-Debug "Store the response for processing";
$Statistics = Get-scoWebFeed -scoUri $WebServiceUrl -Credential $Credential;
}
End
{
Write-Verbose "Return the entry element";
Return $Statistics;
}
}
Function Get-scoParameter
{
<#
.SYNOPSIS
Get all the Parameters for a given Runbook
.DESCRIPTION
This function will return all the Parameters that are required
for a given Runbook to work properly. This would typically be
used with the Get-scoRunbook function.
.PARAMETER RunbookId
This is the Id, as a URL from the Runbook
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
$Parameters = Get-scoParameter -RunbookId ((Get-scoRunbook -ManagementServer orch.company.com -Title 'Provision New User') `
|Select-Object -Property Id)
Description
-----------
This example shows the most common use of this function, use Get-scoRunbook to return a specific Runbook
and Select-Object to pull just the Id and pass that into the function.
.NOTES
FunctionName : Get-scoParameter
Created by : jspatton
Date Coded : 06/05/2014 08:52:03
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Get-scoParameter
#>
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true)]
[string]$RunbookId = $null,
[pscredential]$Credential = $null
)
Begin
{
}
Process
{
Write-Debug "Store the response for processing";
$Parameters = Get-scoWebFeed -scoUri "$($RunbookId)/Parameters" -Credential $Credential;
}
End
{
Write-Verbose "Return the entry element";
Return $Parameters;
}
}
Function Start-scoRunbook
{
<#
.SYNOPSIS
This function will start a Runbook
.DESCRIPTION
This function starts a runbook on the management server. It requires a runbook object
to work with. In the context of this module that is an xmlelemnt.
.PARAMETER Runbook
An XmlElement that is returned from Get-scoRunbook
.PARAMETER Value
A hash that contains the proper key field to be passed to the runbook. So if the Runbook
has two parameters Fname and Lname you need to pass in a hash like this:
@{"Fname"="John";"Lname"="Smith"}
.PARAMETER Credential
A credential object if we need to authenticate against the Orchestrator server
.EXAMPLE
Start-Runbook -Runbook (Get-scoRunbook -ManagementServer orch.company.com -Title 'Provision new user') -Value @{"Fname"="John";"Lname"="Smith"}
Description
-----------
This would be the most common use of this function. Get-scoRunbook returns a single runbook from the server
titled 'Provision new user' and we pass in values that the Runbook needs to work properly.
.NOTES
FunctionName : Start-scoRunbook
Created by : jspatton
Date Coded : 06/05/2014 10:27:15
.LINK
https://code.google.com/p/mod-posh/wiki/SCOrchestratorManagement#Start-scoRunbook
#>
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.Xml.XmlElement]$Runbook,
[hashtable]$Value = @{},
[pscredential]$Credential = $null
)
Begin
{
if ($Runbook.id.Contains('guid'))
{
Write-Debug "The GUID is embedded inside the Id url property of the runbook";
Write-Debug "Split the id property on a single tick mark, and return the second element";
Write-Verbose "Get the GUID inside the id property";
$rbid = $Runbook.id.Split("'")[1]
}
else
{
Write-Error "Invalid or missing GUID in Runbook";
break;
}
Write-Debug "Get a list of parameters for the Runbook";
$Parameters = Get-scoParameter -RunbookId $Runbook.id;
Write-Debug "Did we get an array object back, or a single item back";
#
# This should be skipped for runbooks without parameters
#
$TypeName = $Parameters.GetType().name;
switch ($TypeName)
{
"Object[]"
{
Write-Verbose "There is more than one Parameter for this Runbook";
if ($Parameters.Count -eq $Value.Count)
{
foreach ($Parameter in $Parameters)
{
Write-Debug "The GUID is embedded inside the Id url property of the parameter";
Write-Debug "Split the id property on a single tick mark, and return the second element";
Write-Verbose "Get the GUID inside the id property";
$ParamId = $Parameter.id.Split("'")[1];
foreach ($v in $Value.GetEnumerator())
{
Write-Debug "Build a hash table with Parameter Id and Value";
if ($Parameter.title.'#text' -eq $v.Key)
{
$rbParameters += @{$ParamId = $v.Value};
}
}
}
}
else
{
Write-Error "Number of Parameters ($($Parameters.count)) do not match the number of Values ($($Value.Count))";
break;
}
}
"XmlElement"
{
Write-Debug "We only have one Parameter, make sure we only have one Value";
if ($Value.Count -eq 1)
{
Write-Debug "The GUID is embedded inside the Id url property of the parameter";
Write-Debug "Split the id property on a single tick mark, and return the second element";
Write-Verbose "Get the GUID inside the id property";
$ParamId = $Parameters.id.Split("'")[1];
$rbParameters = @{$ParamId = ($Value.GetEnumerator()| Select-Object -ExpandProperty Value)};
}
else
{
Write-Error "Number of Parameters ($($Parameters.count)) do not match the number of Values ($($Value.Count))";
break;
}
}
}
}
Process
{
Write-Debug "Build a System.Uri object from the Runnbook Id";
[System.Uri]$webUri = New-Object System.Uri($Runbook.id);
Write-Debug "Use the System.Uri object to build the Orchestrator url";
Write-Debug "Store the response in a System.Net.HttpWebRequest object";
$request = [System.Net.HttpWebRequest]::Create($webUri.Scheme + "://" + $webUri.Host + ":" + $webUri.Port + "/Orchestrator2012/Orchestrator.svc/Jobs");
if ($Credential -eq $null)
{
Write-Verbose "Using default credentials";
$request.UseDefaultCredentials = $true
}
else
{
Write-Verbose "Use specific credentials to authenticate";
$request.Credentials = $Credential
}
Write-Verbose "Build all the headers";
Write-Debug "Method = POST";
$request.Method = "POST";
Write-Debug "UserAgent = Powershell Host Name";
$request.UserAgent = $Host.Name;
Write-Debug "Accept = application/atom+xml,application/xml";
$request.Accept = "application/atom+xml,application/xml";
Write-Debug "Contenttype = application/atom+xml";
$request.ContentType = "application/atom+xml";
Write-Debug "KeepAlive = true";
$request.KeepAlive = $true;
Write-Debug "Accept-Encoding = identity";
$request.Headers.Add("Accept-Encoding","identity");
Write-Debug "Accept-Language = en-us";
$request.Headers.Add("Accept-Language","en-US");
Write-Debug "DataServiceVersion = 1.0;Netfx";
$request.Headers.Add("DataServiceVersion","1.0;NetFx");
Write-Debug "MaxDataServiceVersion = 2.0;NetFx";
$request.Headers.Add("MaxDataServiceVersion","2.0;NetFx");
Write-Debug "Pragma = no-cache";
$request.Headers.Add("Pragma","no-cache");
Write-Debug "Build the parameter string";
#
# This should be skipped for runbooks without parameters
#
$rbParamString = "";
if ($rbParameters -ne $null)
{
Write-Verbose "Create the parameters";
Write-Debug "Begin the opening xml for the parameters";
$rbParamString = "<d:Parameters><![CDATA[<Data>";
foreach ($p in $rbParameters.GetEnumerator())
{
Write-Debug "For each entry in the hashtable, add the key (ID) and value (DATA)";
$rbParamString = -join ($rbParamString,"<Parameter><ID>{",$p.key,"}</ID><Value>",$p.value,"</Value></Parameter>");
}
Write-Debug "Close the parameter xml";
$rbParamString += "</Data>]]></d:Parameters>";
}
Write-Verbose "Build the request body";
Write-Debug "The request body has to have certain items inside, as well as be ordered in a specific way";
$requestBody = "<?xml version=`"1.0`" encoding=`"utf-8`" standalone=`"yes`"?>";
$requestBody += "<entry xmlns:d=`"http://schemas.microsoft.com/ado/2007/08/dataservices`" xmlns:m=`"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata`" xmlns=`"http://www.w3.org/2005/Atom`">";
$requestBody += "<content type=`"application/xml`">";
$requestBody += "<m:properties>";
$requestBody += "<d:RunbookId m:type=`"Edm.Guid`">$($rbid)</d:RunbookId>";
#
# This should be skipped for runbooks without parameters
#
$requestBody += $rbparamstring;
$requestBody += "</m:properties>";
$requestBody += "</content>";
$requestBody += "</entry>";
Write-Debug "Create a System.IO.StreamWriter object to receive the stream from the server";
$requestStream = new-object System.IO.StreamWriter $Request.GetRequestStream();
Write-Debug "Upload the xml to the server";
Write-Verbose "Sending data to server";
$requestStream.Write($RequestBody);
Write-Debug "Flush the stream";
$requestStream.Flush();
Write-Debug "Close the stream";
$requestStream.Close();
Write-Debug "Get the response code from the server";
$response = $Request.GetResponse();
Write-Debug "Get the response stream";
$responseStream = $Response.GetResponseStream();
Write-Debug "Build a System.IO.StreamReader object to read in the stream";
$readStream = new-object System.IO.StreamReader $responseStream;
Write-Debug "Read the stream returned from the server";
Write-Verbose "Get data from the server";
$responseString = $readStream.ReadToEnd();
Write-Debug "Close the StreamReader";
$readStream.Close();
Write-Debug "Close the response stream";
$responseStream.Close()
}
End
{
if ($response.StatusCode -eq 'Created')
{
Write-Debug "Convert the xml string to an XML object";
$xmlDoc = [xml]$responseString;
Write-Debug "Get the Job ID from the running job";
$jobId = $xmlDoc.entry.content.properties.Id.InnerText;
Write-Host "Successfully started runbook. Job ID: " $jobId;
Write-Verbose "Return the entry element";
return $xmlDoc.entry;
}
else
{
Write-Host "Could not start runbook. Status: " $response.StatusCode;
}
}
}