Reputation: 5
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
What I've done and works:
$Group = @("server1","server2")
ForEach ($Group in $Group) {
Add-ADPrincipalGroupMembership $User -MemberOf LocalAdmin.$Group.Rights
}
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.
Upvotes: 0
Views: 220
Reputation: 111
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.
Upvotes: 0
Reputation: 25001
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.
Upvotes: 0
Reputation: 135
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.
Upvotes: 0
Reputation: 256
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.
Upvotes: 1
Reputation: 298
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
Upvotes: 0