Microsoft`s Strategy to Centralize and Manage Automated Installations: SMS 2003 - A sample WMI script to connect and retrieve SMS information
(Page 5 of 6 )
As this is an introductory article, I simply wanted to demonstrate how you connect and retrieve SMS information using VBScript together with WMI.
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set smsserver = locator.ConnectServer("servername", "rootsms")
Set info = smsserver.ExecQuery("SELECT * From SMS_ProviderLocation")
For each i in info
sitecode = i.SiteCode
Wscript.Echo sitecode
Next
Set smsserver = Nothing
The above would simply list all the sms site codes. Let me explain the code part by part. I hope by now you can already understand the first two statements. The third statement is as follows:
Set info = smsserver.ExecQuery("SELECT * From SMS_ProviderLocation")
The above code uses WQL (WMI Query Language). The serverobject (smsserver) supports a method "ExecQuery" to execute any SELECT statement (conforming to the semantics of WMI Query Language) which can return a handful of information (either in the form of a table or objects or properties). Once we receive information, we need to go through all those objects and display them on the screen. The following code fragment does the same.
For each i in info
sitecode = i.SiteCode
Wscript.Echo sitecode
Next
It is always a good practice to release memory resources, once we complete our work. That means the connection to the SMS server that we established in the beginning is no longer necessary. The following statement would release all such memory resources:
Set smsserver = Nothing
Let us extend the above a bit. The beauty of the above code is that we can directly use it in ASP for web development as follows:
<html>
<body>
<%
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set smsserver = locator.ConnectServer("servername", "rootsms")
Set info = smsserver.ExecQuery("SELECT * From SMS_ProviderLocation")
For each i in info
sitecode = i.SiteCode
<%= sitecode %>
Next
Set smsserver = Nothing
%>
</body>
</html>
It is that easy. You can simply imagine that any VBScript script can be converted to ASP with a very few basics.
Next: Another Feature Pack from Microsoft to ease SMS deployments >>
More BrainDump Articles
More By Jagadish Chaterjee