简体   繁体   English

如何在PowerShell中将参数传递给嵌入式脚本?

[英]How to pass arguments to an inline script in PowerShell?

I'm new to PowerShell scripting. 我是PowerShell脚本的新手。 I was wondering if there was a way to invoke a script inline without creating a new .ps1 file and pass arguments to it, eg 我想知道是否有一种方法可以直接调用脚本而不创建新的.ps1文件并将参数传递给它,例如

powershell -command "echo `$args[0]" 123 456

I want echo $args[0] to be the script and 123 456 to be the arguments, but PowerShell seems to interpret 123 456 as part of the script (as if I'd wrote "echo `$args[0] 123 456" ) and prints 我希望echo $args[0]作为脚本,而123 456作为参数,但是PowerShell似乎将123 456解释为脚本的一部分(好像我写了"echo `$args[0] 123 456" )并打印

123
456

Is it possible to work around this without creating a new PowerShell script on disk? 是否可以解决此问题而无需在磁盘上创建新的PowerShell脚本?

So far you have seen a couple answers and comments showing you how to do just what you asked for. 到目前为止,您已经看到了一些答案和评论,向您展示了如何按照您的要求去做。 But I have to echo @BillStewart's comment: what is your true purpose? 但是我必须回应@BillStewart的评论:您的真正目的是什么?

If it is to be able to execute some arbitrary PowerShell that you have in a string, you could certainly invoke powershell.exe, as you have seen in a couple answers and comments. 如果要能够执行字符串中包含的任意PowerShell,您肯定可以调用powershell.exe,就像在几个答案和注释中看到的那样。 However, if you do that be sure to include the -noprofile parameter for improved efficiency. 但是,如果这样做,请确保包括-noprofile参数以提高效率。 And for even more efficiency, you could use Invoke-Command without having to spin up a whole separate PowerShell environment, eg 为了获得更高的效率,您可以使用Invoke-Command而不必启动整个单独的PowerShell环境,例如

Invoke-Command -ScriptBlock ([Scriptblock]::Create('echo $args[0]')) -arg 123, 456

If, on the other hand, you want to execute a chunk of PowerShell ( not within a string but just separate in your script for whatever reason), you can simplify to this: 另一方面,如果您想执行大块的PowerShell( 不在字符串中,而是出于某种原因而在脚本中单独执行),则可以简化为:

Invoke-Command -ScriptBlock {echo $args[0]} -arg 123, 456

Whenever you have the choice though, avoid manipulating strings as executable code (in any language)! 不过,只要有选择,就避免将字符串作为可执行代码 (任何语言)进行处理! The oft-used way to do that in Powershell is with Invoke-Expression , which I mention only to point you to the enlightening article Invoke-Expression considered harmful . 在Powershell中执行此操作的常用方法是Invoke-Expression ,我只想向您指出启发性的文章Invoke-Expression被认为是有害的

This does what I think you want: 这符合我的要求:

powershell -command "& {write-host `$args[0] - `$args[1] - `$args[2]}" 123 456 789

Each element of the $args array is a different parameter so to access three parameters you need $args[0], $args[1], and $args[2]. $ args数组的每个元素都是一个不同的参数,因此要访问三个参数,您需要$ args [0],$ args [1]和$ args [2]。

You could also use the param statement and get named parameters: 您还可以使用param语句并获取命名参数:

powershell -command "& {param(`$x,`$y); write-host `$x - `$y}" -x 123 -y 456

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 arguments 传递给远程 powershell 脚本? - How to pass arguments to a remote powershell script? 如何将带点的批处理参数传递给powershell脚本? - How can I pass batch arguments with dots into a powershell script? 如何在不同的进程中运行PowerShell脚本并将参数传递给它? - How to run PowerShell script in a different process and pass arguments to it? 如何将命令行 arguments 传递给由 doskey 调用的 PowerShell 脚本 - How to pass command line arguments to a PowerShell script invoked by a doskey 如何将参数或变量从一个 powershell 脚本传递到另一个? - How do I pass arguments or variables from one powershell script to another? 如何通过Powershell启动过程将带引号的参数传递给提升的批处理脚本 - How to pass quoted arguments to elevated batch script via powershell Start-Process 在Powershell中执行环境变量时如何传递参数? - How to pass arguments when executing environment variable in Powershell? powershell传递命令行参数 - powershell pass command line arguments 如何在PowerShell中将参数传递给类似Unix的命令(通过MinGW) - How to pass arguments to Unix-like commands (via MinGW) in PowerShell 如何以PowerShell中的参数作为管理员运行python脚本 - How run python script with arguments from powershell as admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM