Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

blackberry - Passing Powershell variables into a scriptblock

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

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: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

Thanks for reading, cheers Colm.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...