Add Multiple Users to LocalGroup Multiple Servers

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
brandonw
Posts: 26
Last visit: Thu Nov 03, 2011 4:18 am

Add Multiple Users to LocalGroup Multiple Servers

Post by brandonw »

I was able to get this script to work if I only add 1 username and 1 computername and 1 groupname in the text files. I want to be able to add multiple entries into these text files without Error.
Code:

Code: Select all

# Add a domain user to a local group on the local or a remote computer
$computerName = Get-Content Server1d$ScriptsDevelopmentDepartmentDevServers.txt
$userName = Get-Content Server1d$ScriptsDevelopmentDepartmentDevUsers.txt
$localGroupName = Get-Content Server1d$ScriptsDevelopmentDepartmentLocalGroups.txt
if ($computerName -eq "") {$computerName = "$env:computername"}
[string]$domainName = ([ADSI]'').name
([ADSI]"WinNT://$computerName/$localGroupName,group").Add("WinNT://$domainName/$userName")
Write-Host "User $domainName$userName is now member of local group $localGroupName on $computerName."



The Exception I am getting is:
The following exception occurred while retrieving member "Add": "The network path was not found.
"
At :line:9 char:57
+ ([ADSI]"WinNT://$computerName/$localGroupName,group").Add
brandonw2009-07-16 15:40:03
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Add Multiple Users to LocalGroup Multiple Servers

Post by jvierra »

Problems with you rinpu tdata. Try doing a couple by hand not using variables. Look at results of loading files which can becom ecorrupted.

Try using $env:userdomain

When ADSi complains about path it is usually right that teh path can't be found. Either the path doesn't exist or you gave it a bad path name.
This topic is 14 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked