-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebService-Functions.ps1
719 lines (599 loc) · 38.6 KB
/
WebService-Functions.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
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
############################################################
#
# WEB Service Functions
#
############################################################
Function ConvertTo-HashTable
{
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[Object]$InputObject,
[string[]]$ExcludeTypeName = @("ListDictionaryInternal","Object[]"),
[ValidateRange(1,10)][Int]$MaxDepth = 4
)
$propNames = $InputObject.psobject.Properties | Select-Object -ExpandProperty Name
$hash = @{}
$propNames | ForEach-Object {
if ($null -ne $InputObject.$_ )
{
if ($InputObject.$_ -is [string] -or (Get-Member -MemberType Properties -InputObject ($InputObject.$_) ).Count -eq 0)
{$hash.Add($_,$InputObject.$_)}
else
{
if ($InputObject.$_.GetType().Name -in $ExcludeTypeName){Write-Verbose "Skipped $_"}
elseif ($MaxDepth -gt 1){$hash.Add($_,(ConvertTo-HashTable -InputObject $InputObject.$_ -MaxDepth ($MaxDepth - 1)))}
}
}
}
$hash
}
Function Read-PostRequest($request)
{
# Get post data from the input stream.
# This function by Peter Hinchley
# https://hinchley.net/articles/create-a-web-server-using-powershell/
$length = $request.contentlength64
$buffer = new-object "byte[]" $length
[void]$request.inputstream.read($buffer, 0, $length)
$body = [system.text.encoding]::ascii.getstring($buffer)
$data = @{}
$body.split('&') | ForEach-Object{$part = $_.split('=') ; $data.add($part[0], $part[1])}
return $data
}
Function Read-GetRequest($request)
{
$data = @{}
$request.QueryString|ForEach-Object {$data.add($_ , $request.QueryString.get_Item($_))}
return $data
}
Function Get-Time
{
$Result = Get-Date|select Date, Day, DayOfWeek, DayOfYear, Hour, Kind, Millisecond, Minute, Month, Second, Ticks, TimeOfDay, Year, DateTime
Return $Result
}
Function Ping-service
{
$now = Get-Date
$Elapsed = (Get-Date $now) - (Get-Date $StartServiceTime)
$Result = New-Object PSObject
Add-Member -InputObject $Result -MemberType NoteProperty -Name Status -Value "Running"
Add-Member -InputObject $Result -MemberType NoteProperty -Name Days -Value $Elapsed.days
Add-Member -InputObject $Result -MemberType NoteProperty -Name Hours -Value $Elapsed.Hours
Add-Member -InputObject $Result -MemberType NoteProperty -Name Minutes -Value $Elapsed.Minutes
Return $Result
}
function New-ScriptBlockCallback
{
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
param(
[parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[scriptblock]$Callback
)
# Is this type already defined?
if (-not ( 'CallbackEventBridge' -as [type])) {
Add-Type @'
using System;
public sealed class CallbackEventBridge {
public event AsyncCallback CallbackComplete = delegate { };
private CallbackEventBridge() {}
private void CallbackInternal(IAsyncResult result) {
CallbackComplete(result);
}
public AsyncCallback Callback {
get { return new AsyncCallback(CallbackInternal); }
}
public static CallbackEventBridge Create() {
return new CallbackEventBridge();
}
}
'@
}
$bridge = [callbackeventbridge]::create()
Register-ObjectEvent -InputObject $bridge -EventName callbackcomplete -Action $Callback -MessageData $args > $null
$bridge.Callback
}
############################################################
#
# AD Functions
#
############################################################
Function Get-ComputerName
{
Param (
[String]$ComputerName,
[String]$SearchPath
)
If ([String]::IsNullOrWhiteSpace($ComputerName))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] No value specified for property 'ComputerName'"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 9
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
If (-not([String]::IsNullOrWhiteSpace($SearchPath)))
{$Result = Get-ADComputer -Filter {name -eq $ComputerName} -SearchBase $SearchPath -properties MemberOf}
else
{$Result = Get-ADComputer -Filter {name -eq $ComputerName} -properties MemberOf}
If ([String]::IsNullOrWhiteSpace($Result))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] There is no Computer with Name $ComputerName in AD"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 10
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
else
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $True
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "Machine $ComputerName Found in AD!!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 0
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Result.Name
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerADpath -Value $Result.DistinguishedName
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerSID -Value $Result.SID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerObjGUID -Value $Result.ObjectGUID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerMemberOf -Value $Result.MemberOf.Value
Return $ReturnObj
}
}
Function Get-GroupName
{
Param (
[String]$GroupName,
[String]$SearchPath
)
If ([String]::IsNullOrWhiteSpace($GroupName))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] No value specified for Group Name"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 9
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
If (-not([String]::IsNullOrWhiteSpace($SearchPath)))
{$Result = Get-ADGroup -filter 'Name -eq $GroupName' -SearchBase $SearchPath -Properties Members,SID}
else
{$Result = Get-ADGroup -filter 'Name -eq $GroupName' -Properties Members,SID}
If ([String]::IsNullOrWhiteSpace($Result))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] There is no Group with Name $GroupName in AD"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 18
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupName -Value $Null
Return $ReturnObj
}
else
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $True
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "Group $GroupName Found in AD!!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 0
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupName -Value $Result.Name
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupADpath -Value $Result.DistinguishedName
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupSID -Value $Result.SID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupObjGUID -Value $Result.ObjectGUID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupMembers -Value $Result.Members.Value
Return $ReturnObj
}
}
Function Get-OuName
{
Param (
[String]$OuName,
[String]$SearchPath
)
If ([String]::IsNullOrWhiteSpace($OuName))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] No value specified for OU Name"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 9
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
If (-not([String]::IsNullOrWhiteSpace($SearchPath)))
{$Result = Get-ADOrganizationalUnit -Filter 'Name -eq $OuName' -SearchBase $SearchPath -ErrorAction SilentlyContinue}
else
{$Result = Get-ADOrganizationalUnit -Filter 'Name -eq $OuName' -ErrorAction SilentlyContinue}
If ([String]::IsNullOrWhiteSpace($Result))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] There is no OU with Name $OuName"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 13
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupName -Value $Null
Return $ReturnObj
}
else
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $True
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "Ou $OuName Found !!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 0
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name OuName -Value $Result.Name
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name OuADpath -Value $Result.DistinguishedName
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name OuObjGUID -Value $Result.ObjectGUID
Return $ReturnObj
}
}
Function Add-NewComputer
{
Param (
[String]$ComputerName,
[string]$OU
)
##== Validate Computer name
If ($ComputerName.Length -gt 15)
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] Computer Name $ComputerName is more than 15 Caraters"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 11
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
$Result = Get-ComputerName -ComputerName $ComputerName
If (-not([String]::IsNullOrWhiteSpace($Result.ComputerName)))
{
$Result.Success = $False
$Result.Message = "[ERROR] Computer Name Already exists"
$Result.ErrorNumber = 12
Return $Result
}
##== Create Computername
If ([String]::IsNullOrWhiteSpace($OU))
{New-ADComputer -Name $ComputerName -SAMAccountName $ComputerName -Enabled $true}
Else
{
##== Check OU
$Result = Get-ADOrganizationalUnit -Filter 'Name -like $OU'
If (-not([String]::IsNullOrWhiteSpace($Result)))
{New-ADComputer -Name $ComputerName -SAMAccountName $ComputerName -Path $Result.DistinguishedName -Enabled $true}
else
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] Specified OU does not exists!!!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 13
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
}
##== Return Result
$Result = Get-ComputerName -ComputerName $ComputerName
$Result.Message = "Machine created successfully !!"
$Result.ErrorNumber = 0
Return $Result
}
Function Add-NewGeneratedComputer
{
Param (
[String]$Prefix,
[string]$Suffix,
[Int]$Digits,
[string]$OU
)
If (([String]::IsNullOrWhiteSpace($Prefix)) -and ([String]::IsNullOrWhiteSpace($Suffix)))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] Parameters Prefix and Suffix can not both be empty, One parameter must be submitted at least!!!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 15
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
If ([String]::IsNullOrWhiteSpace($Digits))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] Parameter digits missing !!!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 16
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $Null
Return $ReturnObj
}
##== Create Computername
If (-not ([String]::IsNullOrWhiteSpace($Prefix))) {$AdFilter = "$Prefix*" ; [Int]$TmpLength = $Prefix.Length}
If (-not ([String]::IsNullOrWhiteSpace($Suffix))) {$AdFilter = "*$Suffix" ; [Int]$TmpLength = $Suffix.Length}
##== Change digits into friendly powershell something
[String]$Digits = "{0:d$Digits}"
[String]$DigitsAsString = $Digits -f 0
$NameLength = $TmpLength + $DigitsAsString.Length
[String]$RegexRule = "^.{$NameLength,$NameLength}$"
[Int]$I = 0
Do
{
$I += 1
#$NewComputerName = (Get-ADComputer -Filter {name -like $AdFilter}).name|Select-Object -Last 1
$NewComputerName = (Get-ADComputer -properties Name -filter *| Where-object { $_.Name -like $AdFilter -and $_.Name -match $RegexRule}).name|Select-Object -Last 1
##== Manage case when this is the very first computer to create
If ([String]::IsNullOrWhiteSpace($NewComputerName)){$NewComputerName = $($AdFilter.replace("*",$DigitsAsString)) }
##== Cleanup name from strings to allow number incrementation
If (-not ([String]::IsNullOrWhiteSpace($Prefix))) {$NewComputerName = $NewComputerName.replace($Prefix,"")}
If (-not ([String]::IsNullOrWhiteSpace($Suffix))) {$NewComputerName = $NewComputerName.replace($Suffix,"")}
##== Increment number, and format with requested Digits
[int]$NewComputerName = $NewComputerName ; $NewComputerName += $I ; [String]$NewComputerName = $Digits -f $NewComputerName
##== Rebuild new name
If (-not ([String]::IsNullOrWhiteSpace($Prefix))) {$NewComputerName = $($Prefix + $NewComputerName) }
If (-not ([String]::IsNullOrWhiteSpace($Suffix))) {$NewComputerName = $($NewComputerName + $Suffix) }
}while ($null -ne ((Get-ADComputer -Filter {name -eq $NewComputerName}).name))
If ([String]::IsNullOrWhiteSpace($OU))
{$Result = Add-NewComputer -ComputerName $NewComputerName}
else
{$Result = Add-NewComputer -ComputerName $NewComputerName -OU $OU}
Return $Result
}
Function Add-ComputerToOU
{
Param (
[String]$ComputerName,
[string]$OU
)
##== Check Computer Name
$CompResult = Get-ComputerName -ComputerName $ComputerName
If ($CompResult.Success -eq $False){Return $CompResult}
##== Check OU
$OuResult = Get-OuName -OuName $OU
If ($OuResult.Success -eq $False){Return $OuResult}
##== Check If Computer is not already a member of the OU
$Result = Get-ComputerName -ComputerName $ComputerName -SearchPath $OuResult.OuADpath
If ($Result.Success -eq $True)
{$Message = "Computer $ComputerName is already a member of the OU $OU"}
Else
{
##== Move Computer to target OU
Move-ADObject -Identity $CompResult.ComputerADpath -TargetPath $OuResult.OuADpath -ErrorAction SilentlyContinue
##== Check if Move was successful !!
$Result = Get-ComputerName -ComputerName $ComputerName -SearchPath $OuResult.OuADpath
If ($Result.Success -eq $False)
{
$Result.Message = "[ERROR] Unable to move Computer $ComputerName to OU $OU"
$Result.ErrorNumber = 17
Return $Result
}
else
{$Message = "Computer $ComputerName moved successfully to OU $OU"}
}
$Result.Message = $Message
$Result.ErrorNumber = 0
Add-Member -InputObject $Result -MemberType NoteProperty -Name OuName -Value $OuResult.OuName
Add-Member -InputObject $Result -MemberType NoteProperty -Name OuAdPath -Value $OuResult.OuADpath
Add-Member -InputObject $Result -MemberType NoteProperty -Name OuGUID -Value $OuResult.OuObjGUID
Return $Result
}
Function Add-ComputerToGroup
{
Param (
[String]$ComputerName,
[string]$Group
)
##== Check Computer Name
$CompResult = Get-ComputerName -ComputerName $ComputerName
If ($CompResult.Success -eq $False){Return $CompResult}
##== Check Computer Group
$GrpResult = Get-GroupName -GroupName $Group
If ($GrpResult.Success -eq $False){Return $GrpResult}
##== Check If Computer is not already a member of the Group
If ($GrpResult.GroupMembers -contains $CompResult.ComputerADpath)
{$Message = "Computer $ComputerName is already a member of group $Group"}
else
{
##== Add computer to Group
Add-ADGroupMember $Group -Members $($ComputerName + "$")
##== Check If Computer has successfully joined the group
$GrpResult = Get-GroupName -GroupName $Group
If ($GrpResult.GroupMembers -contains $CompResult.ComputerADpath)
{
##== Check again to send back updated information
$CompResult = Get-ComputerName -ComputerName $ComputerName
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $True
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "Computer $ComputerName was successfully added to group $Group"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 0
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerName -Value $CompResult.ComputerName
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerADpath -Value $CompResult.ComputerADpath
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerSID -Value $CompResult.ComputerSID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerObjGUID -Value $CompResult.ComputerObjGUID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerMemberOf -Value $CompResult.ComputerMemberOf
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupName -Value $GrpResult.GroupName
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupADpath -Value $GrpResult.GroupADpath
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupSID -Value $GrpResult.GroupSID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupObjGUID -Value $GrpResult.GroupObjGUID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name GroupMembers -Value $GrpResult.GroupMembers
Return $ReturnObj
}
else
{
$GrpResult.Success = $False
$GrpResult.Message = "[ERROR] Unable to move Computer $ComputerName to group $group"
$GrpResult.ErrorNumber = 19
Add-Member -InputObject $GrpResult -MemberType NoteProperty -Name ComputerName -Value $ComputerName
Return $GrpResult
}
}
}
############################################################
#
# SCCM Functions
#
############################################################
Function Get-CMComputerCollection
{
Param (
[String]$Collection
)
##== INIT
If ([String]::IsNullOrWhiteSpace($Script:CMDrive)){$Script:CMDrive = Get-CMSite}
If ([String]::IsNullOrWhiteSpace($Script:CurrentLocation)){$Script:CurrentLocation = Get-Location}
If ([String]::IsNullOrWhiteSpace($Collection))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] No value specified for Collection Name"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 20
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name CollectionName -Value $Null
Return $ReturnObj
}
##==Change Location to SCCM
Set-Location $Script:CMDrive.SiteDrive
$Result = Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Query "select * from SMS_Collection Where SMS_Collection.Name='$Collection'"
If ([String]::IsNullOrWhiteSpace($Result))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] There is no Collection with Name $Collection"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 21
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name CollectionName -Value $Null
Set-Location $Script:CurrentLocation
Return $ReturnObj
}
else
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $True
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "Collection $Collection Found !!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 0
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name CollectionName -Value $Result.Name
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name CollectionID -Value $Result.CollectionID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name CollectionMember -Value $Result.MemberCount
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name CollectionLastRefreshTime -Value $([datetime]::parseexact(($Result.LastRefreshTime).split('.')[0],"yyyyMMddHHmmss",[System.Globalization.CultureInfo]::InvariantCulture))
Set-Location $Script:CurrentLocation
Return $ReturnObj
}
}
Function Get-CMComputerName
{
Param (
[String]$ComputerName
)
##== INIT
If ([String]::IsNullOrWhiteSpace($Script:CMDrive)){$Script:CMDrive = Get-CMSite}
If ([String]::IsNullOrWhiteSpace($Script:CurrentLocation)){$Script:CurrentLocation = Get-Location}
If ([String]::IsNullOrWhiteSpace($ComputerName))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] No value specified for Computer Name"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 22
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMName -Value $Null
Return $ReturnObj
}
##==Change Location to SCCM
Set-Location $Script:CMDrive.SiteDrive
$Result = Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Class SMS_R_System -Filter "NetbiosName='$ComputerName'" -ErrorAction SilentlyContinue
If ([String]::IsNullOrWhiteSpace($Result))
{
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $False
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "[ERROR] There is no Computer with Name $ComputerName In SCCM"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 23
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMName -Value $Null
Set-Location $Script:CurrentLocation
Return $ReturnObj
}
else
{
$CollectionsMemberchip = (Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Query "SELECT SMS_Collection.* FROM SMS_FullCollectionMembership, SMS_Collection where name = '$ComputerName' and SMS_FullCollectionMembership.CollectionID = SMS_Collection.CollectionID" -ErrorAction SilentlyContinue).Name
$ClientActiveStatus = (Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -query "SELECT SMS_G_System_CH_ClientSummary.* FROM SMS_G_System_CH_ClientSummary, SMS_R_System WHERE name = '$ComputerName'and SMS_G_System_CH_ClientSummary.ResourceId = SMS_R_System.ResourceId" -ErrorAction SilentlyContinue).ClientActiveStatus
$ReturnObj = New-Object PSObject
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Success -Value $True
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name Message -Value "Machine $ComputerName Found In SCCM!!"
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ErrorNumber -Value 0
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMName -Value $Result.Name
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMADpath -Value $Result.DistinguishedName
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMResourceId -Value $Result.ResourceId
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMSID -Value $Result.SID
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMWindowsBuild -Value $Result.Build
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMClientVersion -Value $Result.ClientVersion
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMIPAddresses -Value $($Result.IPAddresses -join ",")
Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMMACAddresses -Value $($Result.MACAddresses -join ",")
If ( -not [String]::IsNullOrWhiteSpace($CollectionsMemberchip)){Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMCollectionMemberchip -Value $($CollectionsMemberchip -join ",")}
If ( -not [String]::IsNullOrWhiteSpace($ClientActiveStatus)){Add-Member -InputObject $ReturnObj -MemberType NoteProperty -Name ComputerCMClientIsActive -Value $([system.convert]::ToBoolean($ClientActiveStatus))}
Set-Location $Script:CurrentLocation
Return $ReturnObj
}
}
Function Add-CMComputerToCollection
{
Param (
[String]$ComputerName,
[String]$Collection
)
##== INIT
If ([String]::IsNullOrWhiteSpace($Script:CMDrive)){$Script:CMDrive = Get-CMSite}
If ([String]::IsNullOrWhiteSpace($Script:CurrentLocation)){$Script:CurrentLocation = Get-Location}
##==Change Location to SCCM
Set-Location $Script:CMDrive.SiteDrive
##== Check Computer Name
$CompResult = Get-CMComputerName -ComputerName $ComputerName
If ($CompResult.Success -eq $False){Set-Location $Script:CurrentLocation ; Return $CompResult}
##== Check Collection
$GrpResult = Get-CMComputerCollection -Collection $Collection
If ($GrpResult.Success -eq $False){Set-Location $Script:CurrentLocation ; Return $GrpResult}
##== Check If Computer is not already a member of the Collection
If ($CompResult.ComputerCMCollectionMemberchip -contains $GrpResult.CollectionName)
{
$CompResult.Success = $False
$CompResult.Message = "[ERROR] Computer $ComputerName is already a member of collection $Collection"
$CompResult.ErrorNumber = 24
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionName -Value $GrpResult.CollectionName
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionID -Value $GrpResult.CollectionID
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionMember -Value $GrpResult.CollectionMember
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionLastRefreshTime -Value $GrpResult.CollectionLastRefreshTime
Set-Location $Script:CurrentLocation
Return $CompResult
}
else
{
##== Add computer to collection
### This code section by Kaido & Keith
## More info here https://keithga.wordpress.com/2018/01/25/a-replacement-for-sccm-add-cmdevicecollectiondirectmembershiprule-powershell-cmdlet/
## Code taken from https://github.com/keithga/CMPSLib/blob/master/PSCMLib/Collections/Add-CMDeviceToCollection.ps1
# Note to self: method used here is "AddMembershipRule", not "AddMembershipRule(s)" used in keith code (no array needed !)
$CollectionQuery = Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Class SMS_Collection -filter "Name='$Collection' and CollectionType='2'"
$InParams = $CollectionQuery.PSBase.GetMethodParameters('AddMembershipRule')
$cls = Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Class SMS_CollectionRuleDirect -list
$NewRule = $cls.CreateInstance()
$NewRule.ResourceClassName = "SMS_R_System"
$NewRule.ResourceID = $CompResult.ComputerCMResourceId
$NewRule.Rulename = $CompResult.ComputerCMName
$Rules = $NewRule.psobject.BaseObject
$InParams.CollectionRule = $Rules.psobject.BaseOBject
$PreviousEval = $([datetime]::parseexact(($CollectionQuery.LastMemberChangeTime).split('.')[0],"yyyyMMddHHmmss",[System.Globalization.CultureInfo]::InvariantCulture))
$CollectionQuery.PSBase.InvokeMethod('AddMembershipRule',$InParams,$null) | Out-Null
$CollectionQuery.RequestRefresh() | Out-Null
##== Check If Computer has successfully joined the collection by updating collection membership
$NewEval = $([datetime]::parseexact(($((Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Class SMS_Collection -filter "Name='$Collection' and CollectionType='2'" -Property LastMemberChangeTime).LastMemberChangeTime)).split('.')[0],"yyyyMMddHHmmss",[System.Globalization.CultureInfo]::InvariantCulture))
While ($PreviousEval -ge $NewEval)
{
start-sleep -Seconds 2
$NewEval = $([datetime]::parseexact(($((Get-WmiObject -computername $Script:CMDrive.SiteServer -Namespace $Script:CMDrive.WMInameSpace -Class SMS_Collection -filter "Name='$Collection' and CollectionType='2'" -Property LastMemberChangeTime).LastMemberChangeTime)).split('.')[0],"yyyyMMddHHmmss",[System.Globalization.CultureInfo]::InvariantCulture))
}
##== Get updated infos
$CompResult = Get-CMComputerName -ComputerName $ComputerName
If ($CompResult.ComputerCMCollectionMemberchip -like "*$($GrpResult.CollectionName)*")
{
$GrpResult = Get-CMComputerCollection -Collection $Collection
$CompResult.Message = "Computer $ComputerName was successfully added to collection $Collection"
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionName -Value $GrpResult.CollectionName
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionID -Value $GrpResult.CollectionID
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionMember -Value $GrpResult.CollectionMember
Add-Member -InputObject $CompResult -MemberType NoteProperty -Name CollectionLastRefreshTime -Value $GrpResult.CollectionLastRefreshTime
Set-Location $Script:CurrentLocation
Return $CompResult
}
else
{
$CompResult.Success = $False
$CompResult.Message = "[ERROR] Unable to move Computer $ComputerName to collection $Collection"
$CompResult.ErrorNumber = 25
Set-Location $Script:CurrentLocation
Return $CompResult
}
}
}