将 Powershell 变量传递到脚本块中 [英] Passing Powershell variables into a scriptblock

查看:25
本文介绍了将 Powershell 变量传递到脚本块中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 powershell 变量并将它们应用于脚本块.

I am trying to take powershell variables and apply them to a scriptblock.

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 MotionBlackBerry Enterprise Server Resource KitBlackBerry 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

.RandomActivationEmail.ps1 -username besadmin -password Pa$$word -u bb.user

我得到的错误是:-

Invoke-Command:找不到接受参数Pa$$word"的位置参数.在 C:ScriptsbRandomActivationEmail.ps1:12 字符:15+调用命令<<<<<<-Session $s -Scriptblock {cd "C:Program Files (x86)Research In MotionBlackBerry Enterprise Server Resource KitBlackBerry Enterprise Server 用户管理工具客户端"+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException+ FullQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

Invoke-Command : A positional parameter cannot be found that accepts argument 'Pa$$word'. At C:ScriptsbRandomActivationEmail.ps1:12 char:15 + Invoke-Command <<<< -Session $s -Scriptblock {cd "C:Program Files (x86)Research In MotionBlackBerry Enterprise Se rver Resource KitBlackBerry Enterprise Server User Administration Tool Client" + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

感谢阅读,Colm 加油.

Thanks for reading, cheers Colm.

推荐答案

您可以通过 -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:

Invoke-Command -Session $s -Scriptblock {
    cd "C:Program Files (x86)Research In MotionBlackBerry Enterprise Server Resource KitBlackBerry Enterprise Server User Administration Tool Client"
    ./BESUserAdminClient -username $args[0] -password $args[1] -ad_auth -domain staging -b bbbes -u $args[2] -change -wrandom
} -argumentlist $username $password $u

或者在脚本块内定义参数并使用命名参数:

Or define the parameters inside the script block and use named parameters:

Invoke-Command -Session $s -Scriptblock {
    param(
        $username,$password,$u
    )

    cd "C:Program Files (x86)Research In MotionBlackBerry Enterprise Server Resource KitBlackBerry 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

这篇关于将 Powershell 变量传递到脚本块中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆