Execute a PowerShell script as a standard monitor in SCOM

Custom monitors in SCOM may, unless the Authoring Console is used or custom XML employed, only execute VBScript. PowerShell scripts can still be executed though, as long as they’re Base64-encoded and wrapped in VBScript.

  1. Write a PowerShell script that places its output on the console as easily-identifiable strings, the output from which can be easily parsed by VBScript, e.g.

    Success: 0
    Error message: Insufficient memory.
  2. Encode the PowerShell script to Base64, where {Script} is the script:

    $Command = '{Script}'
    $EncodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command))
    $EncodedCommand > EncodedCommand.txt
  3. Create the monitor, implementing a VBScript similar to that below, but inserting the contents of EncodedCommand.txt into the PsScript variable.

    Command="PowerShell.exe -EncodedCommand " & PsScript
    Set Shell=CreateObject("WScript.Shell")
    Set Executor=Shell.Exec(Command)
    varSuccess=Mid(varPSResult, InStr(varPSResult, "Success: ") +
    Len("Success: "), 1)
    varErrorMessage=Right(varPSResult, Len(varPSResult) - (InStr(varPSResult,
    "Error message: ") + Len("Error message: ") - 1))
    Dim oAPI, oBag
    Set oAPI=CreateObject("MOM.ScriptAPI")
    Set oBag=oAPI.CreatePropertyBag()
    Call oBag.AddValue("Success", varSuccess)
    Call oBag.AddValue("Error message", varErrorMessage)
    Call oAPI.Return(oBag)
  4. Configure the healthy, unhealthy and other components as appropriate.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *