ESP Workload Automation

  • 1.  What Windwos script and command types can be called in a job definition?

    Posted Sep 10, 2015 02:23 PM


    Most of our Windows jobs are defined  to run .bat files in the job definitions.  For example, they look something like this:

     

    NT_JOB <Job Name>                        

        RUN SUN LESS 0 WORKDAYS                                     

        CMDNAME \\path\to\file.bat         

        AGENT <agent Name>           

    ENDJOB        

     

    Is it possible to run other Windows script or command types in a similar way?  For example, could you have:

     

    //path/to/file.exe  or

    //path/to/file.vbs   or

    //path/to/file.ps

     

    Of coruse, this assumes that Windows Script Host is running, and (sepecially for .ps) that you have a compatable version of Windows PowerShell running.

     

    Currently, we are doing it this way:

     

    CMDNAME C:\windows\system32\cmd.exe 

    args /c F:\path\to\vb\script.vbs

     

    However, I don't think this is necessary.

     

    Thansk!                           



  • 2.  Re: What Windwos script and command types can be called in a job definition?

    Broadcom Employee
    Posted Sep 10, 2015 03:43 PM

    Hello Jonathan,

     

    On "Command Reference Guide" for ESP, under "CMDNAME Statement—Specify Commands to Run on Windows and UNIX", it has following:

     

    You can run other types of executable files from the Windows command line than those that are directly supported by the CMDNAME statement. (The executable files you can run are defined in the Windows system variable PATHEXT.) There are two ways to run those executable files using the agent: 

    Run the executable from a batch file (.bat) or command file (.cmd)

    Invoke the program's interpreter with the CMDNAME statement and use the ARGS statement to pass the command as a parameter.

     

     

    So for VBS script, you may use following:

    CMDNAME c:\path\cscript.exe

    ARGS F:\path\to\vb\script.vbs

     

    Hope this helps,

     

    Lucy



  • 3.  Re: What Windwos script and command types can be called in a job definition?
    Best Answer

    Posted Sep 10, 2015 03:51 PM
      |   view attached

    For exe files you should be able to specify the EXE with CMDNAME.

     

    When I tried to call a VBS or PS1 script from the CMDNAME I get the following error:

     

    Create process failed for "<scriptname>" with a Cmpc(193).

     

    I was able to get VBS and PS1 by specifying the respective EXE (cscript.exe for VBS, powershell.exe for PS1) with CMDNAME.  In my environment the path to the exe is not required.

     

    VBS Example

    NT_JOB WIN7VBS                     
      AGENT <agent name>           
      CMDNAME cscript.exe              
      ARGS <path to script>\hello.vbs    
      RUN ANYDAY                       
    ENDJOB                            


    PowerShell Example

    NT_JOB WIN7PS1                                                        
      AGENT <agent name>                                               
      CMDNAME powershell.exe                                              
      ARGS /C <path to script>\hello.ps1 %USER1                             
      RUN ANYDAY                                                          
    ENDJOB                                                                
     

    For PowerShell I needed to create 2 functions to get the correct behavior.

    • EnablePowerShellOutput
    • ExitWithCode

     

    I have attached a sample of what I have used to run a PowerShell script in ESP.


    Notes:

    • CMDNAME references PowerShell
    • ARGS contains script
    • Added .txt extension to the PowerShell script attachment to eliminate any issues with the extension

    Attachment(s)

    zip
    hello_ps1.txt.zip   1 KB 1 version


  • 4.  Re: What Windwos script and command types can be called in a job definition?

    Broadcom Employee
    Posted Sep 10, 2015 04:03 PM

    Thank you Rick as always. I like your example with powershell.

     

    Lucy



  • 5.  Re: What Windwos script and command types can be called in a job definition?

    Posted Sep 10, 2015 04:07 PM

    Thank you all for your response and samples.  This was very informative and helpful.



  • 6.  Re: What Windwos script and command types can be called in a job definition?

    Posted Oct 23, 2015 04:44 PM

    First, thank you.  this helped me get thru a small build.

     

    I hope this isn't to dumb a question, but was is the /C preceding the path in the ARGS statement?



  • 7.  Re: What Windwos script and command types can be called in a job definition?

    Posted Oct 23, 2015 06:05 PM

    The /C is shorthand for the -command parameter to powershell.exe

    You could also specify -c



  • 8.  Re: What Windwos script and command types can be called in a job definition?

    Posted Oct 23, 2015 06:11 PM

    Perfect!  Thanks.



  • 9.  Re: What Windwos script and command types can be called in a job definition?

    Posted Dec 19, 2016 10:12 AM

    Hi Rick, we now have some folks inquiring about whether cybAgents (via ESP) can schedule .ps1 files or not.

    A few months back when we asked, CA suggested this type of job def:

    nt_job cybwin2
    cmdname C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    agent nt_xyz
    user domain\userid
    args '-ExecutionPolicy unrestricted' +
    '-File D:\cyb_agent_R7\helloworld.ps1'
    exitcode 1 success
    exitcode 0 success
    run any
    endjob

     

    The .ps1 runs just fine running the .ps1 from windows console but
    Running this via cybAgent we're getting this in the spool
    Unexpected token '-File D:\cyb_agent_R7\helloworld.ps1' in expression or state
    ent.
    At line:1 char:71
    + '-ExecutionPolicy unrestricted' '-File D:\cyb_agent_R7\helloworld.ps1' <<<<
    + CategoryInfo : ParserError: (-File D:\cyb_agent_R7\helloworld.p
    s1:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

    (This is on an R7 agent ...have not tried it on an R11 yet)

    the .ps1 script consists of:

    $strString = "Hello World"
    write-host $strString
    Exit 4

     

    So, while awaiting (a few days) for CA to respond to that error, I saw your example in the Forum.

    To reiterate, you only were able to get yours to work by employing

    CMDNAME powershell.exe                                              
      ARGS /C <path to script>\hello.ps1 %USER1                             
      RUN ANYDAY                                                          
    ENDJOB                                                                
     

    For PowerShell I needed to create 2 functions to get the correct behavior.

    • EnablePowerShellOutput
    • ExitWithCode

     

    I'm really a bit disappointed that the cybAgent would require one to code a customized function in order to write output to

    the agent's spool file for anything that works on the console,  (we'd absolutely need echoing to the spoolfile for debugging the .ps1s) as well as requiring a customized function to set the return code  (for example .vbs and cscript has a built in function for exitting with an errorcode...I thought powershell was supposed to supplant windows scripting host...seems like its a step backward).



  • 10.  Re: What Windwos script and command types can be called in a job definition?

    Posted Dec 21, 2016 03:46 PM

    Was able to get a simple helloworld powershell to run this way:

    (it DID need Ricks ExitWithCode function in order to return a cc to ESP,

    but I didnt need to code the EnablePowerShellOutput function, at least for this extremely simple script,

    and it still echoed out to the jobs spool file aok:

    nt_job cybwin2.v1b
      cmdname C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
      agent nt_cccmsdapp10
      user exelonds\nt_userid
      args "& ""D:\cyb_agent_R7\helloworld.ps1"""
      exitcode 4 success
      exitcode 0 success
      run any
    endjob

     

    Note the args has an & followed by the path of the .ps1 I want to run  and THREE sets of double quotes.

    There is supposed to be a way to use -File to point to your .ps1 file but I kept getting errors...perhaps because its

    not running powershell 2.0.......anyway Thanks Rick for the ExitWithCode ....it is a great help and at least we've shown we are able to run powershell.