传递PowerShell的变量为一个脚本块 [英] Passing Powershell variables into a scriptblock

查看:169
本文介绍了传递PowerShell的变量为一个脚本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把PowerShell的变量,并将它们应用到脚本块。

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

我正在

\\ RandomActivati​​onEmail.ps1 -username besadmin -password霸$$字-u bb.user

.\RandomActivationEmail.ps1 -username besadmin -password Pa$$word -u bb.user

这是我收到的错误是: -

The error that I am getting is:-

调用命令:位置参数无法找到接受的说法霸$$字。
  在C:\\脚本\\ BB \\ RandomActivati​​onEmail.ps1:12字符:15
  +调用-命令<<<< -session $ S -ScriptBlock {CDC:\\ Program Files文件(x86)的\\ Research In Motion公司\\黑莓企业硒
  rver资源工具包\\ BlackBerry Enterprise Server的用户管理工具客户端
      + CategoryInfo:InvalidArgument:(:) [调用命令],ParameterBindingException
      + FullyQualifiedErrorId:PositionalParameterNotFound,Microsoft.P​​owerShell.Commands.InvokeCommandCommand

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 Se rver Resource Kit\BlackBerry Enterprise Server User Administration Tool Client" + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

感谢您的阅读,欢呼声科尔姆。

Thanks for reading, cheers Colm.

推荐答案

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

这篇关于传递PowerShell的变量为一个脚本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆