add a computer to multiple Security groups using Read-Host

  Kiến thức lập trình

I am trying to add a computer to multiple Security Groups using the Read-Host to prompt for the Security group names. when doing this, i get an error:

“Add-ADGroupMember : Cannot find an object with identity: ‘”SGtest1″,”SGTest2”,”SGTest3″‘ under: ‘DC=here,DC=here,DC=here,DC=here,DC=here’.
At line:13 char:1 Add-ADGroupMember -ID $SGs -Members $samaccount

CategoryInfo : ObjectNotFound: (“SGtest1″,”SGTest2″,”SGTest3″:ADGroup) [Add-ADGroupMember], ADIdentityNotFoundException
FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember”

i have tried entering the SG names at the prompt separated by commas with quotes around each SG name and without quotes. I have tried multiple entry configurations for the SGs and stil get the same error.

below is the code i am working on:

 $NewDC = Read-host -Prompt 'Enter the DC name you want to add - Example   "    DC1-DC1-c1-01   " without quotes'
 $SecurityGroup = Read-host -Prompt 'Enter the Security Group you want to add members to - Example " SGName " without quotes.  for more than one group seperate the SG name with a comma'

 New-ADComputer -Name $NewDC -SamAccountName $NewDC -Path "OU=Script Test,DC=here,DC=here,DC=here,DC=here,DC=here"
 $samaccount = foreach ($NewDCs in $NewDC)
 {
 Get-ADComputer $NewDC -Properties * | Select-Object SamAccountName

 }

 foreach ($SecurityGroups in $SecurityGroup) {

 Add-ADGroupMember -ID $securityGroups -Members $samaccount
 }

If I replace the Read-Host in the $SecurityGroup with the SG names like below, the script works flawlessly, but i really need to be able to use the Read-Host

 $SecurityGroup = "SGTest1","SGTEst2","SGTest3"

can anyone help me resolve the error so i can use the Read-Host to add a computer to multiple SGs

LEAVE A COMMENT