[breadcrumb]
This PowerShell tool allows easy and quick automation of local administrator rights on Windows boxes. You can specify a user & a server, a list of users & a list of servers, or a mixture of both.
Download the Local Administrator Tool v1.0 here!
Source Code
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 |
################################################################################################# # # # Local Administrator Tool v1.0 # # # # Written by: Mike Oldfield # # Date: 25/05/2016 # # # # This PS1 helps speed up the process of adding accounts to local administrators group on # # remote machines. The script can apply permissions for individual users or a text file list # # of users. It can also apply these permissions on an individual server or a text file list # # of servers. Run the script without parameters to see usage information. # # # # Usage: See Help-File function, or run script with no parameters. # # # ################################################################################################# ############################# # # # PARAMETERS # # # ############################# # Set some avaialble paramters. This bit must come first. param ( [switch]$Add = $true, # -Add: Adds the specified user to the specified server's local administrator group [switch]$Remove = $false, # -Remove: Removes the specified user to the specified server's local administrator group [switch]$List = $false, # -List: Lists all current local administrators on requested server [string]$User = $null, # -User: Specifies a single user [string]$Users = $null, # -Users: Specifies a list of users. This should be a text file with one user per line [string]$Server = $null, # -Server: Specifies a server name [string]$Servers = $null, # -Servers: Specifies a list of servers. This should be a text file with one server per line [string]$Domain = "YourDomain" # -Domain: Specifies a domain name. If no name specified, YourDomain is assumed ) $scriptVer = "v1.0" # What version of the script is this? Used for header info ############################# # # # FUNCTIONS # # # ############################# # Help-File # This function displays help information for when the script is called incorrectly. function Help-File{ Write-Host "Available Parameters:`r`n" -ForegroundColor yellow Write-Host " -Add: Adds the specified user to the specified server's local administrator group" Write-Host " -Domain: Specifies a domain name. If no name specified, YourDomain is assumed" Write-Host " -List: Lists all current local administrators on requested server" Write-Host " -Remove: Removes the specified user to the specified server's local administrator group" Write-Host " -Server: Specifies a server name" Write-Host " -Servers: Specifies a list of servers. This should be a text file with one server per line" Write-Host " -User: Specifies a single user" Write-Host " -Users: Specifies a list of users. This should be a text file with one user per line`r`n" Write-Host "If -Remove is not specified, we assume -Add by default" Write-Host "`r`nExamples of correct usage:`r`n" -ForegroundColor yellow Write-Host ".\LocalAdminTool.ps1 -List -Server Y" Write-Host ".\LocalAdminTool.ps1 -List -Servers Y.txt" Write-Host ".\LocalAdminTool.ps1 -User X -Server Y" Write-Host ".\LocalAdminTool.ps1 -User X -Servers Y.txt" Write-Host ".\LocalAdminTool.ps1 -User X -Domain YourDomain -Server Y" Write-Host ".\LocalAdminTool.ps1 -User X -Domain YourDomain -Servers Y.txt" Write-Host ".\LocalAdminTool.ps1 -Users X.txt -Server Y" Write-Host ".\LocalAdminTool.ps1 -Users X.txt -Servers Y.txt" Write-Host ".\LocalAdminTool.ps1 -Users X.txt -Domain YourDomain -Server Y" Write-Host ".\LocalAdminTool.ps1 -Users X.txt -Domain YourDomain -Servers Y.txt" Write-Host ".\LocalAdminTool.ps1 -Remove -User X -Server Y`r`n" } ############################# # # # HEADER # # # ############################# # Write a pretty header Write-Host "`r`n##############################################`r`n" -ForegroundColor darkcyan -NoNewLine Write-Host "##### " -ForegroundColor darkgray -NoNewLine Write-Host "Local Administrator Tool $scriptVer " -ForegroundColor gray -NoNewLine Write-Host "#####`r`n##### " -ForegroundColor darkgray -NoNewLine Write-Host "Written by: Mike Oldfield" -ForegroundColor gray -NoNewLine Write-Host " #####`r`n##### " -ForegroundColor darkgray -NoNewLine Write-Host "Last Updated: 26/05/2016 " -ForegroundColor gray -NoNewLine Write-Host " #####`r`n" -ForegroundColor darkgray -NoNewLine Write-Host "##############################################`r`n" -ForegroundColor darkcyan ############################# # # # USAGE GUIDE # # # ############################# # Check if the minimum parameters have been declared. We didn't set these as mandatory as we could use -User instead of -Users for example. if ( (!($User) -or !($Server)) -and (!($User) -or !($Servers)) -and (!($Users) -or !($Server)) -and (!($Users) -or !($Servers) ) -and (!($list) -or !($Server)) -and (!($list) -or !($servers)) ) { # If the minimum parameters have not been set, educate the user on how the script works. Write-Host "Missing parameters! Please review details below and try again!`r`n" -ForegroundColor red Help-File Exit } ############################# # # # SCRIPTY BIT # # # ############################# # Check first if we asked to simply list the existing local admins if ($List -eq $true) { # As we've asked to just list, kill off any unnecessary parameters just in case $add = $false $remove = $false $user = $null $users = $null # Now check if we asked for just one server if ($server) { # Write some pretty header to tell us what we're doing. Write-Host "Local Administrators on $Server" -ForegroundColor cyan Write-Host "------------------------------------------" -ForegroundColor gray # Establish a connection to the remote server and find the "Administrators" group. This should result in, for example, WinNT://yourserver/Administrators,group $AdminGroup = [ADSI]"WinNT://$Server/Administrators,group" # Grab a list of members $Members = $AdminGroup.Invoke('Members') #Create an empty array for us to put our members into $MembersList = @() # Now, for each member listed in $Members, assign it a $Member foreach($Member in $Members) { # Find out the name of the member $MemberName = $Member.GetType().InvokeMember("Name", "GetProperty", $null, $Member, $null) # Add the $MemberName to the $MembersList array $MembersList += $MemberName } # Now, sort the $MemberList alphabetically, then for each item in the list... foreach($item in $MembersList | Sort-Object ) { # Tell us who it is Write-Host $item } # Write a blank line to clean up the end of the script Write-Host "" # But if we didn't ask for one server, check that we asked for a list of servers... } elseif ($servers) { # If we did, read the list of servers and put it into $Nodes $Nodes = Get-Content $servers # For each server in the list... foreach($node in $nodes) { # Write some pretty header to tell us what we're doing. Write-Host "Local Administrators on $node" -ForegroundColor cyan Write-Host "------------------------------------------" -ForegroundColor gray # Establish a connection to the remote server and find the "Administrators" group. This should result in, for example, WinNT://yourserver/Administrators,group $AdminGroup = [ADSI]"WinNT://$node/Administrators,group" # Grab a list of members $Members = $AdminGroup.Invoke('Members') #Create an empty array for us to put our members into $MembersList = @() # Now, for each member listed in $Members, assign it a $Member foreach($Member in $Members) { # Find out the name of the member $MemberName = $Member.GetType().InvokeMember("Name", "GetProperty", $null, $Member, $null) # Add the $MemberName to the $MembersList array $MembersList += $MemberName } # Now, sort the $MemberList alphabetically, then for each item in the list... foreach($item in $MembersList | Sort-Object ) { # Tell us who it is Write-Host $item } # Write a blank line to clean up the end of the script Write-Host "" } # But if we didn't ask for a server or list of servers, then we don't know what! } else { # Tell the user we don't know what they've done and give them some help Write-Host "I have no idea what you've just asked me to do!" -ForegroundColor red Help-File } # Kill the script without doing anything further Exit } # Check if we asked to -Remove the user if ($remove -eq $true) { # If we did, then quickly set the $add value to false to ensure it doesn't add anything $add = $false } # Check if we asked for just one server if ($server) { # Check if we asked for just one user if ($user) { # Establish a connection to the remote server and find the "Administrators" group. This should result in, for example, WinNT://yourservername/Administrators,group $AdminGroup = [ADSI]"WinNT://$Server/Administrators,group" # Setup the path to where the user object is. This should result in, for example, WinNT://yourdomain/moldfield,user $Usr = "WinNT://$Domain/$User,user" # Check if we asked to remove the user, and if we did... if ($remove -eq $true) { # Tell us what we're trying to do... Write-Host "Removing $User from local administrators group on $server ... " -NoNewLine # Let's try to remove the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Remove', $Usr) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is not a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user was not a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } # If we didn't ask for $remove, then check we asked to $add instead. We should have by default. } elseif ($add -eq $true) { # Tell us what we're trying to do... Write-Host "Adding $User to local administrators group on $server ... " -NoNewLine # Let's try to add the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Add', $Usr) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is already a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user is already a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } } # But if we didn't ask for just one user, double check that we actually asked for a list of users. If we did... } elseif ($users) { try { # Grab the content of the text file $Usrs = Get-Content $Users -ErrorAction Stop foreach ($Usr in $Usrs) { # Establish a connection to the remote server and find the "Administrators" group. This should result in, for example, WinNT://yourservername/Administrators,group $AdminGroup = [ADSI]"WinNT://$Server/Administrators,group" # Setup the path to where the user object is. This should result in, for example, WinNT://yourdomain/moldfield,user $Usrp = "WinNT://$Domain/$Usr,user" # Check if we asked to remove the user, and if we did... if ($remove -eq $true) { # Tell us what we're trying to do... Write-Host "Removing $Usr from local administrators group on $server ... " -NoNewLine # Let's try to remove the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Remove', $Usrp) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is not a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user was not a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } # If we didn't ask for $remove, then check we asked to $add instead. We should have by default. } elseif ($add -eq $true) { # Tell us what we're trying to do... Write-Host "Adding $Usr to local administrators group on $server ... " -NoNewLine # Let's try to add the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Add', $Usrp) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is already a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user is already a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } } } # Catch any errors that might occur } catch [System.Exception] { # Tell us what went wrong and give some help. # This is likely that the user didn't specify a file that existed. Write-Host "ERROR! $_`r`n" -ForegroundColor red Help-File Exit } } # If we didn't ask for just one server, check if we asked for a list of servers... } elseif ($servers) { # If we did, read the list of servers and put it into $Nodes $Nodes = Get-Content $servers # For each server in the list... foreach($node in $nodes) { # Check if we asked for just one user if ($user) { # Establish a connection to the remote server and find the "Administrators" group. This should result in, for example, WinNT://yourservername/Administrators,group $AdminGroup = [ADSI]"WinNT://$node/Administrators,group" # Setup the path to where the user object is. This should result in, for example, WinNT://yourdomain/moldfield,user $Usr = "WinNT://$Domain/$User,user" # Check if we asked to remove the user, and if we did... if ($remove -eq $true) { # Tell us what we're trying to do... Write-Host "Removing $User from local administrators group on $node ... " -NoNewLine # Let's try to remove the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Remove', $Usr) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is not a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user was not a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } # If we didn't ask for $remove, then check we asked to $add instead. We should have by default. } elseif ($add -eq $true) { # Tell us what we're trying to do... Write-Host "Adding $User to local administrators group on $node ... " -NoNewLine # Let's try to add the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Add', $Usr) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is already a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user is already a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } } # But if we didn't ask for just one user, double check that we actually asked for a list of users. If we did... } elseif ($users) { try { # Grab the content of the text file $Usrs = Get-Content $Users -ErrorAction Stop foreach ($Usr in $Usrs) { # Establish a connection to the remote server and find the "Administrators" group. This should result in, for example, WinNT://yourservername/Administrators,group $AdminGroup = [ADSI]"WinNT://$node/Administrators,group" # Setup the path to where the user object is. This should result in, for example, WinNT://yourdomain/moldfield,user $Usrp = "WinNT://$Domain/$Usr,user" # Check if we asked to remove the user, and if we did... if ($remove -eq $true) { # Tell us what we're trying to do... Write-Host "Removing $Usr from local administrators group on $node ... " -NoNewLine # Let's try to remove the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Remove', $Usrp) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is not a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user was not a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } # If we didn't ask for $remove, then check we asked to $add instead. We should have by default. } elseif ($add -eq $true) { # Tell us what we're trying to do... Write-Host "Adding $Usr to local administrators group on $node ... " -NoNewLine # Let's try to add the user to the Administrators group try { # Try to open up the Administrators group and add the user $AdminGroup.Invoke('Add', $Usrp) # If it went well, tell us it was OK! Make it a pretty green Write-Host "OK!" -ForegroundColor green # But in case it didn't go OK, capture the error... } catch [System.Exception] { # Check if the error was simply that the user is already a local administrator on that server. if ($_ -match "The specified account name is already a member of the group") { # If that was the case, then give us a nice yellow message telling us so. This shortens the message to prevent it spilling onto a new line. Write-Host "This user is already a local administrator!" -ForegroundColor yellow # But if the error was something else... } else { # Tell us what the error was in a nasty red Write-Host "ERROR! $_" -ForegroundColor red } } } } # Catch and errors that might occur } catch [System.Exception] { # Tell us what went wrong and give some help. # This is likely that the user didn't specify a file that existed. Write-Host "ERROR! $_`r`n" -ForegroundColor red Help-File Exit } } } # If, however, we didn't ask for any servers, then we don't know what you've asked for! # This should, in theory, never get triggered, but just incase! } else { # Tell us in a nasty red that we have no idea what you've just asked for and give the user some help Write-Host "I have no idea what you've just asked me to do!" -ForegroundColor red Help-File } # Write a blank line at the end just to make things pretty Write-Host "" |