Running PowerShell Script using xMatters Agent's xm-shell

Not Yet Reviewed

The only documentation that xMatters provide regarding their xMatters Agent xm-Shell feature is on Running local scripts on the xMatters Agent.  There it talks about, on the server where the xMatters Agent resides, creating a batch file with commands and then running that batch file commands.

I tried to translate this into running a PowerShell script while passing arguments to that script and had no luck.  Here is what I have tried:

// This one seems to start the process without issue but the process doesn't complete.
// var GatherObjectStatus = Shell.script(function () {/* REM #### PLACE YOUR .BAT SCRIPT BETWEEN HERE #####
//     powershell -File "C:\\Coding Library\\PowerShell\\Find Status of Some Object.ps1" "1238233" "Yes"
//     REM ##### AND HERE #### */});
//     //{ID: ObjectId});

// This one also starts the process but it doesn't complete. This version is using ExecutionPolicy.
// var GatherObjectStatus = Shell.script(function () {/* REM #### PLACE YOUR .BAT SCRIPT BETWEEN HERE #####
// powershell -ExecutionPolicy Bypass -File "C:\\Coding Library\\PowerShell\\Find Status of Some Object.ps1" "1238233" "Yes"
// REM ##### AND HERE #### */});
// //{ID: ObjectId});

// This one also starts the process but it doesn't complete. This version is using ${ID}.
// var GatherObjectStatus = Shell.script(function () {/* REM #### PLACE YOUR .BAT SCRIPT BETWEEN HERE #####
// powershell -ExecutionPolicy Bypass -File "C:\\Coding Library\\PowerShell\\Find Status of Some Object.ps1" ${ID} "Yes"
// REM ##### AND HERE #### */},

// Execute it
// var result = Shell.exec('cmd', GatherObjectStatus );

I would also like result to be able to contain more than just if the Shell.exec was successful or not.  For example, run a query against the database while in the PowerShell script and then return the results back to the Step Script to determine how it should proceed.

Do any of you folks that are likely smarter than I, see a way to make this work?

Let me add that I know very little about xm-shell.  If there is a way to run the PowerShell directly without having to put it in a batch file and then run that, I am all ears.  I tried a few things but nothing worked.

TIA

Mike

0

Comments

3 comments
Date Votes

Please sign in to leave a comment.

  • Mike, I was able to get a powershell command running via a Flow script running on an xAgent.

    My script is

    try
    {
     var Shell = require('xm-shell');
    }
    catch (e)
    {
     console.log("Could not load library:" + e);
    }

    if(Object.getOwnPropertyNames(Shell).length === 0 )
    {
     //integration is running in the cloud
     console.log("Running in cloud - exiting.");
     return;
    }

    // request the operating system name from the shell
    var osname = Shell.osname();

    if (osname.startsWith('Windows'))
    {

    var script = Shell.script(function () {/* REM #### PLACE YOUR .BAT SCRIPT BETWEEN HERE #####
     C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -c "& {dir d:\temp\*.zip}"
     REM ##### AND HERE #### */},{});    

    console.log("Running Shell script now...");
    var result = Shell.exec('cmd', script);
    console.log("Output is:");
    console.log( result.output());
    console.log("ExitCode is:");
    console.log( result.exitCode());
    console.log("Error is:");
    console.log( result.error());
    }            

    The output was a directory listing of the .zip files in my d:\temp folder.  Hopefully this gives you something to build your solution on.

    Cheers,

    Jeremy Brown

    0
  • I modified the script block slightly to allow variables to be passed to powershell:

    var script = Shell.script(function () {/* 
    @ C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -c "& {dir ${pathName} }"
    */},{ "pathName":"d:\\temp\\*.zip" })

    (Note the requirement to escape backslashes and asterisks in the argument list.)
    (Note also that the comment block requires a newline after your command -  before the closing "*/" - or else the command won't be executed.)

    0
  • @Jeremy,

    Thanks for that feedback.  Not quite what I am trying to do but close enough to give me some ideas which did work.  Your ran a Windows command with arguments using PowerShell to execute it.  I was trying to use PowerShell to execute a PowerShell script with arguments.

    Using the same type of example I started with in my first post, and tweaking it based on what I learned from your reply, here is what I came up with that does work:

    var script = Shell.script(function () {/*
      powershell -c "& {C:\SolarWinds\CodingLibrary\PowerShell\FindStatusofOrionAlert.ps1 -AlrtActId ${AlrtActId} -scrptInit ${scriptinit}}"
    */},
    {"AlrtActId":"1238233","scriptinit":"Yes"});

    A few things I learned along the way in hopes they will help others:

    • You cannot have spaces in the path or script filename,
    • The PowerShell script must be written to accept params and not args.
    0

Didn't find what you were looking for?

New post