@Phactotum writes:
I have a list of servers and groups, and I need to add the domain group to the servers local administrators group. I figured out if I do the following I can do a single server group combo:
([ADSI]"WinNT://Server/Administrators,group").add("WinNT://Domain/Group,group")And I found that I can use import-csv to show me my CSV file. I just can’t figure out how to get the server name and group name plugged into the command I want to run.
I thought this was a good question so I figured I’d address it here. The first thought I had when I saw this was, “Whew! I don’t have to come up with the ADSI code”.
That particular API is horribly picky and the objects it returns are quite awkward to use. Jeffrey Hicks has seemed to make good use of them, I think he may know something I don’t.
Anyway, my solution to the problem involves what is probably lesser-known feature of PowerShell called named scriptblocks. Ther’es three of them, Begin{}, Process{}, and End{}. Here’s the script:
Process {
$Server = $_.Server
$Group = $_.Group
([ADSI]"WinNT://$Server/Administrators,group").add("WinNT://Domain/$Group,group")
}
So you would save this as a script (or put it into a function) and call it like this:
Import-Csv file.csv | .\Modify-LocalGroups.ps1
So this is an elegant and easy to read way to solve the problem. An alternate way would involve making a foreach-object loop and do everything inside the script. I like using the named script blocks though because that allows you to keep the input separate from the working part of the script.

A buddy of mine asked the question: why would someone do this at all? This is a problem for group policy, not ADSI. He’s right, that is a better way to go. I don’t know why Phactotum wanted to do it this way. It’s possible he had a perfectly good reason.
That was me, and when I saw this, all I could think was that someone had read an old NT4 book. That’s the only time I have ever seen that type of security group model, and it wasn’t around for long. RIP.
There are many sites out there using just Windows 2000 clients (and servers) and therefore cannot use the flexibility of Group Policy preferences. Therefore they would be expected to use Restricted Groups in Group Policy which makes exceptions problematic.
There have been many times I’ve been asked to script local group administration because restricted groups in group policy do not meet the requirements.
Hello,
I am trying to execute your script , facing some issues could you please help me.
CSV file is not recognized as a cmdlet
Perhaps I should have explained further that the input that this guy had was a pre-existing CSV file with two columns: Server and Group.
Thanks, I was trying replacing the group name in the script and have servers in csv
Will try again today , Thanks for the help