0

I'm still new to PowerShell and was hoping if someone could assist.

Basically we get a few IT requests coming in asking us to add a single user to bulk server local admin AD groups. We usually copy the the specified info of these servers names in the request details field of the ticket. Example like the below:

server1 server2 server3 server4

AD example group looks like this: LocalAdmin.servername.Rights

Objective: We want to pass each of the above server name value into where servername is.

What I've done and works:

$Group = @("server1","server2")

ForEach ($Group in $Group) {
    Add-ADPrincipalGroupMembership $User -MemberOf LocalAdmin.$Group.Rights
}

Problem: Where you see "server1","server2" I had to re-format that in Notepad to add quotation marks and paste it in, which is a pain, so to refine it I tried the below.

What doesn't work: I successfully got the string of "server1","server2" passed to the variable of $Servers

$Group = @($Servers)

ForEach ($Group in $Group) {
    Add-ADPrincipalGroupMembership $User -MemberOf LocalAdmin.$Group.Rights
}

But I get an error of this:

Add-ADPrincipalGroupMembership : Cannot find an object with identity: 'LocalAdmin."server1","server2".Rights'

I'm expecting the user gets added to each AD group: LocalAdmin.server1.Rights, LocalAdmin.server2.Rights

But the error picks it up as LocalAdmin."server1","server2".Rights instead.

Any help would be appreciated. Thank you.

5 Answers 5

1

If I'm understanding correctly from your initial question and additional comments, you are entering your server list at a prompt.

As your line here demonstrates:

$CopyDetails = Read-Host -Prompt "Enter servers name"

The problem is then that you want an array of strings put in the $Server variable where each array item is one of the servers from your list.

The easiest way to accomplish this is simply to tokenize the input from your line. Assuming that you are pasting "server1,server2,server3,server4" at your earlier prompt, we can then tokenize that as follows:

$ServerList = Read-Host -Prompt "Enter servers name"
$Group = @($ServerList.Split(","))

You can then use $Group as you originally intended.

0

Are You sure about that your $Servers variable is good?

Try this code:

$Servers = @("server1","server2")
$Group = @($Servers)

ForEach($Group in $Group){$Group}

Br, Adam

1
  • Thanks for answering. Yes, this would work however I would have to format: server1 server2 in Notepad to come out as "server1","server2" and paste this in the brackets in PS. I am trying to avoid having to do this.
    – KeijiVBoi
    Jul 23, 2020 at 7:23
0

Let me see if I understand the problem as you see it. You get a list of values unformatted like this:

server1 server2 server3 server4

No commas and no quotation marks. And you want to avoid the hassle of formatting it into this:

"server1", "server2", "server3", "server4"

So you can use it in your script?

If I understand you correctly I'm afraid that you will be disappointed. If you want to run the script as is, you will have to manipulate the text somehow.

One way of perhaps doing less work is to copy the list of servers into a separate text-document and adding linebreaks so each server name occupies it's own line in the text file. Like this:

server1
server2
server3
server4

Then you could modify the script to use Get-Content to get the content of the text file into the $Group variable.

P.S. I would recommend that for clarity you name the array variable $Groups rather than $Group. The ForEach condition becomes more readable that way.

0

You can split your string based on the common delimiter between the server names. The result will be an array that you can loop through.

$servers = 'server1,server2,server3'
$serverArray = $servers -split ',' # You could use -split ' ' if servers are space delimited

foreach ($server in $serverArray) {
    Add-ADPrincipalGroupMembership $User -MemberOf "LocalAdmin.$server.Rights"
}

Note that the PowerShell syntax "server1","server2" creates an array already because the , is outside of the quotes and therefore is an operator. When the , is between quotes, it is part of a string. If you receive an input string that contains quotes and commas, then you will need to split the string but also remove unwanted quotes.

$inputString = '"server1","server2","server3"'
$serverArray = $inputString -split ',' -replace '"'
foreach ($server in $serverArray) {
    Add-ADPrincipalGroupMembership $User -MemberOf "LocalAdmin.$server.Rights"
}

See About Operators for more information on -split as it uses regex matching. It is superior to String.Split() method and does not have any of the pitfalls associated with the method.

0

Looks like the $Servers variable is a String like "server1","server2" instead of an array with both strings, ex.: @("server1", "server2"), so you have to assemble your $Servers variable some other way. Do you have the code for this variable? Then I could help more.

1
  • Yes, sounds like you're right, it is a whole string. I just don't know how to make it into an array while at the same time avoiding to format server1 server2 into "server1","server2" in Notepad. My code for $Servers: $CopyDetails = Read-Host -Prompt "Enter servers name" # I copied the data as is from a ticket: server1 server2 $Bracket = $CopyDetails -replace " ",""",""" #This adds spacing and brackets between each word except the beginning and the end. Current result: server1","server2 $str1 = """" $Servers= $str1 + $Bracket + $str1 Final result: "server1","server2"
    – KeijiVBoi
    Jul 23, 2020 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.