将 PowerShell 变量传递到脚本块
我正在尝试获取 PowerShell 变量并将它们应用到脚本块。
param(
[string]$username = $(throw "Blackberry Admin User Name is required"),
[string]$password = $(throw "Blackberry Admin Password is required"),
[string]$u = $(throw "Blackberry User Name is required")
)
$s = New-PSSession -computerName bbbes01
Invoke-Command -Session $s -Scriptblock {cd "C:\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server Resource Kit\BlackBerry Enterprise Server User Administration Tool Client"
./BESUserAdminClient -username $username -password $password -ad_auth -domain staging -b bbbes -u $u -change -wrandom} -argumentlist $username $password $u
我正在运行
.\RandomActivationEmail.ps1 -username besadmin -password Pa$$word -u bb.user
我收到的错误是:
Invoke-Command:找不到接受参数“Pa$$word”的位置参数。 在 C:\Scripts\bb\RandomActivationEmail.ps1:12 字符:15
+ 调用命令 <<<<< -Session $s -Scriptblock {cd "C:\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server Resource Kit\BlackBerry Enterprise Server User Administration Tool Client"
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command]、ParameterBindingException
+ FullQualifiedErrorId:PositionalParameterNotFound、Microsoft.PowerShell.Commands.InvokeCommandCommand
I am trying to take PowerShell variables and apply them to a script block.
param(
[string]$username = $(throw "Blackberry Admin User Name is required"),
[string]$password = $(throw "Blackberry Admin Password is required"),
[string]$u = $(throw "Blackberry User Name is required")
)
$s = New-PSSession -computerName bbbes01
Invoke-Command -Session $s -Scriptblock {cd "C:\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server Resource Kit\BlackBerry Enterprise Server User Administration Tool Client"
./BESUserAdminClient -username $username -password $password -ad_auth -domain staging -b bbbes -u $u -change -wrandom} -argumentlist $username $password $u
I am running
.\RandomActivationEmail.ps1 -username besadmin -password Pa$word -u bb.user
The error that I am getting is:
Invoke-Command : A positional parameter cannot be found that accepts argument 'Pa$$word'.
At C:\Scripts\bb\RandomActivationEmail.ps1:12 char:15
+ Invoke-Command <<<< -Session $s -Scriptblock {cd "C:\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server Resource Kit\BlackBerry Enterprise Server User Administration Tool Client"
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过 -arguments 参数传递值,并在脚本块内将它们引用为 $args[0] 等:
或者在脚本块内定义参数并使用命名参数:
You can pass values via the -arguments parameter and refer to them as $args[0] and so on inside the script block:
Or define the parameters inside the script block and use named parameters: