如何在 PowerShell 中将参数传递给调用表达式? [英] How to pass arguments to an Invoke-Expression, in PowerShell?

查看:46
本文介绍了如何在 PowerShell 中将参数传递给调用表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(不工作)PowerShell 脚本:

I have the following (not working) PowerShell script:

$scriptPath = ((new-object net.webclient).DownloadString('https://gist.githubus
ercontent.com/AndrewSav/c4fb71ae1b379901ad90/raw/23f2d8d5fb8c9c50342ac431cc0360ce44465308/SO33205298')); $args = "`"aaa
bbb`"";  iex $scriptPath $args

所以我:

  1. 下载要执行的脚本.
  2. 设置我的参数列表以发送到脚本中
  3. 执行脚本(对我来说,这现在在 cli 中).

但它是错误的:

Invoke-Expression : A positional parameter cannot be found that accepts argument '"aaa bbb"'.
At line:1 char:209
+ ... 44465308/SO33205298')); $args = "`"aaa bbb`"";  iex $scriptPath $args
+                                                     ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Expression], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand

如何将参数传递给这个脚本?

How can I pass in the args to this script?

注意:这个问题引用/衍生自 这个其他问题.

Note: this question references/spawned from this other SO question.

推荐答案

你应该尝试这样的事情:

You should try something like this :

$scriptPath = ((new-object net.webclient).DownloadString('https://gist.githubusercontent.com/AndrewSav/c4fb71ae1b379901ad90/raw/23f2d8d5fb8c9c50342ac431cc0360ce44465308/SO33205298'))
Invoke-Command -ScriptBlock ([scriptblock]::Create($scriptPath)) -ArgumentList "coucou"

在调用之前,您必须从源创建一个 ScriptBlock.

You have to create a ScriptBlock from source before invoking it.

这篇关于如何在 PowerShell 中将参数传递给调用表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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