How to feed an array of items into Add-LocalGroupMember

Hello,
I have this code, I can’t figure out how to get the array into the group. I know the top works , if I print out the array before the invoke-command part, it prints out the names I entered. But it is giving an error:

What Computer do you want to add users to local admin group on?: server3
What users do you want to add?: tom
What users do you want to add?: dick
What users do you want to add?: harry
What users do you want to add?: 
Cannot validate argument on parameter 'Member'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    + CategoryInfo          : InvalidData: (:) [Add-LocalGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand
    + PSComputerName        : server3

Here is the script:

$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock{
   Add-LocalGroupMember -Group "Administrators" -Member $Userarray
}

Thanks!

Here is 1 of my attempts that fails: I have local user accounts called tom, dick, and harry

$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock{
	Foreach ($user in $userarray) {
   Add-LocalGroupMember -Group Administrators -Member $User
}
}

You are trying to use a variable inside a script block you defined outside of it. Your issue is about scope.

You may read the following help topics completely including the examples to learn how to deal with scopes.

This seems like it should work, but it doesnt do anything and I get no error:

$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock{
	Foreach ($Using:user in $userarray) {
   Add-LocalGroupMember -Group Administrators -Member $User
   
}
}

Ive tried changing the $User variable but still not working. Is $User referenced the same way inside the Invoke command after the $Using:User?

$User is not the variable you define OUTSIDE of the scriptblock. :wink:

Still no luck.

Here is my code:

$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
$input = (Read-Host -Prompt "What users do you want to add?")
if($input -ne '') {$Userarray += $input}
	}
until ($input -eq '')
write-host "before scriptblock"
$Computer
$input

Invoke-Command -ComputerName $Computer -ScriptBlock{
	Foreach ($Using:user in $userarray) {
   Add-LocalGroupMember -Group Administrators -Member $input
    }
   Get-LocalGroupMember Administrators
    hostname
    Write-Host "Inside ScriptBlock"
   $computer
   $input
   $user
    }
    Write-Host "Outside ScriptBlock"
    $computer
   $input
   $user

Output:

What Computer do you want to add users to local admin group on?: server3
What users do you want to add?: tom
What users do you want to add?: harry
What users do you want to add?:
before scriptblock
server3
tom
harry

Inside ScriptBlock
Outside ScriptBlock
ObjectClass Name PrincipalSource PSComputerName


Group BASEMENT\Domain Admins ActiveDirectory server3
User BASEMENT\Guest ActiveDirectory server3
User BASEMENT\SERVER2$ ActiveDirectory server3
User SERVER3\Administrator Local server3
server3
server3

:thinking:

$USERARRAY is the variable you define outside the script block and use inside of it. :man_shrugging:t4: :roll_eyes:

$Computer = Read-Host -Prompt "What Computer do you want to add users to local admin group on?"
$Userarray = @()
do {
    $User = Read-Host -Prompt "What users do you want to add?"
    if ($User -ne '') { $Userarray += $User }
}
until ($User -eq '')

Invoke-Command -ComputerName $Computer -ScriptBlock {
    Add-LocalGroupMember -Group Administrators -Member $USING:userarray
}

And BTW: it usually is a bad idea to accept free text input for such a use case. A better option would be to offer a limitted choice the user can choose from.

oh so you do not need the foreach then, it just feeds the entire array in.

Thanks!

I agree, params with validate set would be ideal.