Automic Workload Automation

Expand all | Collapse all

How to use powershell in backend variable?

  • 1.  How to use powershell in backend variable?

    Posted Nov 20, 2017 06:10 PM
    I have been trying to use Windows powershell to get the first (or nth) line out of a file.
    Something like:
    cmd /c PS C:\> Invoke-Command Get-Content  "c:\uc4\agents\windows\bin\UCXJWX6.ini" | select -First 1
    /c PS C:\> Invoke-Command Get-Content  "c:\uc4\agents\windows\bin\UCXJWX6.ini" | select -First 1
    PS C:\> Invoke-Command Get-Content  "c:\uc4\agents\windows\bin\UCXJWX6.ini" | select -First 1

    These are all failing.

    This gets close, but I have not found a way to get windows to only return the nth line---This lists only those lines with 'log' in the line.    
    cmd /c type  c:\uc4\agents\windows\bin\UCXJWX6.ini  | find "log" |  more



  • 2.  How to use powershell in backend variable?

    Posted Nov 20, 2017 07:00 PM

    Get-Content returns an array of strings. To access the first line, just access the first element in the array:

    (Get-Content c:\temp\test.txt).item(0) # First line

    Invoke-Command is for calling external commands, not Powershell cmdlets. 



  • 3.  How to use powershell in backend variable?

    Posted Nov 20, 2017 07:01 PM
    Here is a powershell statement line that returns just the 5th line of a txt file;

    get-content C:\temp\1371306.REP.txt | select -Index 5

    Is that what you are looking for?


  • 4.  How to use powershell in backend variable?

    Posted Nov 20, 2017 07:48 PM
    For bigger files the better choice as pipeline will throw away unused lines (y).


  • 5.  How to use powershell in backend variable?

    Posted Nov 20, 2017 09:14 PM
    Joel and Pete
    That is exactly the type of statement I want.   I will try them tomorrow.  I thought that I used the 'bare' get-content' in the Backend variable (and failed), but I will do it again in the morning.
    Thanks


  • 6.  How to use powershell in backend variable?

    Posted Nov 21, 2017 10:28 AM
    This works perfectly from a command line
    powershell  (Get-Content c:\uc4\agents\windows\bin\UCXJWX6.ini.item)[0]
    I get the first line of the file.

    but gets the following from preview in a backend
    Error: U02000172 Command with RunID '14427931' ended on return code '1'. Command:
    'powershell  (Get-Content c:\uc4\agents\windows\bin\UCXJWX6.ini.item)[0]'.

    adding CMD  gets a response, but the wrong one
               
    Microsoft Windows [Version 6.1.7601]Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    C:\uc4\Agents\Windows\bin>C:\uc4\Agents\Windows\bin>

    adding CMD /c gets
    Error: U02000172 Command with RunID '14427931' ended on return code '1'. Command:
    'cmd /c powershell  (Get-Content
    c:\uc4\agents\windows\bin\UCXJWX6.ini.item)[0]'.

    So what am I missing?   Is powershell missing from the server? is there a config / vara setting in UC4 that I need?


  • 7.  How to use powershell in backend variable?

    Posted Nov 21, 2017 10:36 AM

    Option A)In the Windows tab of your job you can choose the interpreter (bat, cmd or other interpreter). With V11 the default "other interpreter" should be powershell if agent configuration had been adapter. So try changing that then you'll be able to run pure Powershell code in your job.

    Option B) If you want to work with :REGISTER_VARIABLE you must use (and have supported) :BEGIN_EXT_INTERPRETERS / :END_EXT_INTERPRETERS. Lookup the documentation for that feature.

    https://docs.automic.com/documentation/webhelp/english/AWA/12.1/DOCU/12.1/AWA%20Guides/help.htm#AE_AWA_Source/Script/ucsaby.htm?Highlight=:BEGIN_EXT_INTERPRETER

    Regards
    Joel



  • 8.  How to use powershell in backend variable?

    Posted Nov 21, 2017 10:36 AM
    BTW,   Where it works, is on my W10 pro 64 bit laptop.  I cannot remote into the server.


  • 9.  How to use powershell in backend variable?

    Posted Nov 21, 2017 11:15 AM
    Joel, 
    Thanks for the suggestion with a test job.   Does that mean that the 'Preview' button will not work if using Powershell in the variable?

    Getting the administrator to add options at all much less :BEGIN_EXT_INTERPRETERS  is a PITA, so trying backend first.



  • 10.  How to use powershell in backend variable?

    Posted Nov 21, 2017 03:55 PM
    Got it.   Based on the UC_RB_VARA_PROCESSLIST in C-0,

    In the command in the VARA, I have
    powershell -Command "Get-Content  c:\uc4\agents\windows\bin\UCXJWX6.ini" | select -First 1

    This returns the first line when doing the Preview and returns the same value when invoked from a Windows job.

    Would have been so much easier if they would have included a sample of using powershell in the doc.