【发布时间】:2017-06-26 23:47:32
【问题描述】:
如何将命令行参数传递给在 powershell 2.0 中使用“invoke-command”调用的会话
我的脚本:
param(
[string]$hostname = 'my_server_name'
)
function createSession($hostname){
return New-PSSession -ComputerName $hostname -Credential $env:UserDomain\$env:UserName
}
$session = createSession $hostname
invoke-command -Session $session -ScriptBlock {
write-host $hostname
write-host $using:hostname
write-host $script:hostname
write-host '**test text**'
}
Exit-PSSession
输出:(如果我直接打印参数值,我会得到空字符串。)
**test text**
【问题讨论】:
-
-ScriptBlock {} -ArgumentList $hostname -
另外,您可以从父会话中引用变量:
$using:hostname。我很确定这是 PSv2 友好的。 -
我试过 -ArgumentList 和 $using 选项。但他们都没有工作。
标签: powershell powershell-2.0 invoke-command