Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Is there a powershell script to add a listed user or Group to the local Administrators Group without calling a separate text file?

$
0
0

I have been searching for a way to run a powershell script that does not call another text file but that simpley will add a User account and a Domain Group to the Lcoal Administrators group. I have tried the below, and it says it adds the test user 'Dummy' but when i actually check the local Administrators group, the "Dummy" account is not there.

# if your user name is whatever\test
 
$domain = "DomainName"
$strComputer = "Localhost"
$username = "Dummy"
 
$computer = [ADSI]("WinNT://" + $strComputer + ",computer")
$computer.name
 
$Group = $computer.psbase.children.find("administrators")
$Group.name
 
# This will list what’s currently in Administrator Group so you can verify the result
 
function ListAdministrators
 
{$members= $Group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", ‘GetProperty’, $null, $_, $null)}
 $members}
ListAdministrators
 
# Even though we are adding the AD account but we add it to local computer and so we will need to use WinNT: provider
 
$Group.Add("WinNT://" + $domain + "/" + $username)
 
ListAdministrators
 
$Group.Remove("WinNT://" + $domain + "/" + $username)
 
ListAdministrators


David Baur


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>