使用路径作为参数从 CMD 调用 Powershell 脚本
Calling Powershell script from CMD with paths as parameters
我无法通过传递两个参数(路径)将此 cmd 命令发送到 运行 powershell 脚本。
C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Test\testing.ps1" \"\Data1\dataholder$\office\J Smith\backup\" \"\Data1\dataholder$\office\J Smith\backup2\"
我收到以下错误:
At line:1 char:36
+ ... ps\Scripts\Test\testing.ps1 "\Data1\dataholder$\office\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '\Data1\dataholder$\office' in expression or statement.
At line:1 char:146
+ ... Smith\backup" "\Data1\dataholder$\office\J Smith\backup2"
+ ~
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
知道为什么会失败吗?
我不得不修改通往脚本的路径。现在命令看起来像这样:
C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Power test\testing.ps1" \"\Data1\dataholder$\office\J Smith\backup\" \"\Data1\dataholder$\office\J Smith\backup2\"
这是由于脚本路径中的 space 而失败,知道如何处理这个问题吗?
您需要对要从 cmd shell 发送到 PowerShell 的引号进行反斜杠转义:
powershell -command "& \"C:\Apps\Scripts\Power test\testing.ps1\" \"\Data1\dataholder$\office\J Smith\backup\" \"\Data1\dataholder$\office\J Smith\backup2\""
如果您的程序或脚本文件有空格,您需要使用调用运算符 (&
) 调用它,并将其包含在发送到 -Command
的字符串中。注意映射到 -Command
.
的整个 value/string 周围的双引号
注意: 内部双引号在这里起作用,因为 $
后跟 \
。如果以 dataholder$folder
为例,则需要单引号或对 $
进行转义,因为 PowerShell 会尝试将 $folder
解释为变量。
这是反引号空格方法。
type "my script.ps1"
echo $args[0]
powershell .\my` script.ps1 hi` there
hi there
或单引号的战略位置。如果第一个字符不是引号,则可以执行某些操作。引号在反斜杠之前不起作用。
powershell .\'my script.ps1' 'hi there'
hi there
我无法通过传递两个参数(路径)将此 cmd 命令发送到 运行 powershell 脚本。
C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Test\testing.ps1" \"\Data1\dataholder$\office\J Smith\backup\" \"\Data1\dataholder$\office\J Smith\backup2\"
我收到以下错误:
At line:1 char:36
+ ... ps\Scripts\Test\testing.ps1 "\Data1\dataholder$\office\ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '\Data1\dataholder$\office' in expression or statement.
At line:1 char:146
+ ... Smith\backup" "\Data1\dataholder$\office\J Smith\backup2"
+ ~
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
知道为什么会失败吗?
我不得不修改通往脚本的路径。现在命令看起来像这样:
C:\WINDOWS\System32>powershell -command \"C:\Apps\Scripts\Power test\testing.ps1" \"\Data1\dataholder$\office\J Smith\backup\" \"\Data1\dataholder$\office\J Smith\backup2\"
这是由于脚本路径中的 space 而失败,知道如何处理这个问题吗?
您需要对要从 cmd shell 发送到 PowerShell 的引号进行反斜杠转义:
powershell -command "& \"C:\Apps\Scripts\Power test\testing.ps1\" \"\Data1\dataholder$\office\J Smith\backup\" \"\Data1\dataholder$\office\J Smith\backup2\""
如果您的程序或脚本文件有空格,您需要使用调用运算符 (&
) 调用它,并将其包含在发送到 -Command
的字符串中。注意映射到 -Command
.
注意: 内部双引号在这里起作用,因为 $
后跟 \
。如果以 dataholder$folder
为例,则需要单引号或对 $
进行转义,因为 PowerShell 会尝试将 $folder
解释为变量。
这是反引号空格方法。
type "my script.ps1"
echo $args[0]
powershell .\my` script.ps1 hi` there
hi there
或单引号的战略位置。如果第一个字符不是引号,则可以执行某些操作。引号在反斜杠之前不起作用。
powershell .\'my script.ps1' 'hi there'
hi there