简体   繁体   English

如何使用 powershell 中的变量调用命令?

[英]How do I invoke a command using a variable in powershell?

I want to invoke the shutdown.exe executable in powershell and it is located on C:\WINDOWS\System32\shutdown.exe .我想调用 powershell 中的 shutdown.exe 可执行文件,它位于C:\WINDOWS\System32\shutdown.exe上。

Since we already got the C:\WINDOWS in the variable $env:windir I want to just use it and concatenate the rest of the path into the command.由于我们已经在变量$env:windir中获得了C:\WINDOWS我只想使用它并将路径的 rest 连接到命令中。 I was trying this:我正在尝试这个:

PS C:\Users\shina> .\$env:windir\System32\shutdown.exe -s

But I got the following error:但我收到以下错误:

    .\$env:windir\System32\shutdown.exe: The term '.\$env:windir\System32\shutdown.exe' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

How can I accomplish this?我怎样才能做到这一点?

You can use:您可以使用:

& $env:windir\System32\shutdown.exe -s

Or或者

. $env:windir\System32\shutdown.exe -s

(note the space). (注意空格)。


& is the call operator. &是呼叫运算符。 You can read more about it here :你可以在这里阅读更多关于它的信息:

Runs a command, script, or script block.运行命令、脚本或脚本块。 The call operator, also known as the "invocation operator", lets you run commands that are stored in variables and represented by strings or script blocks.调用运算符,也称为“调用运算符”,可让您运行存储在变量中并由字符串或脚本块表示的命令。


. is the dot sourcing operator.是点源运营商。 You can read more about it here :你可以在这里阅读更多关于它的信息:

Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope, overriding existing ones.在当前 scope 中运行脚本,以便将脚本创建的任何函数、别名和变量添加到当前 scope 中,覆盖现有的。

The dot sourcing operator is followed by a space.点源运算符后跟一个空格。 Use the space to distinguish the dot from the dot (.) symbol that represents the current directory.使用空格将点与表示当前目录的点 (.) 符号区分开来。

暂无
暂无

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

相关问题 使用 PowerShell 的 Invoke-Command 进行远程处理时,如何包含本地定义的函数? - How do I include a locally defined function when using PowerShell's Invoke-Command for remoting? 如何使用 Powershell 链接调用命令? - How do I use Powershell to chain Invoke Command? 在Powershell中使用Invoke-Command时,如何在本地显示远程exe的输出? - How do I display the output of a remote exe locally when using Invoke-Command in Powershell? 如何在 Powershell 脚本的非 Powershell 命令中使用此变量? - How do I use this variable in a non Powershell Command in a Powershell Script? 如何使用 powershell 中的调用命令删除文件夹 - how to delete a folder using invoke command in powershell 如何使用 Invoke-RestMethod 从 Powershell 发布 - How do I POST from Powershell using Invoke-RestMethod Powershell $using 变量不能与脚本块中的 Invoke-Command 一起使用 - Powershell $using variable not working with Invoke-Command in scriptblock 如何将命令 output 保存到变量中,以及如何将该变量用作 Powershell 中的命令参数? - How do I save a command output to a variable, and how do I use that variable as command parameter in Powershell? 如何在powershell中使用invoke-command中的变量? - how to use variable out of invoke-command in powershell? 如果命令行参数在Powershell中“存在”,如何设置变量? - How do I set a variable if a command line parameter is “present” in powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM