{"id":69293,"date":"2005-07-28T11:13:00","date_gmt":"2005-07-28T11:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/28\/how-can-i-determine-if-a-wmi-property-is-writeable\/"},"modified":"2005-07-28T11:13:00","modified_gmt":"2005-07-28T11:13:00","slug":"how-can-i-determine-if-a-wmi-property-is-writeable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-a-wmi-property-is-writeable\/","title":{"rendered":"How Can I Determine if a WMI Property is Writeable?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! We have an application that installs a WMI class named UIDLightInfo. That class includes a property named UIDLightState. How can I tell whether or not I can change the value of this property?<BR><BR>&#8212; JC<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JC. You know, we once reviewed a scripting course where the authors continually dealt with the question, \u201cHow do I know if I can do <I>x<\/I>?\u201d Their answer was always the same: \u201cTry it and see what happens. If your script blows up that means you probably can\u2019t do it.\u201d We were always glad that these guys were teaching a scripting class rather than serving as driving instructors:<\/P>\n<P>\u201cDo you think we\u2019d survive if I drove off this cliff?\u201d<\/P>\n<P>\u201cTry it and see what happens.\u201d<\/P>\n<P>Admittedly, there might be times when the only way to find out an answer is to close your eyes, run the script, and hope for the best. This isn\u2019t one of those times, however. WMI is a very powerful technology for retrieving information about processes, services, and other items involving your computer; it\u2019s an equally powerful technology for retrieving information about WMI itself. If you want to know something about WMI namespaces, classes, methods, properties, or what-have-you, the best advice is to just ask WMI.And that\u2019s exactly what this particular script does. This is a generic script that can check a specified property, in a specified class, in a specified namespace, on a specified computer, and then return the following information:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Whether the property stores values as an array.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The data type of the property.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Whether the property is read-write or read-only.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nstrNamespace = &#8220;\\root\\cimv2&#8221;\nstrClass = &#8220;Win32_Printer&#8221;\nstrProperty = &#8220;PortName&#8221;<\/p>\n<p>blnWriteable = False<\/p>\n<p>Set objClass = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; strNameSpace &amp; &#8220;:&#8221; &amp; strClass)<\/p>\n<p>For Each objClassProperty In objClass.Properties_\n    If objClassProperty.Name = strProperty Then\n        If objClassProperty.IsArray Then\n            Wscript.Echo &#8220;This property is an array.&#8221;\n        End If<\/p>\n<p>        Select Case objClassProperty.CIMType\n            Case 8\n                Wscript.Echo &#8220;This is a string property.&#8221;\n            Case 11\n                Wscript.Echo &#8220;This is a Boolean property.&#8221;\n            Case 13\n                Wscript.Echo &#8220;This is an Object property.&#8221;\n            Case 101\n                Wscript.Echo &#8220;This is a date-time property.&#8221;\n            Case 102\n            Wscript.Echo &#8220;This is a Reference property.&#8221;\n            Case 103\n                Wscript.Echo &#8220;This is a string-type property.&#8221;\n            Case Else\n                Wscript.Echo &#8220;This is a numeric property.&#8221;\n        End Select<\/p>\n<p>        For Each objQualifier in ObjClassProperty.Qualifiers_\n            If objQualifier.Name = &#8220;write&#8221; Then\n                blnWriteable = True\n            End If\n        Next\n    End If\nNext<\/p>\n<p>If blnWriteable = True Then\n    Wscript.Echo &#8220;This property is read-write.&#8221;\nElse\n    Wscript.Echo &#8220;This property is read-only.&#8221;\nEnd If\n<\/PRE>\n<P>We begin by defining some variables that will be used to connect to the desired computer, namespace, class, and property. Our sample script connects to the root\\cimv2 namespace on the local computer and queries the <B>PortName<\/B> property in the <B>Win32_Printer<\/B> class; if you want to query a different property\/class\/whatever, just change the values accordingly. For example, assuming your UIDLightInfo class is found in the root\\cimv2 namespace then the first four lines of your script might look like this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nstrNamespace = &#8220;\\root\\cimv2&#8221;\nstrClass = &#8220;UIDLightInfo&#8221;\nstrProperty = &#8220;UIDLightState&#8221;\n<\/PRE>\n<P>In addition, we assign the value False to a fifth variable, blnWriteable. We\u2019re going to assume, by default, that all properties are read-only; that\u2019s what the False is for. Later in the script we\u2019ll test to see if the property is writeable; if it is, then we\u2019ll change the value of blnWriteable to True. Otherwise we\u2019ll just leave it be.<\/P>\n<P>We then use this line of code to connect to the WMI service and bind to the desired class:<\/P><PRE class=\"codeSample\">Set objClass = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; strNameSpace &amp; &#8220;:&#8221; &amp; strClass)\n<\/PRE>\n<P>When we bind to a class (as opposed to returning instances of that class) we can retrieve information about the class itself. For example, each WMI class has a property named <B>Properties_<\/B>; as you might expect, this contains a list of all the properties for that class. We use this line of code to set up a For Each loop to walk through the collection of properties:<\/P><PRE class=\"codeSample\">For Each objClassProperty In objClass.Properties_\n<\/PRE>\n<P>Inside that loop we check each property to see if the property name matches the name we assigned to the variable strProperty. If it does, we retrieve some detailed information about that particular property; that\u2019s because properties also have properties. For example, we can look at the <B>IsArray<\/B> property to determine whether or not the property stores values as an array:<\/P><PRE class=\"codeSample\">If objClassProperty.IsArray Then\n    Wscript.Echo &#8220;This property is an array.&#8221;\nEnd If\n<\/PRE>\n<P>In addition, we can use the <B>CIMType<\/B> property to determine the data type for the property. Because CIMType returns information as an integer value, we set up a Select Case block to convert the value to something a bit more meaningful. For example, if CIMType is an 8, our script echoes back the fact that the property has a string data type:<\/P><PRE class=\"codeSample\">Case 8\n    Wscript.Echo &#8220;This is a string property.&#8221;\n<\/PRE>\n<P>Last, but surely not least, we take a look at the \u201cqualifiers\u201d for this property. Qualifiers are additional information that might or might not be present; if they <I>are<\/I> present they help define the property. For example, if the property has the <B>read<\/B> qualifier then that property is readable; if the property has the <B>write<\/B> qualifier, then the property is writeable. We use the following code to loop through all the qualifiers for the property, looking for one with the name <B>write<\/B>. If we find one, that means that the property is writeable, and we change the value of blnWriteable to True:<\/P><PRE class=\"codeSample\">For Each objQualifier in ObjClassProperty.Qualifiers_\n    If objQualifier.Name = &#8220;write&#8221; Then\n        blnWriteable = True\n    End If\nNext\n<\/PRE>\n<P>We echo whether the property is read-only or read-write, and then call it quits. When we run this script on a Windows XP computer we get back output like this:<\/P><PRE class=\"codeSample\">This is a string property.\nThis property is read-write.\n<\/PRE>\n<P>Because the property is read-write you now know that you can change it using a script.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have an application that installs a WMI class named UIDLightInfo. That class includes a property named UIDLightState. How can I tell whether or not I can change the value of this property?&#8212; JC Hey, JC. You know, we once reviewed a scripting course where the authors continually dealt with the question, [&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-69293","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! We have an application that installs a WMI class named UIDLightInfo. That class includes a property named UIDLightState. How can I tell whether or not I can change the value of this property?&#8212; JC Hey, JC. You know, we once reviewed a scripting course where the authors continually dealt with the question, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69293","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=69293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69293\/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=69293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}