将变量传递给远程导入的powershell模块
我正在尝试创建新的 PSSession,在远程计算机上导入 ActiveDirectory 模块,然后将 pssession 导入到我的本地工作站 - 这工作正常。代码如下所示:
$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
。
有什么建议吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在无法测试它,但尝试使用双引号代替脚本块,以便变量的值可以在移动到目标之前扩展,
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,