我试图在一个PowerShell脚本中运行一个CMD任务,该脚本将在Google中打开一个新的选项卡。我正在Azure DevOps管道中运行这个命令。
我试图运行的任务是:
start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)"当我从本地命令提示符运行此命令时,将打开一个新的选项卡,并按预期的方式工作。要从我的PowerShell窗口运行它,我运行:
cmd /c echo start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)" | cmd.exe但是,上述两个命令都可以正常工作,试图从Azure DevOps运行它们--我收到了一个错误消息:
+ ... " --disable-default-apps --new-window "$($reportHtmlFile)"" | cmd.exe
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At D:\Agent\instance01\Workspace\20\s\pbi-load-test-tool\Run_Load_Test_Only.ps1:59 char:1
+
Missing closing ')' in expression.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString我尝试了以下几点:
"start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)"" | cmd.exe
& "start chrome --user-data-dir="ChromeProfiles\Profile$profile" --disable-default-apps --new-window "$($reportHtmlFile)" | cmd.exe"是语法错误还是Azure DevOps的缺点?
发布于 2022-09-20 18:48:27
怎么回事:
你的论点被搞砸了,因为它们包含了引号。
当您以现在运行它的方式运行它时,PowerShell将尝试在将其传递给exe之前处理参数中的引号。
解决方案:使用飞溅
Splatting是PowerShell语言的一部分,它允许将结构化参数传递给命令。您可以使用[Hashtable]来提供命名参数(如果您正在调用函数或cmdlet)。您还可以使用[Object[]]来提供位置上的参数。
就你而言,这将是:
$startArgs = @(
# The user-data-dir probably wants double quotes
# so we use backticks to embed them.
"--user-data-dir=`"ChromeProfiles\Profile$profile`""
'--disable-default-apps'
'--new-window'
"$($reportHtmlFile)"
)start @startArgs这样做将确保每个参数按照您希望的方式发送,并完全控制参数的引用方式。
另外:避免使用$profile
$profile是PowerShell中指向PowerShell配置文件的自动变量的名称。我怀疑您对铬配置文件感兴趣,而不是对PowerShell配置文件感兴趣,所以我会选择一个不同的变量名来更好地描述您的用途,而不是冒险让自动变量提供糟糕的结果。
https://stackoverflow.com/questions/73791154
复制相似问题