Using SOAP with VBScript

(imported topic written by r11ce91)

Hello.

I’m trying to understand how to use the SOAP interface from within VBScript to query Web Reports.

As a very simple test I’m using the following code to establish a conneciton and run a basic relevance query. The script connects but only returns the first line of results.

Dim client Dim result   Set client = CreateObject(
"MSSOAP.SoapClient") client.MSSoapInit 
"http://WEB_REPORTS_SERVER/webreports?wsdl"   result = client.GetRelevanceResult ( 
"names of bes computers", 
"USERNAME", 
"PASSWORD")   Wscript.echo result

I have a feeling my issue has to do with

GetRelevanceResultResponse

as it is used in the sample Perl code provided in the Web Reports Guide.

foreach my $answer ( $result->valueof( 
"//GetRelevanceResultResponse/a" ) ) 
{ print $answer . 
"\n"; 
}

Does anyone know how I could translate the Perl implementation in the Web Reports Guide for use in VBScript?

Any help is greatly appreciated.

(imported comment written by BenKus)

Hi r11ce,

I don’t know the answer and this is basically cheating, but you can do this:

Dim client
Dim result

Set client = CreateObject(“MSSOAP.SoapClient”)
client.MSSoapInit “http://url/webreports?wsdl"
result = client.GetRelevanceResult ( “concatenation “”–”” of names of bes computers", “ben”, “password”)
results = Split(result, “–”)

For Each res in results
Wscript.echo res
Next

Ben

(imported comment written by MattBoyd)

Here you go:

'===================================================================== 
'     BigFix Web Reports SOAP API Example 
' Author:         Matthew Boyd (iboyd.net) 
'    Created:        10/20/2009 
'  Modified:       7/2/2010 
'    Version: 1.1 
' 
'    This is  an example of how to use the Web Reports SOAP  
'     API to retrieve information about clients in BigFix. It is  
' written in VBScript. It should be called from a command 
'     line prompt using the syntax in the line below.   
' 
'       Usage:  cscript.exe WebReportsSoapExample.vbs  
' 
'  Note: You must set the username and password values  
'        to an actual Web Reports username and password in order  
'    for the script to successfully complete. The script was  
'    tested and worked with Web Reports version 7.2.4.60. 
'=====================================================================   Dim SoapRequest, username, password, relevance, url, objHTTP, result, i, objXML, NodeList   url = 
"https://web reports server:52312/webreports" 
'The URL of the BigFix Web Reports site username = 
"abc123" 
' The web reports username that is used for authentication. password = 
"mypassword" 
' The password of the Web Reports account 
''
' This is where the relevance string is set. You can set it to any relevance you'd like. Remember, quotes in the relevance clause must be escaped by using two quotes (see below near: name of it starts with 
"CB") relevance = 
"(name of it, ip address of it, (last report time of it) as universal string) of bes computers whose (name of it starts with "
"CB"
")"   
''
'This  section builds the Soap Request Body  that will be sent to the Web Reports web server SoapRequest = 
"<?xml version="
"1.0"
" encoding="
"utf-8"
"?>" SoapRequest = SoapRequest & 
"<soap12:Envelope xmlns:xsi="
"http://www.w3.org/2001/XMLSchema-instance"
" xmlns:xsd="
"http://www.w3.org/2001/XMLSchema"
" xmlns:soap12="
"http://www.w3.org/2003/05/soap-envelope"
"> " SoapRequest = SoapRequest & 
"<soap12:Body>"   
''
'This section contains the actual call to the GetRelevanceResult method. SoapRequest = SoapRequest & 
"<GetRelevanceResult xmlns="
"https://schemas.bigfix.com/Relevance"
">" SoapRequest = SoapRequest & 
"<relevanceExpr><![CDATA["& relevance & 
"]]></relevanceExpr>" 
'This is the relevance parameter. The relevance expression must be enclosed in <relevanceExpr></relevanceExpr> brackets. SoapRequest = SoapRequest & 
"<username>"& username & 
"</username>" 
'This is the username parameter in the Soap Request. Notice the credentials are part of the SOAP request. This is why all Web Reports SOAP request (or any authentication to Web Reports) should be done over HTTPS! Otherwise, they are passed in clear text. SoapRequest = SoapRequest & 
"<password>"& password & 
"</password>" 
'This is the password parameter in the Soap Request. SoapRequest = SoapRequest & 
"</GetRelevanceResult>"   
''
'This section cloases the Soap Request Body SoapRequest = SoapRequest & 
"</soap12:Body>" SoapRequest = SoapRequest & 
"</soap12:Envelope>"   
''
'Now that we've built the Soap request, we need to send it over HTTP. This section handles that. Set objHTTP = CreateObject(
"Msxml2.XMLHTTP") 
'Create an HTTP object to send the SOAP Request to the BigFix Server objHTTP.open 
"POST", URL, 

false 
'Configure the HTTP object to use POST, point it to the Web Reports URL. objHTTP.setRequestHeader 
"Content-Type", 
"text/xml" objHTTP.setRequestHeader 
"SOAPAction", url & 
"GetRelevanceResultResponse" objHTTP.send SoapRequest 
'Sendthe Soap Request to the Web Reports Server, collect the result.   result = objHTTP.responseText  
'At this point we have the result of the relevance query. However, it's in XML format, with each result record between <a></a> tags.  We can extract 

this information using a DOMDocument object (see below)   
''
'This section extracts the text data from the result, which is in XML format. We do this by loading the result string into an MSxml2.DOMDocument object and then using SelectNodes with an XPATH statement to select all nodes enclodsed in <a></a> tags. Set objXML = CreateObject (
"Msxml2.DOMDocument") objXML.LoadXml result 
'Load the result from the SOAP Request     If not objXML.SelectSinglenode (
"//faultstring") is nothing then 
'If a faultstring node exists in the XML, GetRelevanceResult returned an error. Raise an exception with the source and description. Err.Raise 1,  
"GetRelevanceResult:" & objXML.SelectSingleNode(
"//faultcode").text, Trim(objXML.SelectSingleNode(
"//faultstring").text) End If   Set NodeList = objXML.SelectNodes(
"//a") 
'Select the nodes enclosed in <a></a> from the XML. Load into NodeList object. wscript.echo NodeList.length For i = 0 to NodeList.length - 1 
'Iterate through each node in the NodeList object, write the text contents to the console. wscript.echo NodeList.Item(i).text Next

By the way, for nice syntax highlighting, I usually use Notepad++

(imported comment written by cstoneba)

very nice. I put it into an html page so now it pulls data straight into IE (one step closer to my Sharepoint page showing BigFix stats). The only downside is that someone can right click > view source > and see see the uname and passwd.

(imported comment written by MattBoyd)

Glad you like it. You could probably fix that username and password issue by converting it to an ASP page… assuming you are running IIS.

(imported comment written by cstoneba)

yup, just did that. Pretty slick…

(imported comment written by r11ce91)

Thanks for everyone’s input…it was very helpful.

@Ben Your trick worked but I wonder if it will come back and bite me as the relevance queries get more complex.

@boyd Thank you very much for sharing your code. From what I gather you create your own SOAP message and parse the return XML. Did you go this route from the start? Did you ever try with the MSSOAP.SoapClient object?

Looking at your code got me thinking about MSSOAP.SoapClient object. Since it is only returning the first line of the results it must mean the results are multi lined and cannot be fed into a variable. My VB skills are very limited so I’m not sure what to do with a multi lined result.

I’ll keep hacking away and report back if I find a way to get this going with MSSOAP.SoapClient.

Thanks again everyone.

(imported comment written by MattBoyd)

r11ce

@boyd Thank you very much for sharing your code. From what I gather you create your own SOAP message and parse the return XML. Did you go this route from the start? Did you ever try with the MSSOAP.SoapClient object?

I did try MSSOAP.SoapClient, but decided to create the SOAP message from scratch. Generally, I prefer to use premade APIs, frameworks, libraries, etc. instead of

reinventing the wheel

. It’s probably easier to call MSSOAP.SoapClient. But AFAIK, MSSOAP.SoapClient doesn’t work on all versions of Windows out of the box. For example, I’m running Win7 x64 on my dev box, and I receive an error message because the ActiveX object can’t be created. The Web Reports SOAP API call is simple, so it was just as easy to create the request from scratch, rather than deal with MSSOAP.SoapClient.

(imported comment written by cstoneba)

any reason why using “&” in my relevance statment clears my reults?

relevance = “(number of unique values of names of applicable computers of bes fixlets whose (source of it as lowercase = ““microsoft”” AND source severity of it as lowercase = ““critical”” )as string) & html “”/”" & (number of bes computers) "

(imported comment written by Lee Wei)

What error are you getting and what is meant by the results being cleared?

I suspect because the relevance statement needs to be modified.

You are concatenating 3 strings, but the last one returns a number.

So try this. Note “number of bes computers as string”.

relevance = “(number of unique values of names of applicable computers of bes fixlets whose (source of it as lowercase = ““microsoft”” AND source severity of it as lowercase = ““critical”” )as string) & html “”/”" & (number of bes computers as string) "

(imported comment written by cstoneba)

That’s just it, i’m not getting any errors back when I have “&” in my relevance. I used your code from post#10 and got the same thing. It seems to work fine in my presentation debugger, but not here. If you’d like, I can send you a copy of my asp page offline.

(imported comment written by Lee Wei)

My next guess is that you have the correct relevance statement, but you have to make sure it is sent correctly through your programming environment.

Ampersand & might mean something in your code needing to be escaped properly.

(imported comment written by MattBoyd)

Yikes, I guess I need to add some CDATA tags to the XML SOAP request. That’s another reason to consider using MSSOAP.SoapClient, since it probably deals with special characters for you.

Here’s a modified version. I added some logic that will cause the VBScript to raise an error if GetRelevanceResult returns an error.

'===================================================================== 
'        BigFix Web Reports SOAP API Example 
' Author:         Matthew Boyd (iboyd.net) 
'    Created:        10/20/2009 
'  Modified:       7/2/2010 
'    Version: 1.1 
' 
'    This is  an example of how to use the Web Reports SOAP  
'     API to retrieve information about clients in BigFix. It is  
' written in VBScript. It should be called from a command 
'     line prompt using the syntax in the line below.   
' 
'       Usage:  cscript.exe WebReportsSoapExample.vbs  
' 
'  Note: You must set the username and password values  
'        to an actual Web Reports username and password in order  
'    for the script to successfully complete. The script was  
'    tested and worked with Web Reports version 7.2.4.60. 
'=====================================================================   Dim SoapRequest, username, password, relevance, url, objHTTP, result, i, objXML, NodeList   url = 
"https://web reports server:52312/webreports" 
'The URL of the BigFix Web Reports site username = 
"abc123" 
' The web reports username that is used for authentication. password = 
"mypassword" 
' The password of the Web Reports account 
''
' This is where the relevance string is set. You can set it to any relevance you'd like. Remember, quotes in the relevance clause must be escaped by using two quotes (see below near: name of it starts with 
"CB") relevance = 
"(name of it, ip address of it, (last report time of it) as universal string) of bes computers whose (name of it starts with "
"CB"
")"   
''
'This  section builds the Soap Request Body  that will be sent to the Web Reports web server SoapRequest = 
"<?xml version="
"1.0"
" encoding="
"utf-8"
"?>" SoapRequest = SoapRequest & 
"<soap12:Envelope xmlns:xsi="
"http://www.w3.org/2001/XMLSchema-instance"
" xmlns:xsd="
"http://www.w3.org/2001/XMLSchema"
" xmlns:soap12="
"http://www.w3.org/2003/05/soap-envelope"
"> " SoapRequest = SoapRequest & 
"<soap12:Body>"   
''
'This section contains the actual call to the GetRelevanceResult method. SoapRequest = SoapRequest & 
"<GetRelevanceResult xmlns="
"https://schemas.bigfix.com/Relevance"
">" SoapRequest = SoapRequest & 
"<relevanceExpr><![CDATA["& relevance & 
"]]></relevanceExpr>" 
'This is the relevance parameter. The relevance expression must be enclosed in <relevanceExpr></relevanceExpr> brackets. SoapRequest = SoapRequest & 
"<username>"& username & 
"</username>" 
'This is the username parameter in the Soap Request. Notice the credentials are part of the SOAP request. This is why all Web Reports SOAP request (or any authentication to Web Reports) should be done over HTTPS! Otherwise, they are passed in clear text. SoapRequest = SoapRequest & 
"<password>"& password & 
"</password>" 
'This is the password parameter in the Soap Request. SoapRequest = SoapRequest & 
"</GetRelevanceResult>"   
''
'This section cloases the Soap Request Body SoapRequest = SoapRequest & 
"</soap12:Body>" SoapRequest = SoapRequest & 
"</soap12:Envelope>"   
''
'Now that we've built the Soap request, we need to send it over HTTP. This section handles that. Set objHTTP = CreateObject(
"Msxml2.XMLHTTP") 
'Create an HTTP object to send the SOAP Request to the BigFix Server objHTTP.open 
"POST", URL, 

false 
'Configure the HTTP object to use POST, point it to the Web Reports URL. objHTTP.setRequestHeader 
"Content-Type", 
"text/xml" objHTTP.setRequestHeader 
"SOAPAction", url & 
"GetRelevanceResultResponse" objHTTP.send SoapRequest 
'Sendthe Soap Request to the Web Reports Server, collect the result.   result = objHTTP.responseText  
'At this point we have the result of the relevance query. However, it's in XML format, with each result record between <a></a> tags.  We can extract 

this information using a DOMDocument object (see below)   
''
'This section extracts the text data from the result, which is in XML format. We do this by loading the result string into an MSxml2.DOMDocument object and then using SelectNodes with an XPATH statement to select all nodes enclodsed in <a></a> tags. Set objXML = CreateObject (
"Msxml2.DOMDocument") objXML.LoadXml result 
'Load the result from the SOAP Request     If not objXML.SelectSinglenode (
"//faultstring") is nothing then 
'If a faultstring node exists in the XML, GetRelevanceResult returned an error. Raise an exception with the source and description. Err.Raise 1,  
"GetRelevanceResult:" & objXML.SelectSingleNode(
"//faultcode").text, Trim(objXML.SelectSingleNode(
"//faultstring").text) End If   Set NodeList = objXML.SelectNodes(
"//a") 
'Select the nodes enclosed in <a></a> from the XML. Load into NodeList object. wscript.echo NodeList.length For i = 0 to NodeList.length - 1 
'Iterate through each node in the NodeList object, write the text contents to the console. wscript.echo NodeList.Item(i).text Next
1 Like

(imported comment written by cstoneba)

works like a champ now. Thanks boyd.

(imported comment written by r11ce91)

cstoneba

any reason why using “&” in my relevance statment clears my reults?

relevance = “(number of unique values of names of applicable computers of bes fixlets whose (source of it as lowercase = ““microsoft”” AND source severity of it as lowercase = ““critical”” )as string) & html “”/”" & (number of bes computers) "

You need to replace “&” with “&” inside of your relevance query since the “&” is a special character in HTML/XML.

(imported comment written by cstoneba)

having boy add the CDATA tags resolved my issue, but thanks r11ce.