giving something back after taking a lot :-)

Maik Koster at myITforum.com

Calling a webservice from a script using MDT 2008

December 20, 2008

The MDT Development Team did a great job on writing their “UnknownComputer” Webservice. But they also wrote the corresponding scripts which enables you to use almost any Webservice from any script without much effort. Why would we need webservices you might ask. Well, it just gives you another way of getting or transforming values. Sometimes webservices already exists, sometimes the security settings can be difficult to get access to sql servers or it is just easier to implement a special function centrally. Especially if it might be changed from time to time without the need to adapt the scripts every time.

In any case, it's not bad being able to consume information on another way and now see how we get this thing working:

To use a webservice you need to have three things:

  • A functional webservice which can be accessed by anonymous users (we really don`t want to bother with Security at this point)
  • A *.ini file which contains the information on how to connect to the webservice
  • A *.wsf scripts which references the ZTIUtility.vbs from the MDT 2008

We assume the webservice has been set up and is working. For demonstration purpose we just use a public Webservice which will take a temperature in one Unit and convert it to another unit. Not really helpful on MDT but it should point out the benefits from using webservices . (a future article on how to create your own Webservice is on it's way already).

So create a folder for your testing and copy the ZTIUtiliy.vbs (from MDT 2008) into this folder. Create a “MDTWebservice.ini” file (just an example, any textfile will work) in this folder and add the following information into this file:

[ConvertTemperature]
WebService=http://www.webservicex.net/ConvertTemperature.asmx/ConvertTemp
Parameters=MyTemperature,FromUnit,ToUnit
MyTemperature=Temperature

Sometimes (probably most of the time) the names of the values from the Webservice doesn't fit the names used in the scripts. If they don't match you need to assign them to each other as seen on the "MyTemperature" Variable which is used in the script, but the webservice would like to have a "Temperature" value. Just take care about, that the definition of the values seems to be "swapped". So you need to define your variable names on Parameters and then assign the values from the webservice to your Variable names if they don't match exactly.

And last we need a script which executes this Webservice. So in your Testfolder create a file “ConvertTemperature.wsf”

I will just outline the basic structure, leaving out all wsf and mdt specific parts. The complete script can be downloaded a the end of this post. It's pretty self-explaining (at least I hope so). In the Script we just set our Variables. Here we use the MDT way for storing Environment variables to be used from different scripts. It will also allow us to persist the values even throughout reboots.

oEnvironment.Item("MyTemperature") = "68"
oEnvironment.Item("FromUnit") = "degreeFahrenheit"
oEnvironment.Item("ToUnit") = "degreeCelsius"

 

No we call our function which will execute the webservice. For further processing we store the value from the Function in a Variable

MyTemp = ConvertTemperature

 

The "ConvertTemperature" Function just needs a couple lines of code. First we create an instance of the "WebService" Class from MDT 2008.

Set oService = new WebService
oService.IniFile = "MDTWebservice.ini"
oService.SectionName = "ConvertTemperature"

 

Then we simply query the webservice

Set oXML = oService.Query

 

and process the result.

If oXML Is Nothing then
     oLogging.CreateEntry "Unable to call ConvertTemperature web service.", LogTypeWarning
Else
     ToTemp = CDbl(oXML.SelectSingleNode("double").Text)
End if

 

Processing the result can be quite challenging but that's up to you now :-)

If we now execute the script we get a pretty neat output, telling us everything was working as supposed (the logs can be found on C:\MININT\SMSOSD\OSDLOGS as we use the MDT logging)

Calling_a_Webservice_Log2

The rest is just getting some flesh on the bones. See the ZTI_MediaHook from the MDT 2008 for a real-life implementation. We will have a deeper look on this in a later post. Also check the MDT 2008 Help documentation. There is an example on how to use a webservice directly from the customsettings.ini.

Download the scripts.

Comments

  • No Comments