Saturday 29 November 2008

Add-DomainUserToLocalAdministatorsGroup.ps1

#Requires -Version 2.0
#
Add-DomainUserToLocalAdministatorsGroup.ps1
#
Add a domain user to a Local Administrator's group
#
Thomas Lee - tfl@psp.co.uk
#
Based on http://powershell.com/cs/media/p/380.aspx, with some error checking

# Setup up information on user to add,etc
$Domain = "Cookham" # where the user account comes from
$Computer = "Cookham8" # Computer to add the user to
$User = "tfl" # User

# Try to add user to Administrator's group
try {
$group.Add("WinNT://" + $Domain + "/" + $User)
"User {0}\{1} added to Administrator's local group in system: {2}" -f $domain,$user, $computer
}
catch {
"Error adding user to Administrator's group"
}
""
# Now print results
$computer = [ADSI]("WinNT://" + $Computer + ",computer")
$group = $computer.psbase.children.find("Administrators")

"Group: {0} contains {1} members:" -f $group.Name.Value, $members.Length
$members = $group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)}
$members
This script produces the following output (assuming no errors!):
PS C:\foo> .\Add-DomainUserToLocalAdministatorsGroup.ps1
User Cookham\tfl added to Administrator's local group in system: Cookham8

Group: Administrators contains 3 members:
Administrator
Domain Admins
tfl


Technorati Tags: ,,

No comments: