Jump to content

- Functions

- # LaunchCmdEx

  Followers 0

  VBScript

  This function calls (launches) a given command, i.e. launches any given application. It offers to write the output of the command (StdOut and StdErr) into the server trace file for later analysis.

  Please see the Introduction chapter for some usage instructions.

      '----------------------------------------------------------------
      ' LaunchCmdEx
      '
      ' Executes the given command and also offers to writes its output into the
      ' server trace.
      '
      ' Parameter:
      '   sCommand            command to be called
      '   bTraceStdOut        trace StdOut of the called program to server trace
      '   bTraceStdErr        trace StdErr of the called program to server trace
      '
      ' Return:
      '   boolean             true - command successfully executed
      '----------------------------------------------------------------
      Function LaunchCmdEx( sCommand, bTraceStdOut, bTraceStdErr )
          PBXScript.OutputTrace "------> LaunchCmdEx"
          PBXScript.OutputTrace "sCommand = " & sCommand
          PBXScript.OutputTrace "bTraceStdOut = " & CStr(bTraceStdOut)
          PBXScript.OutputTrace "bTraceStdErr = " & CStr(bTraceStdErr)

          On Error Resume Next

          Dim bReturn
          bReturn = True

          Dim objWsh, objWshScriptExec
          Set objWsh = CreateObject("Wscript.Shell")
          Set objWshScriptExec = objWsh.Exec(sCommand)

          if Err <> 0 then
              PBXScript.OutputTrace "Error executing command!"
              PBXScript.OutputTrace Err & ": " & Err.Description
              bReturn = False
          else
              PBXScript.OutputTrace "Command successfully executed."
          end if

          if bTraceStdOut then
              Dim objStdOut, sStdOut
              Set objStdOut = objWshScriptExec.StdOut
              sStdOut = Trim(objStdOut.ReadAll)
              PBXScript.OutputTrace "StdOut:"
              PBXScript.OutputTrace sStdOut
          end if

          Dim objStdErr, sStdErr
          Set objStdErr = objWshScriptExec.StdErr
          sStdErr = Trim(objStdErr.ReadAll)
          if sStdErr <> "" then bReturn = False
          if bTraceStdErr then
              PBXScript.OutputTrace "StdErr:"
              PBXScript.OutputTrace sStdErr
          end if

          Set objWsh = Nothing

          PBXScript.OutputTrace "bReturn = " & CStr(bReturn)
          PBXScript.OutputTrace "<----- LaunchCmdEx"
      End Function

  Please note that this command will be launched on the SwyxServer machine. No windows will be opened, and no user input can be made. So only start command line applications which do not require any user input.

  As the call routing is executed by the Swyx Server service, the command will also be executed by the Swyx Server service. That means, it is executed by the Windows user, the Swyx Server service is running under (usually this is the local SwyxServiceAccount user).

  To call any standard command like "copy" or "move" you need to call the command line interpreter (cmd.exe) and then pass your command as parameter to it. The environment variable %comspec% provides to full path to cmd.exe.

  This example copies all files from FolderA to Folder B.

      LaunchCmdEx "%comspec% /c copy C:\FolderA\*.* C:\FolderB", True, True

  Make sure that the SwyxServiceAccount (or the account you have configured to the Swyx Server service) has enough privileges to execute your command.

  This function makes use of the Server Script API function PBXScript.OutputTrace to write trace information into the SwyxServer trace file.

  This function was initially posted into this forum topic.

  By Tom Wellige

  - March 2, 2024
  Share
  Share
  Followers 0
  Previous Page LaunchCmd
  Next Page LaunchHTTPRequest

## Important Information

By using this site, you agree to our Terms of Use and have taken note of our Privacy Policy.
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.