且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 Powershell 变量传递到脚本块中

更新时间:2023-01-08 18:40:15

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