Passing PowerShell variables into a script block
Asked Answered
P

1

13

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

Polybius answered 11/8, 2011 at 8:36 Comment(0)
R
25

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 Motion\BlackBerry Enterprise Server Resource Kit\BlackBerry 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 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
Rarotonga answered 11/8, 2011 at 8:43 Comment(4)
Hi Shay,thanks for getting back to me I have tried both of your solutions and I get the same error "Invoke-Command : A positional parameter cannot be found that accepts argument 'Pa$$word'" Any ideas where I am going wrong? thanks again, ColmPolybius
Can you try with the call operator (e,g &): & BESUserAdminClient.exe -username $username -password $password -ad_auth -domain staging -b bbbes -u $u -change -wrandomRarotonga
Fixed! Thanks to Shay for pointing me in the right direction -username $args[0] -password $args[1] -ad_auth -domain staging -b bbbes -u $args[2] -change -wrandom } -argumentlist @($username, $password, $u) using $args and @() in the argument list has worked many thanksPolybius
I was experiencing the same exact problem. Using the @($var1, $var2) with the -ArgumentList option worked perfectly for me. Thanks guys.Zealand

© 2022 - 2024 — McMap. All rights reserved.