kubusz
kubusz

Reputation: 983

Pass variables to the remotely imported powershell module

I am trying to create new PSSession, import ActiveDirectory module on the remote machine and then import-pssession to my local workstation - this works fine. The code looks like:

$rs = New-PSSession -ComputerName RemoteMachine
Invoke-Command -Session $rs -scriptblock {import-module ActiveDirectory}
Import-PSSession -Session $rs -Module Active Directory

And now I am able to call ActiveDirectory cmdlets, so e.g. Get-ADUser -Filter * works fine.

BUT

I am not able to pass variables to the ActiveDirectory cmdlets, I am not able to execute the following:

$name = 'John Smith'
Get-ADUser -Filter {name -eq $name}

It says $name is not defined. I cannot pass the variable to the Get-ADUser.

Any suggestions?

Thanks

Upvotes: 0

Views: 1816

Answers (1)

Shay Levy
Shay Levy

Reputation: 126702

I can't test it now but try to use double quotes instaed of a script block so the value of the variable can be expanded before it moves on to the target,

Get-ADUser -Filter "name -eq $name"

Upvotes: 1

Related Questions