Execute .exe on different Server via NAV

naffnaff Member Posts: 32
edited 2015-08-28 in NAV Three Tier
Hi,

I need to establish some connection between NAV and some external application.
I can communicate with the external application with cmd-files that i pass as argument when starting the external apllication.
(The external application then will start, execute my command and close again.)

So far both apllications run on the same machine. The code below worked fine for this purpose.
(The external application creates a Status file after finishing execution, so there is some extra code for waiting for this file.)

Local Variables:
Process				DotNet	System.Diagnostics.Process.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
ProcessStartInfo	DotNet	System.Diagnostics.ProcessStartInfo.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
i						Integer
SendCmd(CmdFilePath : Text;ExportDir : Text):
ProcessStartInfo :=ProcessStartInfo.ProcessStartInfo;
ProcessStartInfo.FileName := 'C:\Test\MyApplication.exe';
ProcessStartInfo.Arguments := STRSUBSTNO('-cmd %1',CmdFilePath);
Process := Process.Process;
Process.Start(ProcessStartInfo);
WHILE i<100 DO BEGIN
  IF EXISTS(STRSUBSTNO('%1\Status.xml',ExportDir)) THEN
    EXIT;
  SLEEP(100);
  i += 1;
END;
ERROR('out of time');

Now I ran into a situation where the external application runs on a different server (same domain).
How can I modify this code to start the external application on the other machine.
For the files (cmd and status) I would create a shared folder so that both NAV and the external application have access to those.

Thanks in advance. :)

Answers

  • Rob_HansenRob_Hansen Member Posts: 296
    A couple options...

    1. Configure a NAV server and the NAS to run on the other server with a process running every minute (or some interval) to watch for a file to appear and (when it appears) to run the EXE

    2. If the NAV server simply can't run on the other server, you could write your own .NET application to run as a service and monitor the shared folder for a file to appear, and to then kick off the EXE whenever a file shows up
  • vaprogvaprog Member Posts: 1,140
    Some other options that come to mind:
    • try Sysinternal's PsExec
    • setup a minimalist web server with CGI
    • using eventcreate.exe locally and a scheduled task on the remote machine with event trigger
    • create some other process or service on the other machine that you can trigger to execute your command
  • naffnaff Member Posts: 32
    Thanks for your ideas.

    I think setting up a NAS or writing an .NET application is possible, but it would mean extra effort every time we implement this solution somewhere.

    I tried PsExec but our IT-Team suggested to use some PowerShell commands instead because it would be more secure in our scenario.
    So with their help I managed to create a small script that can connect to another server and run something there (in the example i create a folder instead of starting a .exe, but this should also work).
    $pwd = convertto-securestring "password" -asplaintext -force
    $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist "domain\user",$pwd
    Invoke-Command -ComputerName 123.45.67.89 -ScriptBlock { new-item -itemtype directory -path c:\temp\blablabla } -credential $cred
    

    This actually works if I run it manually. I probably also could run the script in C/AL using the WshShell automation.
    But I don't really want to have this script file somewhere on my server. I would have to copy it to every server where i want to implement this solution.
    So I was trying to use the System.Management.Automation.PowerShell class to write everyting in C/AL.
    But so far with no success. I am not able to execute any powershell commands. For the beginning i didn't even try to run it on another server - this should just create a folder on my machine.
    PowerShell	DotNet	System.Management.Automation.PowerShell.'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
    
    PowerShell := PowerShell.Create;
    PowerShell.AddScript('new-item -itemtype directory -path c:\temp\blablabla');
    PowerShell.BeginInvoke;
    
    MESSAGE('Done');
    

    Any ideas if this is possible and what I'm doing wrong?

    EDIT: I found a solution by Waldo. This seems to be a little bit too much for me but maybe I can extract what is neccessary for my small example.
    https://community.dynamics.com/nav/b/navericwauters/archive/2014/12/16/running-powershell-from-nav
Sign In or Register to comment.