Script to determine Microsoft OS Type

AManoux
AManoux used Ask the Experts™
on
I need a script to just determine if a Windows machine is a workstation or a server OS.  
I do NOT need to know the following....
1) If the OS is something other than Windows
2) The exact build # or service pack level of the OS
3) The exact OS type
All I need the script to do is check to see if the OS is a Windows Server OS or not.
I can't use "ver | find" because some build numbers are identical for workstation and server OS
I can't use the read the following registry key...
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName
...because it displays the same info for Win2000 workstation and Win2000 server
I am looking for a simple solution.  Thanks



Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Below is a simple code i found on the web a while ago.  It does a simple query to find the first 3 chars of the version.  From there it does a case statement to compare the findings and set a variable to the correct name.

another way is to just open command line and it is the very first line in the window.
'/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
' Function: FindOSType
' Parameter: Computer Name
' Purpose: This function return the OS name of the provided computer for all
' Windows OS machines
'
' Written by: Anand Venkatachalapathy (http://anandpv.spaces.live.com)
'/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
Function FindOSType(ComputerName)
    'Defining Variables
    Dim objWMI, objItem, colItems
    Dim OSVersion, OSName, ProductType
 
    'Get the WMI object and query results
    Set objWMI = GetObject("winmgmts:\\" & ComputerName & "\root\cimv2")
    Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
 
    'Get the OS version number (first two) and OS product type (server or desktop)
    For Each objItem in colItems
        OSVersion = Left(objItem.Version,3)
        ProductType = objItem.ProductType
    Next
 
    'Time to convert numbers into names
    Select Case OSVersion
        Case "6.0" 
            OSName = "Windows Vista"
        Case "5.2" 
            OSName = "Windows 2003"
        Case "5.1" 
            OSName = "Windows XP"
        Case "5.0" 
            OSName = "Windows 2000"
        Case "4.0" 
            OSName = "Oh! Really! NT 4.0"
        Case Else
            OSName = "Hi! Grandpa! Windows ME or older"
    End Select
 
    'Return the OS name
    FindOSType = OSName
   
    'Clear the memory
    Set colItems = Nothing
    Set objWMI = Nothing
End Function 
 
 
CODE FROM: http://anandpv.spaces.live.com/blog/cns!AFCCA5892B178862!890.entry

Open in new window

This should be pretty simple using WMI. See attached vbs code below for a single machine. the only drawback i see to this is it also give you some partition information. This must be info stored in the win32_operatingsystem name data. See screenshot
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_OperatingSystem",,48) 
For Each objItem in colItems 
    Wscript.Echo objItem.Name
Next

Open in new window

1.jpg
And for a group of remote machines you could use the following code. Replace computer1,2,3,4 with actual system names.


arrComputers = Array("computer1","computer2","computer3","computer4")
For Each strComputer In arrComputers
   WScript.Echo
   WScript.Echo "=========================================="
   WScript.Echo "Computer: " & strComputer
   WScript.Echo "=========================================="
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_OperatingSystem",,48) 
For Each objItem in colItems 
    Wscript.Echo objItem.Name
Next
Next

Open in new window

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Commented:
The below snippet will display the just the operating system name, simply and cleanly - similar to xxdcmast's script, however this one does not display the System Disk Information


strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" 
 
& strComputer & "\root\cimv2")
 
Set colOS = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
 
For Each objOS in colOS
	Wscript.Echo objOS.Caption
Next

Open in new window

Author

Commented:
Hi Amerilab,
When I run your script I receive the following error <file attached>.

Author

Commented:
Hmmmm, didn't seem to attach the screenshot error
error.jpg

Author

Commented:
Hi Psy053,
I would be interested in seeing your script work.  However, when I run it I receive the following error.
error-2.jpg

Commented:
Just to make it a little closer to what you have requested.

(Also to fix an issue with how the code snipper displayed the last code)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2")
 
Set colOS = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
 
For Each objOS in colOS
	If objOS.Caption <> "*Server*" Then
		Wscript.echo "Not Server"
	Else
		WScript.Echo "Server"
	End If
Next

Open in new window

Commented:
Sorry, the error is caused by the Snippet splitting a line incorrectly. I fixed the issue in my last comment

Author

Commented:
Thanks for the fix Psy053.  Your script works correctly now, but it doesn't provide the desired output.  For example, when I ran it on a Windows 2000 Advanced Server, it echo'd "Not Server".
Commented:
Apologies, I wasn't paying attention when  I wrote it up and didn't test it. Here is the re-write.

I don't have a 2K Server infront of me to test fully, so I have set the script to display the Operating System as well as test it - that way we can see what OS the script is returning for your 2000 Server.





strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2")
 
Set colOS = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
 
For Each objOS in colOS
	Wscript.echo objOS.Caption
	
	If InStr (objOS.Caption, "Server") Then
		Wscript.Echo "Server"
	Else
		Wscript.Echo "Not Server"
	End If
 
Next

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial