Passing Powershell variables into a scriptblock

后端 未结 1 1313
后悔当初
后悔当初 2020-12-01 23:47

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

param(
    [string]$username = $(throw \"Blackberry Admin User Name is required\")         


        
相关标签:
1条回答
  • 2020-12-02 00:27

    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
    
    0 讨论(0)
提交回复
热议问题