Powershell Script to Add Domain Users and Groups to Local Admin using CSV or arguments
Wed, Jun 9, 2010Below is a script that will import domain users or domain groups into local groups either from the command line or a csv. Enjoy.
#Title: Add-LocalAdmin Script
#Changes: Added "mode", user functionality, and csv input functionality
#Author: send4help.net
#Description: Adds Domain Objects to Local Admin Groups
#Usage: ./add-LocalAdmin.ps1 {Mode:[csv/user/group]} {objectname} {server}
# {objectname}(can be a csv in csv mode)
#csv needs fields servername objectname objecttype #Which are, respectively, name of server, name of the AD object, and whether the type is a group or user
$DomainName = "Contoso"
$mode = $null
$object = $null
$server = $null
$mode = $args[0]
$object = $args[1]
$server = $args[2]
If (($mode -ne "csv") -and ($mode -ne "group") -and ($mode -ne "user"))
{
Write-Host Please specify a mode and try again.
Exit
}
If (($mode -eq "user") -or ($mode -eq "group"))
{
Write-Host Detected mode $mode
$title = "Add Local Admins"
$message = "Do you want to add PHNT\" + $object + " to " + $Server
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Attempts to add object to server"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Exits"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {([ADSI]"WinNT://$server/Administrators,group").add("WinNT://$DomainName/$object,$mode")}
1 {Exit}
}
}
If ($mode -eq "csv")
{
Write-Host Detected mode $mode
Import-CSV $object | foreach {
$serverName = $_.servername
$objectName = $_.objectname
$objectType = $_.objecttype
Write-Host Adding $objecttype $objectname to $servername
([ADSI]"WinNT://$serverName/Administrators,group").add("WinNT://$DomainName/$objectName,$objectType")
}
}