powershell - 关于powershell命令执行的问题

标签 powershell

有人可以帮我解释一下这里发生了什么吗?抱歉,如果这是一个基本问题, 我从我正在尝试编写的管道表达式中简化了它:

$foo = pwd
$cmd = "dir"

& $cmd $foo #Works

dir $foo  #Works

& "dir $foo" #Error

*术语“dir C:\”未被识别为 cmdlet、函数、可运行程序或脚本文件。验证术语并重试。

在行:1 字符:2 + & <<<< "dir $foo"*

dir pwd #Error

*Get-ChildItem:找不到路径“C:\pwd”,因为它不存在。

第 1 行字符:4 + 目录 <<<< 密码*

我希望所有这四个都产生相同的结果

最佳答案

引号中的表达式被解释为单个参数。在第三条命令中,shell 将其解释为对不带参数的命令“dir C:\”的请求,而不是对命令“dir”的请求参数为“C:\”。

关于powershell - 关于powershell命令执行的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1412733/

相关文章:

azure - 通过Az模块创建azure应用程序并使用powershell分配API权限

winforms - DropDownList 的默认文本

powershell - 使用 PowerShell 进行 Azure 部署, "New-AzureDeployment : There was no endpoint listening at https://management.core.windows.net/..."

powershell - 在PowerShell中替换字符串: “-”

powershell - 如何分割包含换行符的字符串

powershell - 从分隔的文本文件中删除标题

powershell - 根据应用程序用户名检测应用程序关闭

powershell - 通过启动进程以管理员身份从 powershell 脚本重定向 stdout、stderr

Powershell CSV 变量到 Out-GridView

Powershell 脚本参数 : display help on incorrect usage?