{"id":51843,"date":"2009-12-08T00:01:00","date_gmt":"2009-12-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/12\/08\/hey-scripting-guy-how-can-i-ensure-my-scripts-wont-fail-on-various-windows-operating-systems\/"},"modified":"2009-12-08T00:01:00","modified_gmt":"2009-12-08T00:01:00","slug":"hey-scripting-guy-how-can-i-ensure-my-scripts-wont-fail-on-various-windows-operating-systems","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-ensure-my-scripts-wont-fail-on-various-windows-operating-systems\/","title":{"rendered":"Hey, Scripting Guy! How Can I Ensure My Scripts Won&#8217;t Fail on Various Windows Operating Systems?"},"content":{"rendered":"<p class=\"MsoNormal\"><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>     &nbsp;<br><font size=\"2\"><img decoding=\"async\" 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\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I have a mixture of operating systems at work. We have Windows XP on the desktop, a few Windows Vista machines, Windows 7 on a few computers so far, Windows 2003 in the server room, and Windows 2008 and a couple of Windows 2008 R2 computers that are running Hyper-V on Windows Core edition. I know we are not unique in this regard, but given all this diversity, do you have any tricks that can be done to ensure that my scripts will not bomb out? <\/p>\n<p class=\"MsoNormal\">&#8212; JH<\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" 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\">Hello JH, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. The other night I watched the movie, <a href=\"http:\/\/en.wikipedia.org\/wiki\/A_Christmas_Story\"><font face=\"Segoe\">A Christmas Story<\/font><\/a>, which is about a little boy who wants a BB gun for Christmas. One of the funniest scenes in the movie happens when the boy gets his tongue stuck to the flagpole in the schoolyard. I never got my tongue stuck to a flagpole when I was a little scripting guy (I grew up in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Florida\"><font face=\"Segoe\">Florida<\/font><\/a> in the United States and it was never an option). Luckily, I had seen the movie when I moved to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ohio\"><font face=\"Segoe\">Ohio<\/font><\/a>, and my co-workers tried to get me to stick my tongue on the flagpole at work. The trick to keeping your tongue attached to your mouth lies in knowing about the properties of frozen flagpoles. <\/p>\n<p class=\"Readeraidonly\">Note: Portions of today&#8217;s Hey, Scripting Guy! post are excerpted from the Microsoft Press book, <i>Windows PowerShell 2.0 Best Practices<\/i> by Ed Wilson, which is now <a href=\"http:\/\/bit.ly\/edwilsonbestpractices\">available for pre-order<\/a>. <\/p>\n<p class=\"MsoNormal\">JH, the trick to ensure that your scripts do not bomb out is to know about the various operating systems. The easiest way to see if a script will work on a particular version of the operating system is to run it and see what happens. I have a Windows Server 2008 R2 Core Edition at home configured to run Hyper-V and a variety of different operating systems. When I determine a script will not work on a particular version of the operating system, I add a check for the operating system and exit the script gracefully. <\/p>\n<p class=\"MsoNormal\">Given the differences between the various versions of the Windows operating system, it is a best practice to check the version of the operating system before executing the script if you know that there could be version compatibility issues. There are, of course, several different methods of doing this. You can use the .NET Framework class <b>System.Environment<\/b> to check the operating system version. This is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; ([system.environment]::osversion).version.tostring()<br>6.1.7600.0<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">It is true that you can use Windows PowerShell remoting to get information from this class remotely, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Invoke-Command -ComputerName hyperv -Script { ([system.environment]::osversion).version.tostring() }<br>6.1.7600.0<br>PS C:&gt;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">But you can also achieve similar results using the WMI class <b>Win32_OperatingSystem<\/b>. The advantage of this approach is that WMI automatically remotes, and the syntax is a bit easier to use. <\/p>\n<p class=\"MsoNormal\">The GetVersion.ps1 script accepts a single command-line parameter, <b>$computer<\/b>, which is set by default to the localhost computer. The entry point to the script passes the value of the <b>$computer<\/b> variable and a reference type variable <b>$osv<\/b> to the <b>Get-OSVersion<\/b> function. Inside the <b>Get-OSVersion<\/b> function, the first thing that is done is to use the <b>Get-WMIObject<\/b> cmdlet to query the <b>Win32_OperatingSystem<\/b> WMI class from the computer that is targeted by the <b>$computer<\/b> variable. The resulting management object is stored in the <b>$os<\/b> variable. <\/p>\n<p class=\"MsoNormal\">The <b>Switch<\/b> statement is used to evaluate the <b>Version<\/b> property of the <b>Win32_OperatingSystem<\/b> class. If the value of the <b>Version<\/b> property is equal to 5.1.2600, the <b>value<\/b> property of the <b>$osv<\/b> reference type variable is set equal to <b>xp<\/b>. This type of logic is repeated for the value 5.1.3790, which was the build number for Windows Server 2003. <\/p>\n<p class=\"MsoNormal\">A problem arises if the version number is 6.0.6001 because both Windows Server 2008 <span>&nbsp;<\/span>and Windows Vista have the same build number. This is the same problem if the version number is 6.1.7600 because both Windows Server 2008 R2 and Windows 7 have the same build number. This is why the script stores the entire <b>Win32_OperatingSystem<\/b> management object in the <b>$os<\/b> variable instead of retrieving only the version attribute. The <b>ProductType<\/b> property can be used to distinguish between a workstation and a server. The possible values for the <b>ProductType<\/b> property are seen in Table 1.<\/p>\n<p class=\"MsoNormal\"><b>Table 1&emsp; Win32_OperatingSystem ProductType Values and Associated Meanings<\/p>\n<p><\/b><\/p>\n<table class=\"MsoNormalTable\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"120\">\n<p class=\"MsoNormal\"><b>Value <\/p>\n<p><\/b><\/p>\n<\/td>\n<td valign=\"top\" width=\"252\">\n<p class=\"MsoNormal\"><b>Meaning <\/p>\n<p><\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">\n<p class=\"MsoNormal\">1 <\/p>\n<\/td>\n<td valign=\"top\" width=\"252\">\n<p class=\"MsoNormal\">Work Station <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">\n<p class=\"MsoNormal\">2 <\/p>\n<\/td>\n<td valign=\"top\" width=\"252\">\n<p class=\"MsoNormal\">Domain Controller <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"120\">\n<p class=\"MsoNormal\">3 <\/p>\n<\/td>\n<td valign=\"top\" width=\"252\">\n<p class=\"MsoNormal\">Server<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">After the version of the operating system has been detected, a single word or number representing the operating system is assigned to the <b>value<\/b> property of the reference variable. In the GetVersion.ps1 script, this value is displayed on the console. The complete GetVersion.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>GetVersion.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Param($computer = &#8220;localhost&#8221;)<\/p>\n<p>Function Get-OSVersion($computer,[ref]$osv)<br>{<br><span>&nbsp;<\/span>$os = Get-WmiObject -class Win32_OperatingSystem `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-computerName $computer<br><span>&nbsp;<\/span>Switch ($os.Version)<br><span>&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;5.1.2600&#8221; { $osv.value = &#8220;xp&#8221; }<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;5.1.3790&#8221; { $osv.value = &#8220;2003&#8221; }<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;6.0.6001&#8221; <br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>If($os.ProductType -eq 1)<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;Vista&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end if<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;2008&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end 6001<br><span>&nbsp; <\/span><span>&nbsp;&nbsp;<\/span>&#8220;6.1.7600&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>If($os.ProductType -eq 1)<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;Win7&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end if<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;2008R2&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>} #end else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end 7600<br><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>DEFAULT { &#8220;Version not listed&#8221; }<br><span>&nbsp; <\/span>} #end switch<br>} #end Get-OSVersion<\/p>\n<p># *** entry point to script ***<br>$osv = $null<br>Get-OSVersion -computer $computer -osv ([ref]$osv)<br>$osv<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">The GetVersion.ps1 script returns a single word to indicate the version of the operating system. You can use this from the command line to quickly check the operating system version, as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:bp&gt; .GetVersion.ps1<br>Vista<br>PS C:bp&gt; .GetVersion.ps1 -c berlin<br>2008<br>PS C:bp&gt; .GetVersion.ps1 -c lisbon<br>xp<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">The GetVersion.ps1 script was written as a function to permit easy inclusion in other scripts. This allows you to perform the operating system version check, and then decide if you wish to continue processing the script. An example of this approach is seen in the GetVersionGetVolume.ps1 script that uses the <b>Win32_Volume<\/b> WMI class we examined in <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/12\/07\/hey-scripting-guy-does-the-win32-volume-wmi-class-support-filtering-on-the-name-property.aspx\">yesterday&rsquo;s Hey, Scripting Guy! post<\/a>.<\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>GetVersionGetVolume.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Param($drive = &#8220;C:&#8221;, $computer = &#8220;localhost&#8221;)<\/p>\n<p>Function Get-OSVersion($computer,[ref]$osv)<br>{<br><span>&nbsp; <\/span>$os = Get-WmiObject -class Win32_OperatingSystem `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>-computerName $computer<br><span>&nbsp;<\/span>Switch ($os.Version)<br><span>&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;5.1.2600&#8221; { $osv.value = &#8220;xp&#8221; }<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;5.1.3790&#8221; { $osv.value = &#8220;2003&#8221; }<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;6.0.6001&#8221; <br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>If($os.ProductType -eq 1)<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;Vista&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end if<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;2008&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end 6001<br><span>&nbsp;&nbsp;&nbsp; <\/span>&#8220;6.1.7600&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>If($os.ProductType -eq 1)<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;Win7&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end if<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$osv.value = &#8220;2008R2&#8221;<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end else<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>} #end 7600<br><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>DEFAULT { &#8220;Version not listed&#8221; }<br><span>&nbsp; <\/span>} #end switch<br>} #end Get-OSVersion<\/p>\n<p>Function Get-Volume($drive, $computer)<br>{<br><span>&nbsp;<\/span>$drive += &#8220;\\&#8221;<br><span>&nbsp;<\/span>Get-WmiObject -Class Win32_Volume -computerName $computer `<br><span>&nbsp;<\/span>-filter &#8220;Name = &#8216;$drive'&#8221;<br>} #end Get-Volume<\/p>\n<p># *** entry point to script ***<br>$osv = $null<br>Get-OSVersion -computer $computer -osv ([ref]$osv)<br>if($osv -eq &#8220;xp&#8221;) { &#8220;Script does not run on XP&#8221; ; exit }<br>Get-Volume -Drive $drive -Computer $computer<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">When the GetVersionGetVolume.ps1 script is run against my remote Windows XP computer (named WinXP), the script displays a message that states the script will not run on a Windows XP computer. This is seen here:<\/p>\n<p class=\"Fig-Graphic\"><span><img decoding=\"async\" title=\"Image showing the script will not run on a Windows XP computer\" alt=\"Image showing the script will not run on a Windows XP computer\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1208\/hsg-12-8-9-1.jpg\" width=\"590\" height=\"536\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/december\/hey1208\/hsg-12-8-9-1.jpg\"><font face=\"Segoe\"><\/font><\/a><\/span><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">JH, that is all there is to using WMI to check the operating system version. WMI Week will continue tomorrow. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<br><\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I have a mixture of operating systems at work. We have Windows XP on the desktop, a few Windows Vista machines, Windows 7 on a few computers so far, Windows 2003 in the server room, and Windows 2008 and a couple of Windows 2008 R2 computers that are running Hyper-V on [&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":[31,3,4,288,45,6],"class_list":["post-51843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-scripting-techniques","tag-version-information","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I have a mixture of operating systems at work. We have Windows XP on the desktop, a few Windows Vista machines, Windows 7 on a few computers so far, Windows 2003 in the server room, and Windows 2008 and a couple of Windows 2008 R2 computers that are running Hyper-V on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51843","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=51843"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51843\/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=51843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}