以下batch-file正在生成异常:
& : The term '.\Run-Regression.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:3
+ & '.\Run-Regression.ps1' -InputCSV '..\Desktop\tests\V10MWB.csv' -CAR ...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\Run-Regression.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
我在做什么错?和/或如何解决此问题?
我确实想保留相对路径,因为powershell具有其他依赖性。
@ECHO OFF
:: Enable PowerShell script execution
PowerShell Set-ExecutionPolicy Unrestricted
:: Navigate to the 'Common' directory (preserves relative paths)
PUSHD %~dp0\..\Common
:: Prepare the 'logs' directory
IF NOT EXIST ..\logs (MD ..\logs)
DEL /Q ..\logs\*.log 1>NUL 2>&1
:: Execute script
PowerShell "& 'Run-Regression.ps1' -InputCSV '..\Desktop\tests\%1.csv' -CARS_ID 0 -RunOnDesktop -Log -Email -Progress -Archive 2>&1" 1>"..\logs\%1.log";
:: Navigate back to original directory
POPD
根据错误消息,您在调用脚本时无法在当前目录中找到该脚本。更改为正确的目录,或使用完全限定的路径调用它。
使用相对路径可能会误解启动批处理文件的启动文件夹。如果批处理脚本和PowerShell脚本位于同一文件夹中,并且您不想关心启动文件夹,请尝试%~dp0
指令-它指向批处理文件所在的文件夹。例如,这将执行与bat \ cmd文件位于同一文件夹中的Run-Regression.ps1
脚本,而不考虑执行策略和启动文件夹。
PowerShell.exe -ExecutionPolicy Bypass -File %~dp0Run-Regression.ps1
您可以在此线程中找到更多有用的东西:What does %~dp0 mean, and how does it work?
您的错误消息与批处理文件中的实际调用命令不匹配:
PowerShell "& 'Run-Regression.ps1' ..." ...
失败,因为PowerShell设计不允许在PowerShell内部通过纯文件名运行可执行文件和脚本(无论是直接调用还是通过调用操作员&
调用。)>
相反,您必须在.\
之前添加前缀,以明确表示要从当前目录
目录中,但在批处理文件的目录中,请参见Vladimir Dronov's helpful answerPowerShell "& .\Run-Regression.ps1 ..." ...
如果您的
*.ps1
实际上不在当前
但是,请考虑使用-File
CLI参数而不是(隐含的)-Command
参数,在这种情况下,不需要.\
前缀,并且通常可以简化语法:
PowerShell -File Run-Regression.ps1 -InputCSV ..\Desktop\tests\%1.csv -CARS_ID 0 -RunOnDesktop -Log -Email -Progress -Archive 2>&1 1>"..\logs\%1.log";
注意:
使用-File
和-Command
之间有许多细微的区别;有关更多信息,请参见this answer。
PowerShell [Core],其可执行文件名为pwsh.exe
,默认为-File
,而不是-Command
,这是在类似Unix的平台上支持使用shebang lines的脚本所需的更改。
谢谢大家的回应,因为他们帮助我提出了以下解决方案,该解决方案非常适合我的情况: