powershellvbscriptquotes

Calling a PowerShell script from within a VBScript and the use of quotes in the variable passed (JSON format variable)


I am neither a seasoned VBScript or PS scripting specialist. My monitoring system has a controlling VBScript for opening support tickets, and it now needs to change to accommodate JSON output posted to a service.

I first attempted to POST the JSON string directly from within the VBScript, but I always get an error "msxml3.dll: A connection with the server could not be established" that I cannot seem to fix. So, I tested the JSON POST from PS, and it worked straight away.

In my infinite wisdom, I then decided to call the PS script from within the VBScript, but that has opened its own can of worms. The two biggest worms I'm referring to here are 1) trying to get the call right, and 2) dealing with quotes.

This is the PS call from within my VBS:

strOut = "{""integrationName"":""WhatsUpGold"",""actionName"":""WhatsUpGold"",""sourceId"":""" & LogID & """,""payload"":{""UID"":""" & LogID & """,""MD"":""SPNVMSA116"",""Agent_Class"":""WhatsUp Gold"",""Agent_Instance"":""" & mon_name & """,""Client"":""BCX"",""Date"":""" & ActMonStartTimeDate & """,""Time"":""" & ActMonStartTimeTime & """,""ESMTool"":""WhatsUp Gold"",""Severity"":""" & state & """,""State"":""" & mon_state_override & """,""Server"":""" & disp_name & """,""Instance_Detail"":""Server"",""UserData"":""" & payload & """}}"

Context.LogMessage mon_state & " " & disp_name & " -ACTIVE " &  mon_name & " monitor alert:           " & strOut

Dim objShell, intReturnCode
Set objShell = CreateObject("Wscript.Shell")
intReturnCode = objShell.Run("powershell.exe -noexit set-ExecutionPolicy Unrestricted -c .\c:\SXI\SXI_post_active3.ps1 " & strOut)
MsgBox intReturnCode

This is my PS script:

******************************************************************************************
$content = $args[0]

[hashtable]$headers=@{}

$headers.Add("Authorization", "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
$headers.Add('Content-Type', 'application/json')
$headers.Add('Accept', 'application/json')

$uri = "https://mywebservice"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Invoke-RestMethod -Method 'POST' -Uri $uri -Headers $headers -Body $content

*****************************************************************************************

The variable strOut in my VBScript passes JSON test, so I thought I would be good to go, but when I execute my VBS I get the following error from the PS script output:

At line:1 char:95
+ ... c .\c:\SXI\SXI_post_active3.ps1 {integrationName:WhatsUpGold,actionNa ...
+                                                                 ~
Missing argument in parameter list.
At line:1 char:156
+ ... actionName:WhatsUpGold,sourceId:4904575,payload:{UID:4904575,MD:SPNVM ...
+                                                                 ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument

As can be seen, the JSON was good in my VBSCript output, but PS has stripped out the quotes. I think I've come to the end of my tether. I'm also not sure whether I've called the PS script correctly from within my VBScript. Any pointers would be helpful.


Solution

  • I would probably consider letting PowerShell handle creating the json payload instead and just pass in what it needs as arguments to the script

    Example vbscript code (since PowerShell accepts single-quotes use around arguments for simplicity )

    Dim strLogId, strMonName, strActMonStartTimeDate, strActMonStartTimeTime
    strLogId = "LogIDA"
    strMonName = "MonNameA"
    strActMonStartTimeDate = "2022-04-23"
    strActMonStartTimeTime = "23:12:12"
    intReturnCode = objShell.Run("powershell.exe -noprofile -noexit -ExecutionPolicy Unrestricted -Command c:\temp\b.ps1 '" & strLogId & "' '" & strMonName & "' '" & strActMonStartTimeDate & "' '" & strActMonStartTimeTime & "'")
    'MsgBox intReturnCode
    

    Example PowerShell script

    param(
        $LogID,
        $mon_name,
        $ActMonStartTimeDate,
        $ActMonStartTimeTime
    )
    
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $true 
    
    $content = [pscustomobject]@{
        integrationName = 'WhatsUpGold'
        actionName      = 'WhatsUpGold' 
        sourceId        = $LogID
        payload         = [pscustomobject]@{
            uid = $LogID; MD = 'SPNVMSA116'
            Agent_Class = 'WhatsUp Gold'
            Agent_Instance = $mon_name
            Client = 'BCX'
            Date = $ActMonStartTimeDate
            Time = $ActMonStartTimeTime
            ESMTool = 'WhatsUp Gold' 
        } 
    } | ConvertTo-Json 
    
    
    [hashtable]$headers = @{}
    
    $headers.Add('Authorization', 'Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') $headers.Add('Content-Type', 'application/json') $headers.Add('Accept', 'application/json')
    
    $uri = 'https://mywebservice'
    
    Invoke-RestMethod -Method 'POST' -Uri $uri -Headers $headers -Body $content