How Can I Create a New WMI Namespace Under the Root Namespace?

How Can I Create a New WMI Namespace Under the Root Namespace?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a new WMI namespace under the root namespace?

-- MT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MT. So how are you doing, MT? We hope you’re doing well; the Scripting Guys hope that everyone is doing well. (OK: we hope that most everyone is doing well. There are a few exceptions to that general rule.) However, if it turns out that you aren’t doing well (and we hope that you are) we thought we should mention that you can now get funerary urns and caskets that reflect the colors and logo of your favorite major league baseball team.

Admittedly, this isn’t the sort of thing that the Scripting Guy who writes this column would typically talk about; he’s really not all that keen on the whole death thing. And typically he would ignore a subject like this, except that while thumbing through a magazine the other day, he saw an ad for baseball funerary urns. “Supplies are limited,” said the ad. “Act now.”

Act now? To be honest, the Scripting Guy who writes this column wasn’t sure how to interpret that. If you want to spend eternity in a Boston Red Sox funerary urn does that mean you should hurry up and die now, while you still have the chance? (After all, who wants to run the risk of having to spend eternity in an LA Dodgers funerary urn instead?) Granted, you probably don’t have to use your funerary urn right away; you could probably save it for later. But, in this day and age, can you really take that chance? For example, Seattle’s very own NBA team, the Supersonics, are on the verge of moving to Oklahoma City. Suppose you went out today and bought a funerary urn featuring the Sonics logo only to have the team move to Oklahoma City. Then what? Do you really want to spend eternity in the funerary urn of a defunct team? That’s not the Scripting Guys’ definition of heaven.

Note. To be fair, we haven’t really come to a consensus on this matter. However, it’s safe to say that the Scripting Guys’ definition of heaven includes far more doughnuts and far less being holed up inside a funerary urn.

OK, sure, that would be different if it was a Tampa Bay Devil Rays funerary urn. But the Tampa Bay funerary urn isn’t available yet. If you want to spend an eternity of forlorn nothingness with the Tampa Bay Devil Rays you’ll just have to attend one of their games instead.

Which, come to think of it, isn’t the Scripting Guys’ definition of heaven, either.

Now, we know what you’re thinking. You’re thinking, “I don’t know, Scripting Guys; I mean, a Philadelphia Phillies funerary urn?!? That’s pretty tempting. What can you possibly offer us that could make life more attractive than death?” That’s a good question, and we’re not sure we can top a Philadelphia Phillies funerary urn. (Interesting note: Microsoft Word’s spellchecker doesn’t recognize Phillies as being a valid word.) But what if we offered you a script that creates a new WMI namespace under the root namespace:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root")

Set objItem = objWMIService.Get("__Namespace")
Set objNamespace = objItem.SpawnInstance_

objNamespace.Name = "ScriptCenter"
objNamespace.Put_

Note. If you run this script on Windows Vista with normal administrator privileges, you’ll receive an error message:

C:\scripts\hey0725.vbs(9, 1) SWbemObjectEx: Access denied

If you actually want this script to work on Windows Vista, you need to run with elevated privileges. That means you should run the script from the command prompt: right-click Command Prompt and select Run As Administrator.

Admittedly, you might not ever need to create a new WMI namespace under the root namespace (or anywhere else for that matter). Nevertheless, we found this question interesting, simply because it demonstrates how cool WMI scripting really is; after all, you can even write scripts that modify WMI itself. (Although, as far as we know, you can’t yet outfit WMI in the colors and logo of your favorite major league baseball team.) Creating a new namespace might not be the most practical use for WMI scripting, but it’s definitely an interesting one. If you want more practical – and more typical – uses for WMI, then take at the WMI Scripting Primer in the Microsoft Windows 2000 Scripting Guide.

As for the script itself, we start out by binding to the WMI service on the local computer (although, as usual, you can run this script against a remote machine simply by assigning the name of that computer to the variable strComputer). After making the connection to WMI we then use the Get method to bind us to the __Namespace class (note the two underscores preceding the word Namespace):

Set objItem = objWMIService.Get("__Namespace")

The __Namespace class contains instance data for all the namespaces located in – in this case, anyway – the root namespace. Because we want to create a brand-new namespace our next step is to call the SpawnInstance_ method, which creates a new, “blank” namespace:

Set objNamespace = objItem.SpawnInstance_

And because our new namespace needs (at minimum) a name, we then assign the value ScriptCenter to the Name property:

objNamespace.Name = "ScriptCenter"

So are we done yet? Just about; all we need to do now is call the Put_ method and write the new namespace (root\ScriptCenter) to the WMI repository:

objNamespace.Put_

Now we’re done.

Metaphorically-speaking, that is.

Of course, we know what you’re thinking now; now you’re thinking, “What if I change my mind and decide to delete that new namespace? Can I do that, or am I going to be stuck with this namespace just like I’d be stuck with a Seattle Supersonics funerary urn?” Relax. If you want to delete the namespace root\ScriptCenter all you have to do is run this script:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root")

Set objItem = objWMIService.Get("__Namespace.Name='ScriptCenter'")
objItem.Delete_

Note. Needless to say, you should take care when running the preceding script. If you want to create and then delete a custom namespace named ScriptCenter, well, have at it. But you’re asking for trouble if you use this same approach to delete one of WMI’s built-in namespaces.

In this second script we once again bind to the WMI service; however, this time around we use the following line of code to bind directly to the root\ScriptCenter namespace:

Set objItem = objWMIService.Get("__Namespace.Name='ScriptCenter'")

As you can see, we’re simply connecting to the instance of the __Namespace class that has a Name equal to ScriptCenter. Once we’ve made the connection we can then call the Delete_ method and remove the namespace from the WMI repository:

objItem.Delete_

Just that easy, just that quick.

As we noted earlier, we hope our faithful readers (neither of them) are in need of a funerary urn at the moment. (No, not even an urn featuring the team colors and logo of your favorite baseball team.) If you do need a funerary urn, however, keep in mind, at the moment, you can only get urns for the following teams:

St. Louis Cardinals

Atlanta Braves

Boston Red Sox

Chicago Cubs

Detroit Tigers

Los Angeles Dodgers

New York Yankees

Philadelphia Phillies

Although the final decision is up to you, the Scripting Guys recommend getting the Chicago Cubs urn. That’s because there’s such a nice symbolism there: you’re going to spend eternity in your Chicago urn, and the Cubs are likely to spend the same amount of time pursuing their next World Series championship.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • 1. How do I do this in PowerShell?

    2. Once the new Namespace has been created in PowerShell, then how do I create a new class under that namespace?