-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathAddMember-ToLocalAdministrators.ps1
80 lines (64 loc) · 2.85 KB
/
AddMember-ToLocalAdministrators.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
<#
.Synopsis
Adds a user or group to local administrator group
.Description
This scripts adds the given user or group to local administrators group on given list of servers.
.Parameter ComputerName
Computer Name(s) on which you want to add user/group to local administrators
.Parameter ObjectType
This parameter takes either of two values, User or Group. This parameter indicates the type of object
you want to add to local administrators
.Parameter ObjectName
Name of the object (user or group) which you want to add to local administrators group. This should be in
Domain\UserName or Domain\GroupName format
.Example
Set-LocalAdminGroupMembers.ps1 -ObjectType User -ObjectName "AD\TestUser1" -ComputerName srvmem1, srvmem2
Adds AD\TestUser1 user account to local administrators group on srvmem1 and srvmeme2
.Example
Set-LocalAdminGroupMembers.ps1 -ObjectType Group -ObjectName "ADDomain\AllUsers" -ComputerName (Get-Content c:\servers.txt)
Adds AD\TestUser1 Group to local administrators group on servers listed in c:\servers.txt
.Notes
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,Position=1)]
[ValidateSet("User","Group")]
[String]
$ObjectType,
[Parameter(Mandatory=$true,Position=2)]
[ValidateScript({($_.split("\").count -eq 2)})]
[string]$ObjectName,
[Parameter(Position=3)]
[String[]]$ComputerName=$env:COMPUTERNAME
)
#Name and location of the output file. Change this line if you want to alter the location
$ResultsFile = "c:\temp\ResultsofLocalGroupAddition.csv"
$ObjDomain = $ObjectName.Split("\")[0]
$ObjName = $ObjectName.Split("\")[1]
$ComputerCount = $ComputerName.Count
$count = 0
Add-Content -Path $ResultsFile -Value "ComputerName,Status,Comments"
foreach($Computer in $ComputerName) {
$count++
$Status=$null
$Comment = $null
Write-Host ("{0}. Working on {1}" -f $Count, $Computer)
if(Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
Write-Verbose "$Computer : Online"
try {
$GroupObj = [ADSI]"WinNT://$Computer/Administrators"
$GroupObj.Add("WinNT://$ObjDomain/$ObjName")
$Status = "Success"
$Comment = "Added $ObjectName $ObjectType to Local administrators group"
Write-Verbose "Successfully added $ObjectName $ObjectType to $Computer"
} catch {
$Status = "Failed"
$Comment = $_.toString().replace("`n","").replace("`r","")
Write-Verbose "Failed to add $ObjectName $ObjectType to $Computer"
}
Add-Content -Path $ResultsFile -Value ("{0},{1},{2}" -f $Computer,$Status,$Comment )
} else {
Write-Warning "$Computer : Offline"
Add-Content -Path $ResultsFile -Value ("{0},{1}" -f $Computer,"Offline")
}
}