Search code examples
powershellinvoke-command

PowerShell Invoke-Command using Start-Process is not creating log file


I have a command line exe on a remote server which I need to execute remotely (running on the remote server). This exe connects to the DB, and writes a log file.

I have enabled WinRM by executing these PowerShell commands:

  1. netsh http add iplisten localip {add local ip here}
  2. netsh http add iplisten 127.0.0.1
  3. Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force
  4. Enable-PSRemoting -Force

This works and I was able to test by executing

Test-WSMan {computername}

I am now trying to execute the command line exe via Invoke-Command and expecting a log file entry to be created with this script.

Invoke-Command –ComputerName MyServer –ScriptBlock {
  Start-Process "C:\TheRemotePath\ToTheExe.exe"
}

PowerShell does not display any errors. However, no log file is generated. I have full rights to the directory of the exe as well as the log file directory. I am able to successfully run the exe via Windows Explorer (navigate to the remote exe and double click to run).

Any idea why this does not seem to work or what tools I can use to try and diagnose?


Solution

  • I would try testing the path and switching to the call operator as Ansgar mentioned, i.e.:

    Invoke-Command –ComputerName MyServer –ScriptBlock {
        $path = "C:\TheRemotePath\ToTheExe.exe"
        if (Test-Path $path) {
            Write-Host -ForegroundColor Green "Confirmed access to $path!"
            & $path
        }
        else {
            Write-Host -ForegroundColor Yellow "Unable to reach $path!"
        }
    }
    

    Doing those will help you diagnose where it's getting tripped up. Depending on the EXE, I've had to use different methods of starting the process from Powershell.