Passing Powershell variables into a scriptblock Passing Powershell variables into a scriptblock powershell powershell

Passing Powershell variables into a scriptblock


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