I am attempting to query a domain controller using Get-ADComputer
for Active Directory information about a specific computer. This computer is specified by a variable in a local script. However, I can't pass a local variable to the scriptblock for Invoke-Command -ArgumentList
then into the -Filter{}
block in Get-ADComputer
. Below is what I thought would work:
$computername = "nicolas"
Invoke-Command -ComputerName DomainController -ScriptBlock {
Get-ADComputer -Filter {name -like "$args[0]"}
} -ArgumentList $computername
However, the $args[0]
variable does not pass through to the internal filter; it passes to the initial scriptblock, but no farther. It is defined inside of the first scriptblock (I can correctly return its value), but is not defined inside of the -Filter{}
scriptblock. How can I pass this variable from my local script, to the remote scriptblock, and into the -Filter{}
?
I've attempted $args[]
, $USING:computername
, but neither seem to let me pass it all the way through.