Automic Workload Automation

  • 1.  PowerShell intepreter jobs

    Posted Feb 09, 2014 06:13 AM

    Is anyone using PowerShell forInterpreter jobs on Windows? We are experimenting with this, but have not found a completely workable configuration. The sticking point right now is error & return code handling.

    The TRAILER.WINDOWS JOBI (include) in client 0 has a bunch of commands that are specific to Windows jobs of type BAT. These include commands to run the job messenger daemon. E.g.,

    :INC TRAILER.WINDOWS.USER.END ,nofound=ignore
    :IF &UC_WIN_TYP = "BAT"
    &UC_JOBMD JNR=&UC_REALNR MNR=&UC_MANDANT PNR=&UC_IP_PORT IPA=&UC_IP_ADR TYP=E RET=00000000 TXT="        Job ended"
    :DATA @set retcodemsg=%errorlevel%
    :DATA @set retcode=0
    GOTO JOBENDE
    ...

    My understanding is that these job messenger commands are important for sending information about the status of the job back to the agent and the Automation Engine. There are other similar :IF clauses later in the same JOBI, and in HEADER.WINDOWS too (and this header JOBI is incidentally where several important JDM script variables are set). The default JOBI objects for Windows jobs do not include equivalent commands for PowerShell, or indeed for any jobs of type Interpreter. I gather that this is part of the problem.

     

    I began working on a solution based on inserting similar commands in TRAILER.WINDOWS.USER.END

    :IF &ATT_WIN_TYP = "EXTCOMPROC"
    ! Return code handling for PowerShell systems
    : PRINT "*** Including PowerShell-specific instructions from TRAILER.WINDOWS.USER.END ***"
    ! insert return code handling and JMD commands here

    Before I spend too much time on this, I thought I would ask the experts. Has anyone devised a good way to use PowerShell for interpreter jobs?



  • 2.  PowerShell intepreter jobs

    Posted Feb 09, 2014 03:14 PM

    Michael,

    Have you set up a windows agent to act as a powershell interpreter?  The ini file for a WINDOWS agent needs to be configured to act as a powershell interpreter.  it will require a windows agent license, 

    SEE EXAMPLE ini file below

    start the agent and  create a windows job, clicking on interpreter in the WINDOWS tab.  

    use  your powershell scripts (point to them in the process tab, or write the powershell code in the process tab)

    here is an example..

    [GLOBAL]
    name=POWERSHELL
    system=UC4

    logon=1
    language=(E,D)
    logging=..\TEMP\powershell_LOG_##.TXT
    logcount=10
    helplib=UC.MSL
    helpcache=ALL
    licence_class=9

    ; userid_type=INCL
    ; use Windows Job Objects 0=no/1=yes
    useJobObject=0
    ECPEXE=C:\windows\system32\windowspowershell\v1.0\powershell.exe
    ECPEXT=PS1
    ft_temp_file=yes
    HomeDirCache=10

    ; FileProcessingMaxDepth = 0
    ; FileProcessingTimeout  = 0


    [USERID]
    ; user/domain=START


    [STATUS_CHECK]
    time=1


    [VARIABLES]
    UC_HOST_CODE=UC_CODE
    UC_HOST_JCL_VAR=WINDOWS
    UC_EX_PATH_BIN=.
    UC_EX_PATH_TEMP=..\TEMP\
    UC_EX_PATH_JOBREPORT=..\TEMP\
    UC_EX_JOB_MD=.\UCXJWX6M.EXE
    UC_EX_PATH_BACKUP=..\BACKUP


    [TRACE]
    file=..\TEMP\POWERSHELL_TRACE_##.TXT
    trccount=2
    tcp/ip=0
    mail=0
    filetransfer=0
    event=0
    compress=0
    memory=0


    [TCP/IP]
    port=2321
    bindaddr=
    bindlocal=0
    ; try all n seconds to connect to server
    connect=60
    report=60
    SendBufferSize=1048576
    RecvBufferSize=1048576
    cp=YOURUC4SERVER:2217

    TcpKeepAliveTime=yes
    tcp_nodelay=yes


    [HOSTS]
    ; Overwrite IP destination in case of IP NATing problems
    ;  (e.g. firewalls)
    ; <UC4-name>=<dns-name> or
    ; <UC4-name>=<ip-addr>


    [CP_LIST]
    2218=192.168.0.20


    [AUTHORIZATION]
    KeyStore=
    InitialPackage=


    here is a simple get-executionpolicy script that writes the output to a file ( this uses both uc4 variables and powershell variables)  this is placed in the process tab of your new job


    $date = (get-date).dayofyear
    get-executionpolicy | out-file "c:\temp\&$AGENT#\$date.log"
      write-host "* * * * * * * * * *"
      write-host  ("   Return Code: $? " -f $LastExitCode)
      write-host "* * * * * * * * * *"
      exit $LastExitCode
    it writes the return code to the job's report and the output to a file in c:\Temp\PowerShell\40.log

    you can use output filters to check for errors..


  • 3.  PowerShell intepreter jobs

    Posted Feb 10, 2014 02:42 AM
    Mark Blackmer said:

    Michael,

    Have you set up a windows agent to act as a powershell interpreter?
    ...

    ECPEXE=C:\windows\system32\windowspowershell\v1.0\powershell.exe
    ECPEXT=PS1

    Yes, the agent is configured correctly.

    Mark Blackmer said:
    here is a simple get-executionpolicy script that writes the output to a file ( this uses both uc4 variables and powershell variables)  this is placed in the process tab of your new job

    $date = (get-date).dayofyear
    get-executionpolicy | out-file "c:\temp\&$AGENT#\$date.log"
      write-host "* * * * * * * * * *"
      write-host  ("   Return Code: $? " -f $LastExitCode)
      write-host "* * * * * * * * * *"
      exit $LastExitCode

    it writes the return code to the job's report and the output to a file in c:\Temp\PowerShell\40.log

    you can use output filters to check for errors.

    Thanks. That’s an interesting approach. I assumed there was a more straightforward way, but perhaps not.






  • 4.  PowerShell intepreter jobs

    Posted Feb 10, 2014 03:26 AM

    My initial approach was to find a solution that did not require the user to register output files, configure filters, or add steps to the Post-Process tab. This is why I thought of putting special PowerShell handling steps directly into the USER HEADER and TRAILER includes.

    Obviously though, exit codes in PowerShell aremore complicatedthan the simple integer exit codes or error levels of shell or batch scripts. It might not be possible to reduce this complexity without risking losing information. I suppose we could enforce conventions on PowerShell developers, so that a simplified approach would work; however, I generally don’t like imposing arbitrary and restrictive standards on our developers.

    Perhaps a more general approach based on the oneMark Blackmerproposed above would be wise.

    A general solution would ideally catch all exceptions from the PowerShell job (both terminating and non-terminating exceptions), and store as much information about these exceptions as possible. It should also capture the return codes of any normal Windows programs run from the script. All this information should be stored in a form that is easily accessible to UC4 — perhaps a registered output file. This information could then be acted upon by UC4, to set the status of the job or perform other actions.

    In a comment on a post on the unofficialAutomic blog, Joachim (‘joz’) suggests using the USER HEADER and TRAILER job includes to wrap the whole job in a try/catch block.




  • 5.  PowerShell intepreter jobs

    Posted Feb 10, 2014 04:24 PM
      |   view attached

    The login joz in this blog is mine.

    I wrote in german a short description for me (see pdf attached) how to use powershell in UC4 as an alternate commandshell with seamless error handling. 


    Attachment(s)

    pdf
    uc4_powershell.pdf   250 KB 1 version


  • 6.  PowerShell intepreter jobs

    Posted Feb 13, 2014 04:34 AM

    Hello Joachim,

    Your description looks promising; I looked into your PDF and there is one part I do not understand. Where are the WINDOWS.TRAILER.USER.* objects included in the actual job? Is the START included Pre-Process and the END and ABEND in Post-Process? 

    Jeroen



  • 7.  PowerShell intepreter jobs

    Posted Feb 13, 2014 05:18 AM

    I believe the header is inserted at the top of the job script, but is still part of the job and not the Pre-Process. Similarly, I believe the trailer is added to the end of the job script, but is a part of the job, not the Post-Process. Look at the HEADER and TRAILER includes in client 0 to see how these user JOBIs are included. Enable extended reports (in the Header tab of a job) to see additional output from your job, including aJCLtab that shows the generated script, and aScript tab that shows some of the behind-the-scenes activities that the Automation Engine performs during activation & generation.

    Joachimor Automic team, please correct me if I’m mistaken.



  • 8.  PowerShell intepreter jobs

    Posted Feb 13, 2014 06:13 AM

    Thanks! 



  • 9.  PowerShell intepreter jobs

    Posted Feb 13, 2014 08:30 AM

    See also the section of theInside UC4 Guidethat describes the phases of object execution. In the section entitledPhase 2: Generation(webhelpe/Content/ucacni.htm), there is a description of where the header and trailer come into play:

    1. AgentGroup resolution
      The relevant Agent is selected for tasks that are processed in an AgentGroup. Subsequent changes are possible in the Process tab.
    2. Object variables
      The object variables of the task and its superordinate task are provided.
    3. Pre-Process
      Jobs: Pre-Process tab contents are processed.
    4. Attribute dialog
      The Attribute dialog is only displayed if the task is activated manually and NOT generated at runtime.
    5. Header
      Jobs: Header Include and related User Includes are processed.
    6. Process
      Process tab contents are processed.
    7. Trailer
      Jobs: Trailer Include and related User Includes are processed.
    8. JCL generation
      Jobs: JCL generation is possible after UC4 script processing. JCL modifications are possible before the job starts on the destination computer.

    The Post-Process is handled much later, duringPhase 4: Completion(webhelpe/Content/ucacnk.htm).