Workload Automation Agents

 View Only
  • 1.  Executing powershell scripts

    Posted May 11, 2021 11:30 AM
    Good morning everyone,
    I am wondering if there is an option for windows agents to set a default terminal like there is for linux/unix agents?
    We currently are calling powershell.exe and then passing script as an argument but I would like to just call the powershell script itself. This just gives an sub error right now.
    I have associated the ps1 with powershell.exe so that it understand what command interpreter to use, but still not working.


    ------------------------------
    Thanks for any help in advance
    Dustin.
    Lead DevOps Scheduler
    Blue Cross Blue Shield Of Tennessee
    ------------------------------


  • 2.  RE: Executing powershell scripts

    Posted May 11, 2021 11:47 AM

    You could create a Window Batch file PS1.bat

    PS1.bat
    powershell  <fully qualified path/name of PowerShell Script>

    Create a Windows Job

    NT_JOB PS1BAT
    AGENT <Agent Name>
    CMDNAME <Path to Batch file>\PS1.bat
    RUN ANYDAY
    ENDJOB

    Would that provide what you need?



    ------------------------------
    Senior Systems Analyst
    UPS
    United States
    ------------------------------



  • 3.  RE: Executing powershell scripts

    Posted May 11, 2021 11:51 AM

    We currently do it like this

     

    NT_JOB

    RUN ANYDAY

    CMDNAME C:\program files\powershell\7\pwsh.exe

    ARGS F:\job\job.ps1

    ENDJOB

     

    I would like to do it like this

     

    NT_JOB

    RUN ANYDAY

    CMDNAME F:\jobs\job.ps1

    Endjob.

     

    Adding a bat file in the mix adds another layer of potential issues. For instance if a powershell script runs just fine but then the cmd that the .bat was called from fails we get a failure returned when it was actually successful. Just trying to reduce the number of components if possible.

     

     

    Dustin Neely
    DevOps Analyst/Certified Automation button pusher

    W 423.535.5784
    M 423.208.6255
    E Dustin_Neely@bcbst.com
    www.bcbst.com

    Batch Scheduling Wiki

     

     

     

     

    Please see the following link for the BlueCross BlueShield of Tennessee E-mail disclaimer: https://www.bcbst.com/about/our-company/corporate-governance/privacy-security/email-policy.page





  • 4.  RE: Executing powershell scripts

    Posted May 12, 2021 07:40 AM
    An alternative to use that will work with your preferred Job Definition.

    ps2exe - Compiles Powershell scripts to executables.

    https://www.powershellgallery.com/packages/ps2exe/1.0.10


    From Admin Command Window:
    • powershell
    • Install-Module -Name ps2exe
    • ps2exe job.ps1 --> created job.exe

    Update ESP Job definition:


    NT_JOB
      RUN ANYDAY
      CMDNAME F:\jobs\job.exe
    ENDJOB



    ------------------------------
    Senior Systems Analyst
    UPS
    United States
    ------------------------------



  • 5.  RE: Executing powershell scripts

    Broadcom Employee
    Posted May 12, 2021 09:04 AM
    Hi,
    This will not work, because .ps1 are not executable by itself.
    CMDNAME F:\jobs\job.ps1
    You will need to use powershell.exe to call script.  Any script or command that you can double click on Windows can be defined in CMDNAME.

    HTH,
    Nitin Pande

    ------------------------------
    Support
    Broadcom
    Toronto
    ------------------------------



  • 6.  RE: Executing powershell scripts

    Posted May 12, 2021 09:08 AM

    Nitin are you sure?

    I ask because powershell scripts can be double clicked on within windows and they execute just fine but cannot be defined on the CMDNAME line

     

     

    Dustin Neely

    DevOps Analyst/Certified Automation button pusher

    P – 423-535-5784

    E – Dustin_Neely@bcbst.com

    Batch Scheduling Wiki

     






  • 7.  RE: Executing powershell scripts

    Broadcom Employee
    Posted May 12, 2021 09:49 AM
    Dustin,
    I just tried, and it just opened the script in Notepad.  The .ps1 scripts by default needs Powershell.  You can create some kind of link or target that will call powershell and feed the script to it.   In the agent, you will have to define the powershell as command and then args will ps1 script.
    As for script calling other scripts, use LastExitCode and %ERRORLEVEL% to pass the non-zero exit code.

    E.g. batch running powershell
    powershell "&{C:\tmp\my.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%
    
    HTH,
    Nitin Pande

    ------------------------------
    Support
    Broadcom
    Toronto
    ------------------------------



  • 8.  RE: Executing powershell scripts

    Posted May 12, 2021 09:53 AM

    Nitin,

    This is the heart of my question because as you are setting up a windows server you can associated .ps1 scripts to powershell. This is setting the file extension to use a specific command interpreter so that when you double click it just runs. I have test that and it doesn't work. This is helpful to know that this isn't functional in the System agents. We will continue to call powershell first or I may create a wrapper framework for it to standardize it a bit.

     

    Dustin Neely

    DevOps Analyst/Certified Automation button pusher

    P – 423-535-5784

    E – Dustin_Neely@bcbst.com

    Batch Scheduling Wiki

     






  • 9.  RE: Executing powershell scripts

    Posted May 12, 2021 07:31 AM
    Add PowerShell %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe to system PATH

    Run CMD

    powershell -ExecutionPolicy Bypass -f $$path\script.ps1