
The following article is based on a pre-release version of the Microsoft Windows Vista operating system. That means that the features discussed in the article are subject to change: some may not be included in the final product due to marketing, technical, or other reasons. We will do our best to update this article if and when such changes occur.
Each year at least one movie gets nominated for half a dozen or more Academy Awards, including all of the big ones: Best Picture, Best Director, Best Actor, Best Actress. That’s great, except that every so often the principals involved find themselves sitting in the audience on Oscar night, pretending to smile and applauding politely as some other movie rakes in all the major awards. Best Picture or Best Director? Well, no. But we did win for Best Sound Recording and Best Costume Design.
How … nice ….
Script writers know this feeling all too well. Each time a new operating system is announced script writers sit there with their eyes closed and their fingers crossed, hoping that this time one of the major announcements will be targeted towards them. And each time a new operating system is announced script writers inevitably end up disappointed. Sure, there are always new Windows Management Instrumentation (WMI) classes and a handful of new COM objects to play with, and that’s great: the more new things the better. Unfortunately, getting a few new WMI classes is a bit like winning the award for Best Sound Recording: although you’re happy to have won anything, you still can’t help being a little disappointed over the really cool things that other people walked away with. Just once you’d like to be the big winner, just once you’d like the new cutting-edge innovation to be targeted towards you. Best Sound Recording is great, but just once you’d like to win Best Picture or Best Director.
You might not know this, but if you’re a script writer, you’ve been waiting all your life for Windows Vista.
You heard correctly. Windows Vista is filled with all sorts of new things, and one of the coolest and most-anticipated features – the Windows Sidebar – is aimed squarely at script writers. The Windows Sidebar is an area of the screen that has been reserved for “gadgets,” much in the same way that the Quick Launch bar is an area of the screen that has been reserved for shortcuts. Gadgets are mini-applications, little bits of code that can do just about anything the gadget creator wants them to do. For example, Windows Vista ships with several gadgets, including a rudimentary little calculator and a pair of gadgets for monitoring RSS feeds. A new community is also springing up around gadgets; for more information (and to download gadgets created by community members) see the Microsoft Gadgets Web site. You don’t have to spend much time browsing through the gadget gallery before you realize how cool (and how useful) gadgets are likely to be.
But let’s be honest: the world is filled with cool things that other people can create. What makes gadgets really interesting for us is that this new technology has been designed for people who write scripts. Don’t get us wrong: gadgets are cool, gadgets can have a very cutting-edge feel to them, and gadgets can carry out some very sophisticated tasks. And yet, underneath that hip-hop cover, gadgets turn out to be nothing more than miniature HTML pages: put together a simple HTML page, mix in a little VBScript code (including WMI code), toss in a dash of the Gadget object model (to be covered in Part II of this series) and you’ve got yourself a gadget. Gadgets are one of the hot new features of Windows Vista, and scripters are just the people to start creating these gadgets.
You know, that’s a good idea. Maybe it’s time for a less talk and a little more action; let’s see if we can help you create your first gadget.
One of the criticisms leveled at Microsoft (and yes, believe it or not some people have criticized Microsoft in the past) is that many of our products were apparently designed to be used by robots or by aliens from the planet Omicron IV; at any rate, they were not designed to be used by human beings. Happily that’s not the case for Microsoft gadgets. (Although if you happen to be either a robot or an alien from the planet Omicron IV rest assured that you can easily create gadgets as well.) To create a gadget you need only two items:
| • | A “manifest” file named Gadget.xml. This manifest contains all the settings for your gadget, including the gadget name, author and copyright information, and information about the HTML page that makes up the actual gadget. |
| • | An HTML file (for example, Test.htm). Although they might not look like it, gadgets are really nothing more than HTML files: you simply create an HTML file, add the appropriate tags and script code, and you’ve got yourself a gadget. |
Note. Yes, we know: you have no idea what we mean by “the appropriate tags and script code.” Relax; that’s what the rest of this article is for. |
Those are the only items required to create a gadget. Granted, as you create more sophisticated gadgets you will find yourself dealing with icon files, graphics files, settings files, and other elements. But we’ll cross those bridges as we come to them.
See how easy that is? What do you mean, “Sure, so far”? Point well taken: this would seem to be the time when you have to do some sort of horrendously-complicated compilation process, probably using some proprietary compiler that the Scripting Guys will sell you for just $39.95 plus shipping and handling. (Actually, now that you mention that we wish we would have thought of doing something like that.) But instead of using some proprietary compiler here’s how you gather up all your files and then “compile” them into a gadget:
1. | Place all the files in a gadgets folder. |
That’s it: put all the files in a folder and you’ve got yourself a gadget. No compiling, no compilers, nothing more tedious or technical than simply copying files to a specified folder.
Incidentally, if you still want to send us $39.95 we’ll be happy to take it.
Of course, there is a slight catch here: you can’t just put your files in any old folder. Instead, you have to follow this procedure:
To begin with, bring up the Gadgets folder. A quick way to access your Gadgets folder is to type the following in the Run dialog box:
%userprofile%\appdata\local\microsoft\windows sidebar\gadgets
Note. What do you mean you can’t find the Run command on the Start menu? Oh, that’s right: for some reason this command is hidden by default. But that’s OK; to get the Run command back just right-click the Start button and then click Properties. In the Taskbar and Start Menu Properties dialog box, on the Start Menu tab, click Customize. Still with us? Good. Once you’re in the Customize Start Menu dialog box scroll down and check the Run command check box. Click OK a couple times and the Run command will be back to its rightful place. And, yes, usually the Scripting Guys do charge $39.95 for this kind of inside information. But we’ll let you have this one for free. |
Inside the Gadgets folder create a new folder. Give this folder any name you want, provided that the name ends with a .gadget file extension (for example, Test.gadget). Obviously it will make your life easier if the name of the folder bears some resemblance to the gadget contained within. However, the operating system merely uses this folder to identify that the files inside make up a gadget; the gadget name is derived from information found in the manifest, not from the folder name (as the Scripting Guys discovered the hard way).
All you have to do now is place all your files (such as Gadget.xml and Test.htm) in the folder. Does that mean you now have a Microsoft Gadget? You bet it does. In fact, with Windows Sidebar up and running click the + button to bring up the set of gadgets found on your computer. Your gadget will show in the gadget picker dialog box.
Note. What’s that? You can’t find the Windows Sidebar, either? That’s OK; if you can’t find the Windows Sidebar just do this: click the Start menu and then click All Programs. Click Accessories, and you should see a link to the Sidebar. |
Installing a Gadget
Assuming your gadget really does show up in the list of available gadgets, you can install the thing simply by dragging the icon onto the Windows Sidebar and then releasing the mouse button. (Or just right-click the gadget icon and then click Add.) The gadget will be displayed, and you’ll be ready to start using it. If you decide to remove the gadget from the Sidebar just hold the mouse over the thing and click the little X that appears in the upper right-hand corner. If you aren’t sure which little X we’re talking about, well, it’s this one:

As we noted earlier, to create a gadget you need only two things: a manifest file and an HTML file. And we know what you’re thinking: sure, on Omicron IV you guys probably use manifest files all the time. (Some of us do, some of us don’t.) But what do system administrators know about manifest files?
Well, one thing you know (or at least now you know) is that “manifest file” is simply a high-falutin’ technical term; in essence we’re really just talking about something like an .INI file, a simple text file (in this case, one done in XML) that contains configuration information for the gadget. Admittedly, the idea of having to write things in XML might send a shiver or two up and down your spine. Listen, don’t worry about it; this is about as simple and barebones an XML file as you’ll ever have to deal with.
Here’s what we mean:
<?xml version="1.0" encoding="utf-8" ?>
<gadget>
<name>My First Gadget</name>
<author>The Microsoft Scripting Guys</author>
<copyright>2006 Microsoft Corporation</copyright>
<description>Sample gadget that returns the name of the installed operating system.</description>
<icons>
<icon>icon.png</icon>
</icons>
<version value="1.0.0.0" MinPlatformVersion="0.1"></version>
<sidebar>
<type>html</type>
<permissions>full</permissions>
<code>test.htm</code>
<website>www.microsoft.com/technet/scriptcenter</website>
</sidebar>
</gadget>
Before you ask, yes, you can simply copy this file and use it pretty much as-is, just making changes (as needed) to a few of the tag values. (And don’t forget, you must name the file Gadget.xml.) The tags you might want to/need to modify are specified in the following table:
Tag | Description |
<name> | Name of the gadget as it appears in the gadget picker dialog box. |
<author> | Name of the person who wrote the gadget. The author, copyright, and description tags all appear in the gadget picker when you click on a particular gadget. For details, see the illustration shown below. |
<copyright> | Copyright information, including name of the copyright holder and copyright date. |
<description> | Brief description of the gadget and what it does. |
<icon> | Name of the icon file (the icon is the graphic displayed in the gadget picker). For more on icons, see Creating an Icon in the following subsection of this document. |
<code> | Probably not the most intuitive tag name in the world, but this is the name of the HTML file that makes up your gadget. |
<website> | Web site associated with the gadget. |
To help make this all a little plainer, here are the manifest file elements mapped to the items displayed in the gadget picker dialog box:

Creating an Icon
It’s up to you whether you want to supply a custom icon with your gadget; if you don’t supply an icon (or if you don’t specify an icon in the manifest file) the gadget picker will provide you with a default icon. (At no charge to you.)
If you do decide to supply a custom icon, keep in mind that “icon” is just a name given to a regular old image file; these are not true Windows icons, graphics that must be created using special software. Instead an icon is just a picture file, be it a .GIF, .JPG, or .PNG graphic. The sample gadgets that ship with Windows Vista all use .PNG graphics; that’s probably because .PNG graphics allow for transparent backgrounds, giving you the ability to make very cool-looking pictures. (Assuming you have the requisite artistic talent.) However, you don’t have to save your icons as .PNG files. Instead, load up Paint, create an icon, and save it as a .JPG file; your icon will show up just fine in the gadget picker.
Good question: what size should you make your icon? The optimal size is 64 pixels by 64 pixels. The gadget picker will resize your image to fit, but creating a 64x64 icon in the first place will help guard against any image distortion created if the gadget picker needs to shrink or stretch the image to fit the allotted space.
In case you’re wondering, here’s the icon we used for this article:
We liked it, too.
Note. You might have noticed that, the in the manifest file, the <ICON> tag is embedded within an <ICONS> tag:
<icons>
<icon>icon.png</icon>
</icons>
Does that mean you can add additional icons to the manifest file? Probably, although, to be honest, we aren’t sure what you would use those additional icons for. That’s something we’ll look into. |
The HTML file that makes up the gadget itself is actually no different than any Web page that uses dynamic HTML; in fact, to create the HTML file you simply use any valid HTML tagging (including CSS styles) plus script code. We’ll show you a sample HTML page in just a moment. Before we do that, however, we need to take a brief side trip and talk about how you incorporate WMI code into a gadget.
Working with WMI
As a system administrator you’re used to writing scripts that make heavy use of WMI. That’s understandable; after all, WMI is the technology that helps you manage everything from printers to disk drives to mice and monitors. Best of all, WMI is easy to use. For example, suppose you’d like to know the name of the operating system installed on the local computer. No problem; here’s a WMI script that will return that information for you:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
Msgbox objItem.Caption
Next
If you plan on creating gadgets for system administration we’ve got some bad news for you: the WMI scripts you’re used to writing won’t work in a gadget. (Wait, don’t do anything drastic: the news will get better in just a moment, promise.) That’s because, at heart, a gadget is nothing more than a Web page, and, for security reasons, Web pages aren’t able to make use of GetObject. If you place the preceding code in a gadget all you’ll end up with is this error message:
ActiveX component can’t create object: 'GetObject'
Uh-oh.
But don’t panic. You can still use WMI scripts within a gadget; you just can’t use GetObject and the winmgmts: moniker. Instead, you need to use CreateObject to create an instance of the WbemScripting.SWbemLocator object, then use the ConnectServer method to connect to the WMI service. In other words, you need to write a script that looks like this:
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
Msgbox objItem.Caption
Next
See? Like we said, no need to panic. (Although, in all fairness, it was probably our fault that you panicked in the first place.) The only difference between a script that uses the WMI moniker and a script that uses ConnectServer comes when you make a connection to the WMI service. You’re used to making that connection using a single line of code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Now you have to use two lines of code to make the connection (yes, doubling your workload!): you create an instance of the WbemScripting.SWbemLocator object, then you use the ConnectServer method to bind to the WMI service. Notice that we pass ConnectServer two parameters, the name of the computer to connect to (represented by the variable strComputer) and the WMI namespace we want to connect to (in this case, root\cimv2):
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2")
Got all that? Good. Now let’s return to our regularly-scheduled article.
Creating an HTML File
As we’ve already noted, a gadget is just an HTML file. That means that any elements (including dynamic elements) you can use in an HTML page can also be used in a gadget. As you’ve probably already figured out, this includes VBScript code; although most of the early gadgets made available through the Microsoft Gadgets Web site use JScript or JavaScript, VBScript works just fine (as you’ll soon see).
We’ve already shown you a WMI script that returns the name of the operating system installed on the local computer; with that in mind, let’s see if we can turn that script into a gadget. We’ll start off very simple, creating a gadget that consists of a single button that, when clicked, displays the value of the operating system Caption property in a message box. Here’s the HTML code for our gadget:
<html>
<head>
<title>My First Gadget</title>
<style>
body{width:120;height:160}
</style>
</head>
<script language="VBScript">
Sub RunSub
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
Msgbox objItem.Caption
Next
End Sub
</script>
<body>
<input type="button" value="Run" name="run_button" onClick="RunSub">
</body>
</html>
As you can see, there’s nothing “gadgety” about the code; this is rudimentary HTML that doesn’t do much more than display a single button on a page. When that button is clicked, a subroutine named RunSub is executed; that subroutine then uses WMI to determine the name of the operating system installed on the computer.
Note. OK, maybe this is rudimentary HTML for some people, but what if you have no background with HTML coding? If that’s the case, then you might want to take a look at our two-part HTA Tutorial. That will help bring you up to speed on things such as <SCRIPT> tags and <INPUT> tags. |
About the only thing we need to make special note of here is the <STYLE> tag. As we start creating more sophisticated gadgets we’ll discuss the <STYLE> tag in detail; for now, however, we’ll simply point out that we use this tag to configure the default height and width of our gadget:
<style>
body{width:120;height:160}
</style>
This tag simply says that we want our gadget to be 120 pixels wide (the Windows Sidebar is approximately 130 pixels wide) by 160 pixels tall. For this particular gadget 160 pixels might be too tall; if that’s the case, then we can simply assign a different value to the height property:
<style>
body{width:120;height:40}
</style>
When you install this gadget it will look like this in the Windows Sidebar:

And here’s what happens when you click the button:

Go ahead and write home to Mom, telling her all about the cool gadget you just created. Just let us know when you’re done and then we’ll move on to something else.
So far we’ve created a gadget that, when we click a button, displays the name of the operating system in a message box. There’s nothing wrong with that, and there will likely be times when your gadget returns so much information that you have no choice but to display that information in a message box, an Internet Explorer window, or some other location.
On the other hand, sooner or later you will also create gadgets where it makes no sense to display information in a message box, and where it makes no sense to have to click a button in order to return that information. For example, suppose you decided to create a clock for your Windows Sidebar. That’s great, but do you really want to be forced to click a button each time you want to find out what time it is? And when you do, do you want that time to pop up in a message box? Sure, on Omicron IV we like that sort of thing. But human beings find that a bit tedious, not to mention downright silly.
In other words, we need the ability to create gadgets that can:
| • | Display data within the bounds of the gadget itself. Later in this series we’ll show you ways to display this information graphically, but for now we’ll focus on text-based displays. |
| • | Retrieve data automatically, without requiring user interaction of any kind. |
| • | Periodically update that information. For example, a gadget that monitors available memory on a computer (a gadget we’ll create shortly) can’t just retrieve available memory at the time it loads and then call it good. Instead, it needs to periodically take a new, up-to-date memory sample. |
That sounds like a lot, but we’ll make it easy by learning these skills one-at-a-time. Let’s start by discussing the <SPAN> tag, a simple and easy way to display information within the body of the gadget itself.
Here’s a revised version of our HTML file. In this new gadget you still click a button in order to call the subroutine RunSub; however, this time around the operating system name is not echoed back in a message box but is, instead, written to the gadget itself:
<html>
<head>
<title>My First Gadget</title>
<style>
body{width:120;height:160}
</style>
</head>
<script language="VBScript">
Sub RunSub
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
DataArea.InnerHTML = objItem.Caption
Next
End Sub
</script>
<body>
<input type="button" value="Run" name="run_button" onClick="RunSub"><br>
<span id="DataArea"></span>
</body>
</html>
When we click the button on this gadget the returned information is displayed within the gadget itself:

Cool.
An Extra Bonus: Spiffing Up a Gadget’s Background
We’ll talk more about creating fancy-looking gadgets in future installments of this series. For now, we thought we’d mention a few simple little techniques you can use to spiff up your gadgets. For example, suppose you have a graphic that you think will make a good background for a gadget. In that case, all you have to do is set the background attribute of the <BODY> tag of your HTML file. This line of code causes your gadget to use a file named Background.jpg as the gadget background:
<body background = "background.jpg">
Just make sure that Background.jpg is stored in your gadget folder.
Alternatively, you can create a gadget with a different background color by adding the bgcolor parameter to the <BODY> tag. Dying to have a gadget with a bright red background? Hey, why not?
<body bgcolor = "red">
Finally, you can give your gadget a gradient background by using one of the Microsoft multimedia Web filters. In this gadget, we’ve modified the <BODY> style in two ways:
| • | We’ve set the font to be white, 8-point Arial. |
| • | We’ve added a gradient filter. |
Here’s what the code for our modified gadget looks like:
<html>
<head>
<title>My Gadget</title>
<style>
body{width:120;height:160;font:8 pt Arial;color:white;
filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, StartColorStr="#000000",
EndColorStr="#0000FF")}
</style>
</head>
<script language="VBScript">
Sub RunSub
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(".", "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
DataArea.InnerHTML = objItem.Caption
Next
End Sub
</script>
<body>
<input type="button" value="Run" name="run_button" onClick="RunSub"><P>
<span id="DataArea"></span>
</body>
</html>
And here’s what the gadget itself looks like:

It’s not a work of art, but it’s better than just having a plain white box plopped down in the Sidebar.
Note. For more information about using the gradient filter see the HTA Developer’s Center. |
Having a gadget display information in the body of the gadget (as opposed to in a message box) is a nice step forward, and more in tune with the spirit of gadgets. However, we still have to click a button in order to get that information. Surely there must be a way to have that information automatically gathered and displayed the moment the gadget is loaded.
You bet there is. And stop calling us Shirley.
Here’s a gadget that automatically retrieves and displays the name of the installed operating system, no button-clicking required:
<html>
<head>
<title>My First Gadget</title>
<style>
body{width:120;height:160}
</style>
</head>
<script language="VBScript">
Sub Window_OnLoad
RunSub
End Sub
Sub RunSub
strComputer = "."
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
DataArea.InnerHTML = objItem.Caption
Next
End Sub
</script>
<body>
<span id="DataArea"></span>
</body>
</html>
As you can see, this looks very similar to our previous gadget. In fact, there are only two differences:
| • | We removed the button from the gadget. That means there’s no longer any need to click something (for that matter, there’s no longer anything to click). |
| • | We added a Window_OnLoad subroutine. |
As many of you know, the Window_Onload subroutine – when included in a Web page – is designed to run any time that Web page is loaded or refreshed. This subroutine functions exactly the same way when included in a gadget: it automatically runs any time the gadget is loaded up. That’s how we create an auto-run gadget: we simply create a subroutine named Window_OnLoad and put the code we want run at load time into that subroutine.
For this particular gadget our Window_OnLoad subroutine looks like this:
Sub Window_OnLoad
RunSub
End Sub
As you can see, all we do in this particular subroutine is call a second subroutine: RunSub. We should point out that we don’t have to put our WMI code in a separate subroutine; we could have put our WMI script in the Window_OnLoad subroutine and directly executed that code upon startup. So why did we create a superfluous second subroutine? That’s easy: it makes for a nice transition to our next gadget.
In case you’re wondering, here’s what our “button-less” gadget looks like:

Undoubtedly gadgets will earn their keep in large part by serving as monitoring devices, dutifully keeping tabs on anything and everything from free disk space to network connectivity to available memory. Gadgets are perfect for tasks like this: they are small and unobtrusive, yet gadgets can still display visual cues that provide information at a glance. Equally important, gadgets can be programmed to take action when specific conditions arise. By combining small size and graphical capabilities with custom script code you can create incredibly useful tools for monitoring the health and well-being of your network.
Of course, the ability to do that hinges on the gadget’s ability to constantly update its information. It’s important that, at startup, a gadget can ping a server and verify that server’s availability. However, as a monitoring tool your gadget is of little use if it can only ping the server at startup. Instead, the gadget needs to periodically refresh itself: at regular intervals it needs to retrieve up-to-date information. In other words, it needs to do something like this:
<html>
<head>
<title>My Gadget</title>
<style>
body{width:120;height:40"}
</style>
</head>
<script language="VBScript">
Sub Window_Onload
GetMemory
iTimerID = window.SetInterval("GetMemory", 10000)
End Sub
Sub GetMemory
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(".", "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
DataArea.InnerHTML = objItem.FreePhysicalMemory
Next
End Sub
</script>
<body>
<span id="DataArea"></span>
</body>
</html>
Before we discuss how this script works we should note that this particular script doesn’t display the name of the operating system installed on the computer; instead, it displays the amount if available memory on the computer. Why did we suddenly drop the operating system gadget in favor of an available memory gadget? Well, we’re assuming that you don’t need to constantly monitor the name if the installed operating system; its safe to say that the name of the operating system doesn’t change anywhere near as often as available memory does. We figured that monitoring available memory might be a bit more realistic than monitoring the name of the installed operating system.
Incidentally, the subroutine for actually determining the current amount of available memory (a subroutine named GetMemory) looks like this:
Sub GetMemory
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(".", "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
DataArea.InnerHTML = objItem.FreePhysicalMemory
Next
End Sub
What we’re interested in here, however, isn’t the GetMemory subroutine but the Window_OnLoad subroutine:
Sub Window_Onload
GetMemory
iTimerID = window.SetInterval("GetMemory", 10000)
End Sub
As you can see, we do two things in this subroutine. First, we call the GetMemory subroutine; we do that to ensure that, upon loading our gadget, the available memory is immediately displayed in the gadget. The second thing we do is a bit more interesting:
iTimerID = window.SetInterval("GetMemory", 10000)
With this line of code we’re using the SetInterval method to create a timer. What this timer does is cause our gadget to call the GetMemory subroutine every 10 seconds (10,000 milliseconds). This is how we cause the gadget to auto-refresh itself: every 10 seconds the gadget calls the GetMemory subroutine, which means that every 10 seconds the gadget will use WMI to get the current amount of available memory and then display that data in the gadget body. If 10 seconds is too long between measurements, then change the parameter 10000 to a smaller number; for example, setting the parameter to 5000 will cause the gadget to update itself every 5 seconds (5000 milliseconds). Likewise, change the 10000 to a larger number if you’d prefer to have a longer interval between measurements.
Here’s our new gadget in action:

We never said it was a work of art. But now that we understand the basics behind creating gadgets (particularly gadgets of interest to system administrators) we’re about ready to start creating works of art. We’ll talk about incorporating graphics into gadgets soon; in addition, we’ll also explore the Gadget object model and some of the really cool things you can do with that.
In order to give you more of a taste for what gadgets can do, here’s a slightly (with the emphasis on the word slightly) more complicated gadget, one that does two things: it monitors available memory and also displays (in a Web page) more detailed system information when the Information button is clicked. Needless to say, this doesn’t even begin to scratch the surface of what gadgets can do, but it does give you a better sense for the range of capabilities that can be included in a single gadget.
You’re on your own to figure out the whys and wherefores of this particular gadget (we’ve already run out of space for this month) but here’s the code:
<html>
<head>
<title>My Gadget</title>
<style>
body{width:120;height:80}
</style>
</head>
<script language="VBScript">
Sub Window_Onload
GetMemory
iTimerID = window.SetInterval("GetMemory", 10000)
End Sub
Sub GetMemory
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(".", "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
DataArea.InnerHTML = objItem.FreePhysicalMemory
Next
End Sub
Sub RunSub
Set objLocator = CreateObject("WbemScripting.SwbemLocator")
Set objWMIService = objLocator.ConnectServer(".", "root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")
For Each objItem in colItems
strHTML = "Computer Name: " & objItem.Name & "<br>"
strHTML = strHTML & "User Name: " & objItem.UserName & "<br><br>"
Next
Set colItems = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
For Each objItem in colItems
strHTML = strHTML & "Operating System: " & objItem.Caption & "<br>"
strHTML = strHTML & "Service Pack: " & objItem.ServicePackMajorVersion & "<br><br>"
Next
Set colItems = objWMIService.ExecQuery("Select * From Win32_Processor")
For Each objItem in colItems
strHTML = strHTML & "Processor: " & objItem.Caption & "<br><br>"
Next
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colItems
strHTML = strHTML & objItem.Caption & "<br>"
For Each strAddress in objItem.IPAddress
strHTML = strHTML & "IP Address: " & strAddress & "<br>"
Next
Next
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.Toolbar = 0
objIE.StatusBar = 0
Set objDoc = objIE.Document.Body
objDoc.InnerHTML = strHTML
objIE.Visible = True
End Sub
</script>
<body>
<span id="DataArea"></span><p>
<input type="button" value="Information" name="run_button" onClick="RunSub">
</body>
</html>