Topic
4 replies Latest Post - ‏2014-01-27T15:30:34Z by Robertc65
Robertc65
Robertc65
15 Posts
ACCEPTED ANSWER

Pinned topic Execute Powershell Script using RunTask

‏2014-01-20T20:59:29Z |

I have a C:D Process that executes a Powershell Script located on the D: drive of my C:D server.

        SYSOPS="pgm(powershell D:\Test\Execute_Remote_Script.ps1)"

A line in my Powershell script executes batch file located on a remote server.

         invoke-command -computername qsc-a-ice01 -scriptblock {Start-Process \\qsc-a-ice01\D$\Test\RunThis.bat

If I execute the local powershell script manually by typing "powershell d:\test\Execute_Remote_Script" on the command line, The script works as expected executing the batch file on the remote server "RunThis.bat"

When ever I try to execute the local script "Execute_Remote_Script.ps1"  from a connnect:direct RunTask is does not work.

I have tested this multiple ways and have concluded that C:D will properly execute both batch and powershell scripts as long as the script or batch file does not call a remote server as part of it's process.

I'm not trying to pass any parameters from C:D to the remote machine. With C:D I'm simply trying to execute my local batch or script file. All neceesary parameters are in the local batch or script file. Again if I simply execute the batch or script from the command line of my C:D server it works as expected.

I do not know why the difference. Can someone help explain what I'm missing here?

 

Thanks in Advance

Rob

 

 

  • VolkerFriedrich
    VolkerFriedrich
    150 Posts
    ACCEPTED ANSWER

    Re: Execute Powershell Script using RunTask

    ‏2014-01-23T10:49:38Z  in response to Robertc65

    First correct the SYSOPTS syntax. The pgm() parameter is expecting a single argument pointing to an executable file on disk. Additional arguments need to be passed using the args() parameter. So, either use pgm() together with args() or use cmd() instead, for example:

    SYSOPTS="pgm(C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe) args(D:\Test\Execute_Remote_Script.ps1)"

    SYSOPTS="cmd(powershell D:\Test\Execute_Remote_Script.ps1)"

     

    The later method is quite useful for debugging RUN TASK/JOB. Redirect stdout/stderr to a log file to capture any output or error message raised by the command line:

    SYSOPTS="cmd(powershell D:\Test\Execute_Remote_Script.ps1 >D:\Test\Execute_Remote_Script.log 2>~&1)"

     

    In my test the log file was clearly showing my problem:

    File D:\Test\Execute_Remote_Script.ps1 cannot be loaded because running
    scripts is disabled on this system. For more information, see
    about_Execution_Policies at 
    http://go.microsoft.com/fwlink/?LinkID=135170.
        + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecord
       Exception
        + FullyQualifiedErrorId : UnauthorizedAccess
    

     

    To resolve this issue, I had to specify an appropriate ExecutionPolicy, like

    SYSOPTS="cmd(powershell -ExecutionPolicy RemoteSigned -File D:\Test\Execute_Remote_Script.ps1 >D:\Test\Execute_Remote_Script.log 2>~&1)"

    Updated on 2014-01-23T10:54:39Z at 2014-01-23T10:54:39Z by VolkerFriedrich
    • Robertc65
      Robertc65
      15 Posts
      ACCEPTED ANSWER

      Re: Execute Powershell Script using RunTask

      ‏2014-01-23T20:03:56Z  in response to VolkerFriedrich

      Volker,

      Thank you again for your help. I did not really understand how to use the pgm command. My Execution Policy settings are correct. Both examples you give are working for me.

      I'm still not sure if I have a full understanding as the C:D documentation is somewhat limited on this topic.

      Can you please try to explain the basics for how to use the pgm and cmd commands and when I should use pgm vs cmd.

       

      Thanks

      Rob Coyle

      • VolkerFriedrich
        VolkerFriedrich
        150 Posts
        ACCEPTED ANSWER

        Re: Execute Powershell Script using RunTask

        ‏2014-01-27T09:58:01Z  in response to Robertc65

        The two ways for specifying RUN TASK sysopts

        • pgm(filespec) args(arguments)
        • cmd(command | parms)

        pgm(filespec) specifies which executable file will be run, typically an .EXE, .CMD or .BAT file.
        args(arguments) specifies optional arguments passed to the executable when it is started.
        Technically, C:D will start this executable directly, wait for its completion and log its exit code as the RUN TASK's completion code.

        cmd(command | parms) specifies a command and any arguments that this command requires. It can be used for internal system commands, such as del, ren or move, but also with executable files.
        Technically, C:D will start a Windows command shell (CMD.EXE) and pass "command | parms" along as the command line to execute. Once CMD.EXE has completed, its exit code will be be logged as the RUN TASK's completion code.

        CMD.EXE /C command parms
        

         

         

         

        • Robertc65
          Robertc65
          15 Posts
          ACCEPTED ANSWER

          Re: Execute Powershell Script using RunTask

          ‏2014-01-27T15:30:34Z  in response to VolkerFriedrich

          Thank you for the explaination Volker. It was not clear to me that the expression "cmd(command | parms)" was starting an instance of the Windows command proccesor. This is very helpful information.

          Rob Coyle