0

我想創建新的PSSession,在遠程機器上導入ActiveDirectory模塊,然後將import-pssession導入到本地工作站 - 這很好。代碼如下所示:將變量傳遞到遠程導入的powershell模塊

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

現在我可以調用ActiveDirectory cmdlet,例如, Get-ADUser -Filter *工作正常。

我不能將變量傳遞到ActiveDirectory中的cmdlet,我不能執行以下操作:

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

它說沒有定義$name。我無法將變量傳遞給Get-ADUser

有什麼建議嗎?

感謝

+1

嘗試:http://get-powershell.com/post/2008/12/14/Passing-Variables-to-a-Remote-PSSession-in-CTP-3.aspx –

+0

讓我知道!投票我的評論,如果它的工作! –

+0

謝謝,我試了下面:'調用命令-Session $ rs -ArgumentList $ name -ScriptBlock {參數($ name)Get-ADUser -Filter {name -eq $ name}}'這工作正常。但是我仍然在想,爲什麼我不能直接將變量傳遞給'Get-ADUser' cmdlet?爲什麼我必須用'invoke-command'來做這件事? – kubusz

回答

1

現在我無法測試,但嘗試移動到目標之前用雙引號instaed一個腳本塊,因此變量的值,可擴展,

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