VBScript

The example VBScript builds a properly formatted SOAP envelope that is posted to the web service API which in turn sends back a response. This response is used to determine success or failure of the web service call.

The script can be reused for other methods by changing the Method name and the appropriate parameters for the SOAP envelope.

strAppPortalServer = "WS07"

Method = "createNewCatalogItem"

 

URL = "http://" & strAppPortalServer & "/esd/api.asmx"

'Do not change this line

nsUrl = "http://sccmexpert.local/API"

 

Title = "Sample API Application"

Description = "Sample Description"

Visible = "true"

ImageFileName = "software.png"

PackageServer = "SMS01"

SMSPackageID = "SMX00A29"

SMSPackageData = "0|0|InstallInteractive"

TemplatePackageID = "846"

CategoryNames = "Software,Adobe"

GroupNames = "smx\domain admins"

MembershipType = "1"

CreateInventoryRecord = "true"

SCCMInventoryAttributes = ""

 

' Create the http request text

Dim strSoapReq

strSoapReq = GenerateSoapBodyStart()

strSoapReq = strSoapReq + GenerateSoapFunctionCallStart(Method)

strSoapReq = strSoapReq + GenerateSoapParameter("Title", Title)

strSoapReq = strSoapReq + GenerateSoapParameter("Description", Description)

strSoapReq = strSoapReq + GenerateSoapParameter("Visible", Visible)

strSoapReq = strSoapReq + GenerateSoapParameter("ImageFileName", ImageFileName)

strSoapReq = strSoapReq + GenerateSoapParameter("PackageServer", PackageServer)

strSoapReq = strSoapReq + GenerateSoapParameter("SMSPackageID", SMSPackageID)

strSoapReq = strSoapReq + GenerateSoapParameter("SMSPackageData", SMSPackageData)

strSoapReq = strSoapReq + GenerateSoapParameter("TemplatePackageID", TemplatePackageID)

strSoapReq = strSoapReq + GenerateSoapParameter("CategoryNames", CategoryNames)

strSoapReq = strSoapReq + GenerateSoapParameter("GroupNames", GroupNames)

strSoapReq = strSoapReq + GenerateSoapParameter("MembershipType", MembershipType)

strSoapReq = strSoapReq + GenerateSoapParameter("CreateInventoryRecord", CreateInventoryRecord)

strSoapReq = strSoapReq + GenerateSoapParameter("SCCMInventoryAttributes", SCCMInventoryAttributes)

strSoapReq = strSoapReq + GenerateSoapFunctionCallEnd(Method)

strSoapReq = strSoapReq + GenerateSoapBodyEnd()

 

Dim oHttp

Dim strResult

Set oHttp = CreateObject("Msxml2.XMLHTTP")

oHttp.open "POST", URL, False

oHttp.setRequestHeader "Content-Type", "text/xml"

oHttp.setRequestHeader "SOAPAction", nsUrl + "/" & Method

oHttp.send strSoapReq

 

strResult = oHttp.responseText

 

WScript.echo(GetResult(strResult, Method & "Result"))

 

 

Function GetResult(byval responseText, byval resultParam)

    

    Dim oXml

    Set oXml = CreateObject("Msxml2.DOMDocument")

    oXml.Async = False

    oXml.LoadXml responseText

    

    Dim strPath

    strPath = "/*/*/*/" + resultParam

    Dim oNode

    Set oNode = oXml.documentElement.SelectSingleNode(strPath)

    GetResult = oNode.Text

    

End Function 

 

 

Function GenerateSoapBodyStart()

    

    Dim strSoap

    strSoap = "<?xml version=""1.0"" encoding=""utf-8""?>"

    strSoap = strSoap + "<soap12:Envelope "

    strSoap = strSoap + "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" "

    strSoap = strSoap + "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" "

    strSoap = strSoap + "xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">"

    strSoap = strSoap + "<soap12:Body>"

    GenerateSoapBodyStart = strSoap

    

End Function 

 

Function GenerateSoapBodyEnd()

    

    Dim strSoap

    strSoap = "</soap12:Body>"

    strSoap = strSoap + "</soap12:Envelope>"

    GenerateSoapBodyEnd = strSoap

    

End Function

 

Function GenerateSoapFunctionCallStart(byval strFunction)

    

    Dim strSoap

    strSoap = "<" + strFunction + " xmlns=""" + nsUrl + """>"

    GenerateSoapFunctionCallStart = strSoap

    

End Function 

 

Function GenerateSoapFunctionCallEnd(byval strFunction)

    

    Dim strSoap

    strSoap = "</" + strFunction + ">"

    GenerateSoapFunctionCallEnd = strSoap

    

End Function 

 

Function GenerateSoapParameter(byval strParam, byval strValue)

    

    If strValue = "True" Then

        strValue = "true"

    End If

    

    If strValue = "False" Then

        strValue = "false"

    End If

    

    Dim strSoap

    

    strSoap = "<" + strParam + ">"

    strSoap = strSoap + strValue

    strSoap = strSoap + "</" + strParam + ">"

    

    GenerateSoapParameter = strSoap

    

End Function