"启动进程"找不到 PATH 中存在的文件,即使给定了文件的绝对路径也是如此



我正在尝试使用变量在Powershell Core中使用Start-Process来指定要启动的进程。我知道dotnet在我的路径中,所以这有效:

$DotnetRunCommandApp = 'run --project path/to/my/project.csproj'
Start-Process dotnet -ArgumentList $DotnetRunCommandApp

但是,当我尝试将dotnet移动到这样的变量中时:

$DotnetCommand = 'dotnet'
$DotnetRunCommandApp = 'run --project path/to/my/project.csproj'
Start-Process $DotnetCommand -ArgumentList $DotnetRunCommandApp

甚至使用绝对路径来dotnet如下:

$DotnetCommand = Resolve-Path ((Get-Command dotnet).Source | Out-String -NoNewline)
if (-not (Test-Path $DotnetCommand)) {
Write-Error "Can not find '$DotnetCommand'"
} else {
Write-Debug "Found $DotnetCommand" # Logs "DEBUG: Found C:Program Filesdotnetdotnet.exe"
}
$DotnetRunCommandApp = 'run --project path/to/my/project.csproj'
Start-Process $DotnetCommand -ArgumentList $DotnetRunCommandApp

我得到一个InvalidOperationException

This command cannot be run due to the error: The system cannot find the file specified.

不知道为什么Start-Process找不到该文件,尽管它确实存在于我的 PATH 中,甚至当我为 cmdlt 提供完整路径时。


我的最终目标是能够在对象中指定参数并将该对象传递给Start-Process。这是在我的构建代理上运行的 pwsh 脚本的一部分,用于测试 Web 作业模板。虽然我希望本地的行为略有不同,但请参阅下面的开关$Azure

$StartProcessParams = @{
FilePath               = $DotnetCommand
ArgumentList           = $DotnetRunCommandApp
RedirectStandardError  = (Resolve-Path $WebJobErrorLogFile)
RedirectStandardOutput = (Resolve-Path $WebJobLogFile)
PassThru               = $true;
# Logging works best if we keep the process in the same "window" on Azure. Locally let the
# WebJob run in a new windows to make it really easy to kill the process in case of any errors
NoNewWindow            = $Azure;
}
$WebJobProcess = Start-Process $StartProcessParams

根据启动过程的帮助文档

If you specify only a filename, use the WorkingDirectory parameter to specify the path."
The WorkingDirectory Paramter "specifies the location of the executable file or document that runs in the process. The default is the current folder."

尝试以下命令:

Start-Process $DotnetCommand -ArgumentList $DotnetRunCommandApp -WorkingDirectory </dir/to/PATH>

您的问题可能是它正在尝试从当前目录而不是 PATH 位置解析变量内容"dotnet"。

正如@iRon在评论中指出的那样,问题是我没有正确使用喷塑。我使用的是$StartProcessParams而不是@StartProcessParams(区别在于第一个字符;$vs@(。这工作得很好:

$StartProcessParams = @{
FilePath               = $DotnetCommand
ArgumentList           = $DotnetRunCommandApp
RedirectStandardError  = (Resolve-Path $WebJobErrorLogFile)
RedirectStandardOutput = (Resolve-Path $WebJobLogFile)
PassThru               = $true;
# Logging works best if we keep the process in the same "window" on Azure. Locally let the
# WebJob run in a new windows to make it really easy to kill the process in case of any errors
NoNewWindow            = $Azure;
}
$WebJobProcess = Start-Process @StartProcessParams

相关内容

最新更新