0

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?

4
  • What log would you expect to be created, and where? Are you sure that the logfile isn't just created in a different place? Did you try Set-Location "C:\some\folder" before running the executable? Did you try the call operator instead of Start-Process? Commented May 2, 2017 at 19:41
  • The exe has a configuration (uses log4Net to log) to log. It currently logs in the same directory as where the exe is. I haven't tried that. I will try that shortly. I assumed that the exe is running in the directory (on the server) where it is hosted. Is this not the case? (I am able to echo a file to the directory, so I know that this is not a permissions issue. PS C:\Windows\system32> Invoke-Command -ComputerName myServer -ScriptBlock { echo test > C:\Playground\log\test.tx }) Commented May 2, 2017 at 19:53
  • $PSScriptRoot and $PWD.Path aren't necessarily the same, no. Commented May 2, 2017 at 20:11
  • Thank you!! This was the issue. I was able to fix as such. Set-Location \\MyServer\c$\MyDirectory Invoke-Command –ComputerName MyServer –ScriptBlock { & "c:\MyDirectory\MyApp.Console.exe" } Commented May 3, 2017 at 20:35

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. I added a comment to Ansgar. His comment was the solution, but marking yours as such, as his is just a comment which I can't mark as answer. :)
0

Change the working directory to the location of your executable before running it. I would also recommend using the call operator (&) instead of Start-Process, unless you have reason to use the latter.

Invoke-Command –ComputerName MyServer –ScriptBlock {
  Set-Location 'C:\MyDirectory'
  & 'MyApp.Console.exe'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.