Oskar
Oskar

Reputation: 2083

`Start-Process` can not find file that exists in PATH, even when given absolut path to the file

I am trying to use Start-Process in Powershell Core using a variable to specify what process to start. I know that dotnet is in my PATH so this works:

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

However, when I try to move dotnet into a variable like this:

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

or even using the absolute path to dotnet like this:

$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

I get an InvalidOperationException:

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

Not sure why Start-Process can not find the file despite that it do exists in my PATH or even when I give the cmdlt the full path.


My end-goal is to be able to specify the params in an object and just pass that object to Start-Process. This is part of a pwsh-script that runs on my build-agent to test a webjob-template. Though I want slightly different behaviors locally, see the switch $Azure below:

$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

Upvotes: 1

Views: 3221

Answers (2)

Oskar
Oskar

Reputation: 2083

As pointed out by @iRon in the comments, the issue is that I am not using splatting correct. I am using $StartProcessParams instead of @StartProcessParams (the difference is the first char; $ vs @). This works just fine:

$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

Upvotes: 1

LogRhythm
LogRhythm

Reputation: 128

According to the help documentation for Start-Process

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."

Try the following command:

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

Your issue may be that it is trying to resolve the variable content 'dotnet' from your current directory, rather than your PATH location.

Upvotes: 1

Related Questions