Add-LocalGroupMember on a Remote Computer

Please, i am trying to add users to a local group on a remote desktop by Using the cmdlet Add-LocalGroupMember. Because Add-LocalGroupMember does not have a parameter for “computerName” i decide to use the invoke-command cmdlet.

Below is my script code:

function Enable-UCRemoteLoggeOnUser{
[cmdletBinding ()]
Param(
[Parameter ()]
[validateScript( {Test-Connection -ComputerName $_ -Quiet -Count 1} )]
[validateNotNullorEmpty ()]
[String[]]$ComputerName = $env:COMPUTERNAME,
[string[]]$GroupName,
[String[]]$UserName
)
foreach ($comp in $computerName) {
Invoke-Command -ComputerName $comp -ScriptBlock {Add-LocalGroupMember -Group $GroupName -Member $UserName} -ArgumentList $GroupName, $UserName
}
}
Below is the error message encountered:
Cannot validate argument on parameter 'Group'. The argument is null. Provide a valid value for the argument, and then try running the command again. + CategoryInfo : InvalidData: (:) [Add-LocalGroupMember], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand + PSComputerName : NGMXL9293LFM
Observation: My $groupname and $username is null, meaning attribute pass to the parameter does not applied to my parameter in the scripblock of my invoke-command.
How could i resolve this issue.
Thanks.

Your issue is about scope. The inside of the scriptblock does not know about the variables existing outside of the scriptblock. The simplest solution would be to use the scope modifier USING. … like this …

function Enable-UCRemoteLoggeOnUser{
    [cmdletBinding ()]
    Param(
        [Parameter ()]
        [validateScript( {Test-Connection -ComputerName $_  -Quiet -Count 1} )]
        [validateNotNullorEmpty ()]
        [String[]]$ComputerName = $env:COMPUTERNAME,
        [string[]]$GroupName,
        [String[]]$UserName
    )
    foreach ($comp in $computerName) {
        Invoke-Command -ComputerName $comp -ScriptBlock {Add-LocalGroupMember -Group $USING:GroupName -Member $USING:UserName} 
        }
}

Please, when you post code or error messages or sample data or console output you should fomrat it as code using the code tags “PRE”. Thanks.
Read Me Before Posting! You’ll be Glad You Did!

BTW: When you carefully (re-)read the help for Invoke-Command you will notice that this cmdlet can take an array of computernames for the parameter -ComputerName. So you don’t need a loop to remote to more than one computer. :wink:

Thanks for your assistance and clarification on the use of invoke-command. This has really increase my level of understanding. Below is the final script code. Necessary adjustment or enhancement will be really appreciated.

 

function Enable-UCRemoteLoggeOnUser{
<#
.DESCRIPTION
Enable a user to remotely connect to his computer.

.EXAMPLE
Enable-UCRemoteLoggeOnUser -ComputerName NGMXL9293LFM -GroupName “Remote Desktop Users” -UserName “JBethelite”

#>
[cmdletBinding ()]
Param(
[Parameter ()]
[validateScript( {Test-Connection -ComputerName $_ -Quiet -Count 1} )]
[validateNotNullorEmpty ()]
[String[]]$ComputerName = $env:COMPUTERNAME,
[Microsoft.PowerShell.Commands.LocalGroup]$GroupName,
[String[]]$UserName
)

Invoke-Command -ComputerName $ComputerName -ScriptBlock {Add-LocalGroupMember -Group $Using:GroupName -Member $Using:UserName -Verbose} -ArgumentList $GroupName, $UserName

Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-LocalGroupMember -Group $Using:GroupName -Verbose} -ArgumentList $GroupName, $UserName

}