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

梦想与她 提交于 2020-01-07 04:01:28

问题


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?


回答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.




回答2:


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


来源:https://stackoverflow.com/questions/43744850/powershell-invoke-command-using-start-process-is-not-creating-log-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!