‎08-31-2010 01:19 AM
powershell.exe -noexit \\server\share\addadmin.ps1 {computer}
# Add a domain user to the local Administrators group on the local or a remote computer
$domainname = "YourDomain"
$erroractionpreference = "SilentlyContinue"
if ($args.count -ge "1") {$computername = $args}
else {$computerName = Read-Host 'Enter computer name or press <Enter> for localhost'}
$userName = Read-Host 'Enter user name'
if ($computerName -eq "") {$computerName = "$env:computername"}
([ADSI]"WinNT://$computerName/Administrators,group").Add("WinNT://$domainName/$userName")
$computer = [ADSI]("WinNT://" + $computername + ",computer")
$Group = $computer.psbase.children.find("Administrators")
$members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
ForEach($user in $members)
{
if ($user -eq $userName) {$found = $true}
}
if ($found -eq $true) {Write-Host "User $domainName\$userName is now local administrator on $computerName."}
else {Write-Host "User not found"}
Experience Lansweeper with your own data. Sign up now for a 14-day free trial.
Try Now