Reputation: 1352
I runing the following simple powershell command on a remote server. However I need to pass a variable to the NET LOCALGROUP
command:
$serverName = "SCO32"
$groupName = "SCO33_Local_Admins"
$Session = New-PSSession -ComputerName $serverName
Invoke-Command -Session $Session -ScriptBlock {
$args[1]
$args[0]
net localgroup administrators domainname\$args[1] /ADD
} -ArgumentList $serverName, $groupName
The arguments are passing correctly as is the remote connection, it just doesn't seem to be able to execute the command because it's trying to use the $args[1] as a literal and not domainname\SCO33_Local_Admins
Thanks in advance.
Upvotes: 0
Views: 346
Reputation: 2272
$servername = 'sv1'
In v2:
Invoke-Command -Session $Session -ScriptBlock {
param($servername, $group)
net localgroup administrators domainname\$servername /ADD
} -ArgumentList $serverName, $groupName
Or in v3
Invoke-Command -Session $Session -ScriptBlock {
net localgroup administrators domainname\${using:servername} /ADD
}
Or:
Invoke-Command -Session $Session -ScriptBlock {
net localgroup administrators domainname\$($args[1]) /ADD
} -ArgumentList $serverName, $groupName
Upvotes: 2
Reputation: 36277
Just like in a function or in a script you can assign parameters to a scriptblock. While using the automatic $args may not work for you, you can do this:
$serverName = "SCO32"
$groupName = "SCO33_Local_Admins"
$Session = New-PSSession -ComputerName $serverName
Invoke-Command -Session $Session -ScriptBlock {
Param($SrvName,$GrpName)
net localgroup administrators domainname\$GrpName /ADD
} -ArgumentList $serverName, $groupName
Upvotes: 0