RemoteAdmins.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
. $PSScriptRoot\PullList.ps1

#Gets remote admins from the computer. Uses default Administrators group SID value to retrive admins if no groupname is supplied.
#User calling this function must have admin priviledges on the remote computer.
Function Get-Admins {
    Param($ComputerName, $GroupName, [PSCredential]$Credential)
    if ($GroupName) {
        $GroupName = $GroupName -join ";"
    }
    $command = {
        param($GroupName)
        $localObject = @() 
        if (!($GroupName)) {
            $Computer = [ADSI]"WinNT://$env:COMPUTERNAME,computer"  
            $Groups = $Computer.psbase.Children | Where {$_.psbase.schemaClassName -eq "group"}
            foreach ($j in $Groups) {
                $b = $j.Path.split('/', [StringSplitOptions]::RemoveEmptyEntries)
                [string[]]$GroupName += $b[-1] 
            }
        }
        elseif ($GroupName -eq "S-1-5-32-544") {
            $objSID = New-Object System.Security.Principal.SecurityIdentifier($GroupName)
            $objgroup = $objSID.Translate( [System.Security.Principal.NTAccount]).Value.split("\")[1]
            $GroupName = $objgroup
        }
        else {$GroupName = $GroupName.split(";")}

        Foreach ($g in $GroupName) {
            Try {
       
                $Group = [ADSI]("WinNT://$env:COMPUTERNAME/$g,group")
                $Members = @($Group.psbase.Invoke("Members"))
                if ($Members) {
                    ForEach ($Member In $Members) {
                        $AdsPath = $Member.GetType().InvokeMember("Adspath", "GetProperty", $null, $Member, $null)
                        $a = $AdsPath.split('/', [StringSplitOptions]::RemoveEmptyEntries)
                        $Name = $a[-1]
                        $domain = $a[-2]
                        if ($domain -eq $env:COMPUTERNAME)
                        {$FullName = $Name}
                        elseif ($domain -eq "WinNT:") {
                            Try {
                                $objSID = New-Object System.Security.Principal.SecurityIdentifier($Name) -ErrorAction stop
                                $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
                                $FullName = $objUser.Value 
                            }
                            Catch {$FullName = $Name}
                        }  
                        Else
                        {$FullName = $domain + "\" + $Name}       
                        $Class = $Member.GetType().InvokeMember("Class", 'GetProperty', $Null, $Member, $Null)

                        $localObject += New-Object PSObject -Property @{ 
                            "Name"         = $FullName;
                            "Class"        = $class;
                            "GroupName"    = $g;
                            "ComputerName" = $env:COMPUTERNAME
                        }            
                    } #Forloop 2 close
                } # if close
        
                else {
                    $localObject += New-Object PSObject -Property @{ 
                        "Name"         = "-";
                        "Class"        = "-";
                        "GroupName"    = $g;
                        "ComputerName" = $env:COMPUTERNAME
                    } #property close
                }# else close
            } #Try close
            Catch {
                $ErrorMessage = $_.Exception.Message
                Write-Warning "$g - $ErrorMessage"
                $localObject += New-Object PSObject -Property @{ 
                    "Name"         = "-";
                    "Class"        = "-";
                    "GroupName"    = "$g NOT found!";
                    "ComputerName" = $env:COMPUTERNAME
                } #property close
            }
        } #Forloop 1 close
        return $localObject
    } #command close

    $AdminMembers = @()
    if (!($Credential)) { 
        $AdminMembers += Invoke-Command -ComputerName $ComputerName -ScriptBlock $command -ArgumentList ($GroupName)
    }
    else {   
        $AdminMembers += Invoke-Command -ComputerName $ComputerName -ScriptBlock $command -ArgumentList ($GroupName) -Credential $Credential
    }
    $newObject = New-Object psobject
    $newObject = $AdminMembers | Select -Property Name, Class, GroupName, ComputerName -Unique
    return $newObject
}

#Adds users to the remote computer. Uses Administrators group SID value to add admins if no groupname is supplied.
#Will not attempt to add user if the user already exists on the computer.
#User calling this function must have admin priviledges on the remote computer.
Function Add-Admins {
    Param($goodAdmins, $ResolvedComputers, $GroupName, [PSCredential]$Credential)

    $command = {
        param ($goodAdmins, $GroupName)
        if ($GroupName -eq "S-1-5-32-544") {
            $objSID = New-Object System.Security.Principal.SecurityIdentifier($GroupName)
            $objgroup = $objSID.Translate( [System.Security.Principal.NTAccount]).Value.split("\")[1]
            $GroupName = $objgroup
        }
        function Get-LocalAdmins {
            $localObject = @()
            $Group = [ADSI]("WinNT://$env:COMPUTERNAME/$GroupName,group")
            $Members = @($Group.psbase.Invoke("Members"))
            if ($Members) {
                ForEach ($Member In $Members) {
                    $AdsPath = $Member.GetType().InvokeMember("Adspath", "GetProperty", $null, $Member, $null)
                    $a = $AdsPath.split('/', [StringSplitOptions]::RemoveEmptyEntries)
                    $Name = $a[-1]
                    $domain = $a[-2]
                    if ($domain -eq $env:COMPUTERNAME)
                    {$FullName = $Name}
                    elseif ($domain -eq "WinNT:") {
                        Try {
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($Name) -ErrorAction stop
                            $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
                            $FullName = $objUser.Value
                        }
                        Catch {$FullName = $Name}
                    }  
                    Else
                    {$FullName = $domain + "\" + $Name}
     
                    $localObject += New-Object PSObject -Property @{ 
                        "Name" = $FullName;
                    }            
                } 
            } # if close
            return $localObject
        } 
        $presentAdmins = Get-LocalAdmins 
        $goodAdmins = $goodAdmins.split(';')
        $toadd = @()
        $toAdd = $goodAdmins | Where {$_ -notin $presentAdmins.Name}
        [string[]]$toDisplay = $goodAdmins | Where {$_ -in $presentAdmins.Name}
        $toDisplay = $toDisplay -join ','
        if ($toDisplay) {Write-Host "$env:COMPUTERNAME : The User(s) Already Exists: $toDisplay" -ForegroundColor DarkRed}        
        $group = [ADSI]("WinNT://$env:COMPUTERNAME/$GroupName,group")
        $toAdd = $toAdd | Foreach {$_.Replace("\", "/")} 
        foreach ($i in $toAdd) {$group.Add("WinNT://" + $i)}
    }
    #net localgroup administrators $toAdd /ADD}
    if (!($Credential)) {
        Invoke-Command -ComputerName $ResolvedComputers -ScriptBlock $command -ArgumentList ($goodAdmins, $GroupName)
    }
    else {
        Invoke-Command -ComputerName $ResolvedComputers -ScriptBlock $command -ArgumentList ($goodAdmins, $GroupName) -Credential $Credential
    }
}

#Removes users from the remote computer. Uses Administrators group SID value to remove admins if no groupname is supplied.
#Will not attempt to remove a user from the computer if the user does not exist.
#User calling this function must have admin priviledges on the remote computer.
Function Remove-Admins {
    Param($BadAdmins, $ResolvedComputers, $GroupName, [PSCredential]$Credential)

    $command = {
        param ($BadAdmins, $GroupName)
        if ($GroupName -eq "S-1-5-32-544") {
            $objSID = New-Object System.Security.Principal.SecurityIdentifier($GroupName)
            $objgroup = $objSID.Translate( [System.Security.Principal.NTAccount]).Value.split("\")[1]
            $GroupName = $objgroup
        }
        function Get-LocalAdmins {
            $localObject = @()
            $Group = [ADSI]("WinNT://$env:COMPUTERNAME/$GroupName,group")
            $Members = @($Group.psbase.Invoke("Members"))
            if ($Members) {
                ForEach ($Member In $Members) {
                    $AdsPath = $Member.GetType().InvokeMember("Adspath", "GetProperty", $null, $Member, $null)
                    $a = $AdsPath.split('/', [StringSplitOptions]::RemoveEmptyEntries)
                    $Name = $a[-1]
                    $domain = $a[-2]
                    if ($domain -eq $env:COMPUTERNAME)
                    {$FullName = $Name}
                    elseif ($domain -eq "WinNT:") {
                        Try {
                            $objSID = New-Object System.Security.Principal.SecurityIdentifier($Name) -ErrorAction stop
                            $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
                            $FullName = $objUser.Value
                        }
                        Catch {$FullName = $Name}
                    }  
                    Else
                    {$FullName = $domain + "\" + $Name}
     
                    $localObject += New-Object PSObject -Property @{ 
                        "Name" = $FullName;
                    }            
                } 
            } # if close
            return $localObject
        }
        $presentAdmins = Get-LocalAdmins
        $BadAdmins = $BadAdmins.split(';')
        $toRemove = @()
        $toRemove = $BadAdmins | Where {$_ -in $presentAdmins.Name}
        $toDisplay = $BadAdmins | where {$_ -notin $presentAdmins.Name}
        $toDisplay = $toDisplay -join ','
        if ($toDisplay) {Write-Host "$env:COMPUTERNAME : The User(s) Does not Exist: $toDisplay" -ForegroundColor DarkRed}
        $group = [ADSI]("WinNT://$env:COMPUTERNAME/$GroupName,group")
        $toRemove = $toRemove | Foreach {$_.Replace("\", "/")} 
        Foreach ($i in $toRemove) {$group.Remove("WinNT://" + $i)}
    }

    if (!($Credential)) {
        Invoke-Command -ComputerName $ResolvedComputers -ScriptBlock $command -ArgumentList ($BadAdmins, $GroupName)
    }
    else {
        Invoke-Command -ComputerName $ResolvedComputers -ScriptBlock $command -ArgumentList ($BadAdmins, $GroupName) -Credential $Credential
    }
}

#Renames any Local group on the remote computer.
#User calling this function must have admin priviledges on the remote computer.
Function Rename-Group {
    Param($ComputerName, $Name, $NewName)
    $command = {
        param($Name, $NewName)
        try {
            $group = [ADSI]"WinNT://$ENV:COMPUTERNAME/$Name, group"
            if ($group) {
                $group.psbase.Rename($NewName)
                $group.psbase.CommitChanges()
            }
        }
        catch {
            $ErrorMessage = $_.Exception.Message
            Write-Warning "$ENV:COMPUTERNAME - $ErrorMessage"
        }
     
    }
    Invoke-Command -ComputerName $ComputerName -ScriptBlock $command -ArgumentList $Name, $NewName

}

#Accepts parameters and performs validation before calling the function Get-Admins to fetch users from remote computer.
#User calling this function must have admin priviledges on the remote computer.
Function Get-RemoteAdmins {

    <#
    .SYNOPSIS
        Get users on remote Computer.
  
    .DESCRIPTION
        The Get-RemoteAdmins cmdlet fetches Admins on the remote Computer and lists the member type. This is done using the PowerShell ADSI adapter.
 
    .PARAMETER ComputerName
        Specifies the Remote Computer Name. This can be supplied as a string with comma separated values or as a path to an Excel sheet.
  
    .PARAMETER Export
        Specifies the path to export the results of the command. This is a path to a CSV file.
         
    .PARAMETER GroupName
        Specifies the remote Computer group to perform the Operation. The default group is "Administrators" if no value is supplied.
 
    .PARAMETER Unique
        Return a unique list of users.
 
    .PARAMETER AllGroups
        Return a list of users from all the local groups on the remote computer.
 
    .EXAMPLE
        Get-RemoteAdmins -ComputerName Computer1,Computer2 -GroupName "Remote Desktop Users"
        Fetches all the users in the group "Remote Desktop Users" on the Computers Computer1 and Computer2.
     
    .EXAMPLE
        Get-RemoteAdmins -ComputerName Computer1,Computer2 -GroupName "Remote Desktop Users" -Unique
        Fetches all the unique users in the group "Remote Desktop Users" on the Computers Computer1 and Computer2.
         
    .EXAMPLE
        Get-RemoteAdmins -ComputerName Computer1,Computer2 -Export "C:\users\user1\Desktop\Export.csv"
        Fetches all the users in the group Administrators on the Computers Computer1 and Computer2 and stores them in the file Export.csv
 
    .EXAMPLE
        Get-RemoteAdmins -ComputerName "C:\users\user1\Desktop\Computers.xlsx" -Export "C:\users\user1\Desktop\Export.csv" -GroupName Guests -Unique
        Fetches all the unique users in the group Guests on the list of Computers in "Computers.xlsx" and stores them in the file Export.csv
         
    .EXAMPLE
        Get-RemoteAdmins -ComputerName Computer1,Computer2 -Export "C:\users\user1\Desktop\Export.csv" -AllGroups
        Fetches all the users from all the local groups on the Computers Computer1 and Computer2 and stores them in the file Export.csv
 
    .EXAMPLE
        Get-RemoteAdmins -ComputerName Computer1,Computer2 -AllGroups -Unique
        Fetches all the users from the all the local groups on the Computers Computer1 and Computer2 and removes the duplicate users.
 
    .EXAMPLE
        Get-RemoteAdmins -ComputerName 'C:\users\user1\Desktop\Computers.xlsx' -AllGroups | Where {$_.Name -eq "DOMAIN\USER"}
        Fetches the user "DOMAIN\USER" from the all the Computers in the file "C:\users\user1\Desktop\Computers.xlsx". This can be used to see on what computers does "DOMAIN\USER" exists.
    #>

    [CmdletBinding(DefaultParameterSetName = 'singleGroup')]
    Param(

        [Parameter(Mandatory = $True,
            Position = 0, ParameterSetName = 'singleGroup')]
        [Parameter(Mandatory = $True,
            Position = 0, ParameterSetName = 'AllGroup')]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName,


        [Parameter(Mandatory = $false,
            Position = 1, ParameterSetName = 'singleGroup')]
        [Parameter(Mandatory = $false,
            Position = 1, ParameterSetName = 'AllGroup')]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("^*.csv$")]
        [string]$Export,

        [Parameter(Mandatory = $false,
            Position = 2, ParameterSetName = 'singleGroup')]
        [ValidateNotNullOrEmpty()]
        [string[]]$GroupName = 'S-1-5-32-544',

        [Parameter(Mandatory = $false,
            Position = 3, ParameterSetName = 'singleGroup')]
        [Parameter(Mandatory = $false,
            Position = 3, ParameterSetName = 'AllGroup')]
        [switch]$Unique,

        [Parameter(Mandatory = $false,
            Position = 2, ParameterSetName = 'AllGroup')]
        [switch]$AllGroups,

        [Parameter(Mandatory = $false,
            Position = 4, ParameterSetName = 'singleGroup')]
        [Parameter(Mandatory = $false,
            Position = 4, ParameterSetName = 'AllGroup')]
        [ValidateNotNullOrEmpty()]
        [PSCredential]$Credential


    )
    switch ($PsCmdlet.ParameterSetName) {
        "singleGroup" {

            Try {

                $newComputerName = ValidateComputerName($ComputerName)

                $ResolvedComputers = ResolveDNS ($newComputerName)
                Write-Verbose -Verbose "Getting Remote Admins..."
                $Admins = Get-Admins $ResolvedComputers $GroupName $Credential
                     
                if (($Export.Length -ne 0) -and ($Unique -eq $True)) {
                    $customFormat = New-Object psobject 
                    $customFormat = $Admins | select -Property Name, Class, GroupName -Unique                   
                    $customFormat | Export-CSV -Path $Export -NoTypeInformation
                    Write-Host "The admin list is stored in $Export" -ForegroundColor Yellow
                }
                     
                elseif (($Export.Length -eq 0) -and ($Unique -eq $True)) {
                    $customFormat = New-Object psobject 
                    $customFormat = $Admins | select -Property Name, Class, GroupName -Unique
                    $customFormat
                }

                elseif (($Export.Length -ne 0) -and ($Unique -eq $false)) {
                    $Admins | Export-CSV -Path $Export -NoTypeInformation
                    Write-Host "The admin list is stored in $Export" -ForegroundColor Yellow
                }
                     
                else {
                    $Admins                                         
                }

            }
            Catch {
              
                Write-Warning $_.Exception.Message
              
            }

        } #Single Group end


        "AllGroup" {

            Try {

                $newComputerName = ValidateComputerName($ComputerName)

                $ResolvedComputers = ResolveDNS ($newComputerName)
                Write-Verbose -Verbose "Getting Remote Admins..."
                $Admins = Get-Admins $ResolvedComputers $Credential
                     
                if (($Export.Length -ne 0) -and ($Unique -eq $True)) {
                    $customFormat = New-Object psobject 
                    $customFormat = $Admins | select -Property Name, Class, GroupName -Unique                   
                    $customFormat | Export-CSV -Path $Export -NoTypeInformation
                    Write-Host "The admin list is stored in $Export" -ForegroundColor Yellow
                }
                     
                elseif (($Export.Length -eq 0) -and ($Unique -eq $True)) {
                    $customFormat = New-Object psobject 
                    $customFormat = $Admins | select -Property Name, Class, GroupName -Unique
                    $customFormat
                }

                elseif (($Export.Length -ne 0) -and ($Unique -eq $false)) {
                    $Admins | Export-CSV -Path $Export -NoTypeInformation
                    Write-Host "The admin list is stored in $Export" -ForegroundColor Yellow
                }
                     
                else {
                    $Admins                                         
                }

            }
            Catch {
              
                Write-Warning $_.Exception.Message
              
            }

        } # Allgroup end
            
    } #Paramter set end

}

#Accepts parameters and performs validation before calling the function Add-Admins to add users on the remote computer.
#User calling this function must have admin priviledges on the remote computer.
Function Add-RemoteAdmins {
    <#
    .SYNOPSIS
        Add user on remote Computer.
  
    .DESCRIPTION
        The Add-RemoteAdmins cmdlet adds Admins on the remote Computer. This is done using the PowerShell ADSI adapter.
 
    .PARAMETER ComputerName
        Specifies the Remote Computer Name. This can be supplied as a string with comma separated values or as a path to an Excel sheet.
  
    .PARAMETER AddAdmin
        Specifies the Admins to be added on the remote Computer. This can be supplied as a string with comma separated values or as a path to an Excel sheet.
         
    .PARAMETER GroupName
        Specifies the remote Computer group to perform the Operation. The default group is "Administrators" if no value is supplied.
 
    .EXAMPLE
        Add-RemoteAdmins -ComputerName Computer1,Computer2 -AddAdmin Domain\user1,Domain\user2 -GroupName "Remote Desktop Users"
        Adds user1 and user2 in the group "Remote Desktop Users" on the Computers Computer1 and Computer2.
         
    .EXAMPLE
        Add-RemoteAdmins -ComputerName Computer1,Computer2 -AddAdmin Domain\user1,Domain\user2
        Adds user1 and user2 in the group Administrators on the Computers Computer1 and Computer2
 
    .EXAMPLE
        Add-RemoteAdmins -ComputerName Computer1,Computer2 -AddAdmin "C:\users\user1\Desktop\AddAdmin.xlsx"
        Adds the users from "AddAdmin.xlsx" in the Administrators group on Computer1 and Computer2.
 
    .EXAMPLE
        Add-RemoteAdmins -ComputerName "C:\users\user1\Desktop\Computers.xlsx" -AddAdmin "C:\users\user1\Desktop\AddAdmin.xlsx"
        Adds the users from "AddAdmin.xlsx" in the Administrators group on the list of Computers in "Computers.xlsx"
 
    #>

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True,
            Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName,

        [Parameter(Mandatory = $true,
            Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string[]]$AddAdmin,

        [Parameter(Mandatory = $false,
            Position = 2)]
        [ValidateNotNullOrEmpty()]
        [string]$GroupName = 'S-1-5-32-544',

        [Parameter(Mandatory = $false,
            Position = 3)]
        [ValidateNotNullOrEmpty()]
        [pscredential]$Credential

    )
    

    $newComputerName = ValidateComputerName($ComputerName)
  
    if ($AddAdmin -match "^*.xlsx$") {
        Resolve-Path $AddAdmin > $null  -ErrorAction Stop
        Write-Verbose -Verbose "Getting the list of Admins to add..." 
        $newAddAdmin = PullList ($AddAdmin)
        $newAddAdmin = $newAddAdmin -join ';'
    }
    else {
        $newAddAdmin = $AddAdmin
    }
                     

    Try {
        $ResolvedComputers = ResolveDNS ($newComputerName)

        Write-Verbose -Verbose "Adding Remote Admins..."
        Add-Admins $newAddAdmin $ResolvedComputers $GroupName $Credential
        Write-Host "Completed" -ForegroundColor Green
    
    }
    Catch {
        Write-Warning $_.Exception.Message
    }    

}

#Accepts parameters and performs validation before calling the function Remove-Admins to remove users from the remote computer.
#User calling this function must have admin priviledges on the remote computer.
Function Remove-RemoteAdmins {
    <#
    .SYNOPSIS
        Remove user on remote Computer.
  
    .DESCRIPTION
        The Remove-RemoteAdmins cmdlet removes the Admins from the remote Computer. This is done using the PowerShell ADSI adapter.
 
    .PARAMETER ComputerName
        Specifies the Remote Computer Name. This can be supplied as a string with comma separated values or as a path to an Excel sheet.
  
    .PARAMETER RemoveAdmin
        Specifies the Admins to be removed from the remote Computer. This can be supplied as a string with comma separated values or as a path to an Excel sheet.
         
    .PARAMETER GroupName
        Specifies the remote Computer group to perform the Operation. The default group is "Administrators" if no value is supplied.
 
    .EXAMPLE
        Remove-RemoteAdmins -ComputerName Computer1,Computer2 -RemoveAdmin Domain\user1,Domain\user2 -GroupName "Remote Desktop Users"
        Removes user1 and user2 from the group "Remote Desktop Users" on the Computers Computer1 and Computer2.
         
    .EXAMPLE
        Remove-RemoteAdmins -ComputerName Computer1,Computer2 -RemoveAdmin Domain\user1,Domain\user2
        Removes user1 and user2 from the group Administrators on the Computers Computer1 and Computer2
 
    .EXAMPLE
        Remove-RemoteAdmins -ComputerName Computer1,Computer2 -RemoveAdmin "C:\users\user1\Desktop\RemoveAdmin.xlsx"
        Pulls the list of users from the given excel and removes them from the Administrator group on Computer1 and Computer2.
 
    .EXAMPLE
        Remove-RemoteAdmins -ComputerName "C:\users\user1\Desktop\Computers.xlsx" -RemoveAdmin "C:\users\user1\Desktop\RemoveAdmin.xlsx"
        Removes the users in "RemoveAdmin.xlsx" from the Administrators group on the list of Computers in "Computers.xlsx"
 
    #>

    [CmdletBinding()]
    Param(

        [Parameter(Mandatory = $True,
            Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName,

        [Parameter(Mandatory = $true,
            Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string[]]$RemoveAdmin,

        [Parameter(Mandatory = $false,
            Position = 2)]
        [ValidateNotNullOrEmpty()]
        [string]$GroupName = 'S-1-5-32-544',

        [Parameter(Mandatory = $false,
            Position = 3)]
        [ValidateNotNullOrEmpty()]
        [pscredential]$Credential
                
    )

    $newComputerName = ValidateComputerName($ComputerName)

    if ($RemoveAdmin -match "^*.xlsx$") {
        Resolve-Path $RemoveAdmin > $null  -ErrorAction Stop
        Write-Verbose -Verbose "Pulling Bad Admins list..."                        
        $newRemoveAdmin = PullList($RemoveAdmin)
        $newRemoveAdmin = $newRemoveAdmin -join ';'
    }
    else {
        $newRemoveAdmin = $RemoveAdmin
    }
                    
    Try {
        $ResolvedComputers = ResolveDNS ($newComputerName)

        Write-Verbose -Verbose "Removing Remote Admins..."
        Remove-Admins $newRemoveAdmin $ResolvedComputers $GroupName $Credential
        Write-Host "Completed" -ForegroundColor Green
    
    }
    Catch {
        Write-Warning $_.Exception.Message
    }

}

#Accepts parameters and performs validation before calling the function Rename-Admins on the remote computer.
#User calling this function must have admin priviledges on the remote computer.
Function Rename-RemoteGroup {
    <#
    .SYNOPSIS
        Rename a group on a remote Computer.
  
    .DESCRIPTION
        The Rename-RemoteGroup cmdlet renames groups on the remote Computer. This is done using the PowerShell ADSI adapter.
 
    .PARAMETER ComputerName
        Specifies the Remote Computer Name. This can be supplied as a string with comma separated values or as a path to an Excel sheet.
  
    .PARAMETER Name
        Specifies the Name of the group to be renamed on the remote Computer.
         
    .PARAMETER NewName
        Specifies the new name of the remote Computer group.
 
    .EXAMPLE
        Rename-RemoteGroup -ComputerName Computer1,Computer2 -Name User -NewName "Remote Desktop Users"
        Renames the group User on Computer1 and Computer2 to "Remote Desktop Users"
 
    .EXAMPLE
        Rename-RemoteGroup -ComputerName "C:\users\user1\Desktop\Computers.xlsx" -Name User -NewName "Remote Desktop Users"
        Renames the group User to "Remote Desktop Users" from the list of Computers in "Computers.xlsx"
 
    #>

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True,
            Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName,

        [Parameter(Mandatory = $true,
            Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(Mandatory = $true,
            Position = 2)]
        [ValidateNotNullOrEmpty()]
        [string]$NewName

    )
    
    $newComputerName = ValidateComputerName($ComputerName)
    $resolvedComputers = ResolveDNS($newComputerName)
    
    Write-Verbose -Verbose "Renaming the group $Name to $NewName..."
    Rename-Group $resolvedComputers $Name $NewName
    Write-Host "Completed" -ForegroundColor Green

}
export-modulemember -function Get-RemoteAdmins
export-modulemember -function Add-RemoteAdmins
export-modulemember -function Remove-RemoteAdmins
Export-ModuleMember -Function Rename-RemoteGroup