代码之家  ›  专栏  ›  技术社区  ›  Oskar

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

  •  0
  • Oskar  · 技术社区  · 5 年前

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

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

    然而,当我试图移动时 .net 变成这样的变量:

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

    或者甚至使用绝对路径 .net 这样地:

    $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 Files\dotnet\dotnet.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.
    

    不知道为什么 启动流程 无法找到该文件,尽管它确实存在于我的PATH中,甚至当我为cmdlt提供完整路径时也是如此。


    我的最终目标是能够指定对象中的参数,并将该对象传递给 启动流程 。这是在我的构建代理上运行的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
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   LogRhythm    5 年前

    根据启动流程的帮助文档

    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'。

        2
  •  1
  •   Oskar    5 年前

    正如@iRon在评论中指出的那样,问题是我没有使用 splatting 对的。我正在使用 $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