{"id":64373,"date":"2007-07-26T01:01:00","date_gmt":"2007-07-26T01:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/26\/how-can-i-create-a-new-wmi-namespace-under-the-root-namespace\/"},"modified":"2007-07-26T01:01:00","modified_gmt":"2007-07-26T01:01:00","slug":"how-can-i-create-a-new-wmi-namespace-under-the-root-namespace","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-new-wmi-namespace-under-the-root-namespace\/","title":{"rendered":"How Can I Create a New WMI Namespace Under the Root Namespace?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I create a new WMI namespace under the root namespace?<BR><BR>&#8212; MT <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, MT. So how are you doing, MT? We hope you\u2019re doing well; the Scripting Guys hope that <I>everyone<\/I> is doing well. (OK: we hope that <I>most<\/I> everyone is doing well. There <I>are<\/I> a few exceptions to that general rule.) However, if it turns out that you <I>aren\u2019t<\/I> doing well (and we hope that you are) we thought we should mention that you can now get <A href=\"http:\/\/www.eternalimage.net\/mlb_page.html\" target=\"_blank\"><B>funerary urns and caskets<\/B><\/A> that reflect the colors and logo of your favorite major league baseball team. <\/P>\n<P>Admittedly, this isn\u2019t the sort of thing that the Scripting Guy who writes this column would typically talk about; he\u2019s really not all that keen on the whole death thing. And typically he <I>would<\/I> ignore a subject like this, except that while thumbing through a magazine the other day, he saw an ad for baseball funerary urns. \u201cSupplies are limited,\u201d said the ad. \u201cAct now.\u201d <\/P>\n<P><I>Act now<\/I>? To be honest, the Scripting Guy who writes this column wasn\u2019t 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\u2019t <I>have<\/I> 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\u2019s 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\u2019s not the Scripting Guys\u2019 definition of heaven.<\/P>\n<TABLE class=\"dataTable\" id=\"E1D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. To be fair, we haven\u2019t really come to a consensus on this matter. However, it\u2019s safe to say that the Scripting Guys\u2019 definition of heaven includes far more doughnuts and far less being holed up inside a funerary urn.<\/P>\n<P>OK, sure, that <I>would<\/I> be different if it was a Tampa Bay Devil Rays funerary urn. But the Tampa Bay funerary urn isn\u2019t available yet. If you want to spend an eternity of forlorn nothingness with the Tampa Bay Devil Rays you\u2019ll just have to attend one of their games instead.<\/P>\n<P>Which, come to think of it, isn\u2019t the Scripting Guys\u2019 definition of heaven, either.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, we know what you\u2019re thinking. You\u2019re thinking, \u201cI don\u2019t know, Scripting Guys; I mean, a <I>Philadelphia<\/I><I> Phillies funerary urn<\/I>?!? That\u2019s pretty tempting. What can you possibly offer us that could make life more attractive than death?\u201d That\u2019s a good question, and we\u2019re not sure we <I>can<\/I> top a Philadelphia Phillies funerary urn. (Interesting note: Microsoft Word\u2019s spellchecker doesn\u2019t recognize <I>Phillies<\/I> as being a valid word.) But what if we offered you a script that creates a new WMI namespace under the root namespace: <\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root&#8221;)<\/p>\n<p>Set objItem = objWMIService.Get(&#8220;__Namespace&#8221;)\nSet objNamespace = objItem.SpawnInstance_<\/p>\n<p>objNamespace.Name = &#8220;ScriptCenter&#8221;\nobjNamespace.Put_\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EZE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. If you run this script on Windows Vista with normal administrator privileges, you\u2019ll receive an error message:<\/P><PRE class=\"codeSample\">C:\\scripts\\hey0725.vbs(9, 1) SWbemObjectEx: Access denied\n<\/PRE>\n<P>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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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\u2019t 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\u2019s definitely an interesting one. If you want more practical \u2013 and more typical \u2013 uses for WMI, then take at the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_overview.mspx\" target=\"_blank\"><B>WMI Scripting Primer<\/B><\/A> in the <I>Microsoft Windows 2000 Scripting Guide<\/I>.<\/P>\n<P>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 <B>Get<\/B> method to bind us to the __Namespace class (note the two underscores preceding the word <I>Namespace<\/I>):<\/P><PRE class=\"codeSample\">Set objItem = objWMIService.Get(&#8220;__Namespace&#8221;)\n<\/PRE>\n<P>The __Namespace class contains instance data for all the namespaces located in \u2013 in this case, anyway \u2013 the root namespace. Because we want to create a brand-new namespace our next step is to call the <B>SpawnInstance_<\/B> method, which creates a new, \u201cblank\u201d namespace:<\/P><PRE class=\"codeSample\">Set objNamespace = objItem.SpawnInstance_\n<\/PRE>\n<P>And because our new namespace needs (at minimum) a name, we then assign the value <I>ScriptCenter<\/I> to the <B>Name<\/B> property:<\/P><PRE class=\"codeSample\">objNamespace.Name = &#8220;ScriptCenter&#8221;\n<\/PRE>\n<P>So are we done yet? Just about; all we need to do now is call the <B>Put_<\/B> method and write the new namespace (root\\ScriptCenter) to the WMI repository:<\/P><PRE class=\"codeSample\">objNamespace.Put_\n<\/PRE>\n<P><I>Now<\/I> we\u2019re done.<\/P>\n<P>Metaphorically-speaking, that is.<\/P>\n<P>Of course, we know what you\u2019re thinking now; now you\u2019re thinking, \u201cWhat if I change my mind and decide to <I>delete<\/I> that new namespace? Can I do that, or am I going to be stuck with this namespace just like I\u2019d be stuck with a Seattle Supersonics funerary urn?\u201d Relax. If you want to delete the namespace root\\ScriptCenter all you have to do is run <I>this<\/I> script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root&#8221;)<\/p>\n<p>Set objItem = objWMIService.Get(&#8220;__Namespace.Name=&#8217;ScriptCenter'&#8221;)\nobjItem.Delete_\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E4G\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. 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\u2019re asking for trouble if you use this same approach to delete one of WMI\u2019s built-in namespaces.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>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:<\/P><PRE class=\"codeSample\">Set objItem = objWMIService.Get(&#8220;__Namespace.Name=&#8217;ScriptCenter'&#8221;)\n<\/PRE>\n<P>As you can see, we\u2019re simply connecting to the instance of the __Namespace class that has a <B>Name<\/B> equal to <I>ScriptCenter<\/I>. Once we\u2019ve made the connection we can then call the <B>Delete_<\/B> method and remove the namespace from the WMI repository:<\/P><PRE class=\"codeSample\">objItem.Delete_\n<\/PRE>\n<P>Just that easy, just that quick.<\/P>\n<P>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 <I>do<\/I> need a funerary urn, however, keep in mind, at the moment, you can only get urns for the following teams:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>St. Louis Cardinals<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Atlanta Braves<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Boston Red Sox<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Chicago Cubs<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Detroit Tigers<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Los Angeles Dodgers<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>New York Yankees<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Philadelphia Phillies<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Although the final decision is up to you, the Scripting Guys recommend getting the Chicago Cubs urn. That\u2019s because there\u2019s such a nice symbolism there: you\u2019re 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.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a new WMI namespace under the root namespace?&#8212; MT Hey, MT. So how are you doing, MT? We hope you\u2019re 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,6],"class_list":["post-64373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a new WMI namespace under the root namespace?&#8212; MT Hey, MT. So how are you doing, MT? We hope you\u2019re 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64373"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64373\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}