• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 663
  • Last Modified:

Windows OS Version: How to remotely query for it?

Points of My Scenario:
1. I am Windows admin for a Server 2003 domain.
2. Windows OS versions include XP, 2003 (Standard & Enterprise), and 2008 R2.
QUESTION:
What command can I use from my workstation to query servers for the OS version and Editions?
0
waltforbes
Asked:
waltforbes
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
theraffCommented:
There's probably a better (read: Windows-specific) answer to this question, but I'd use nmap. This is available in most Linux distros, but you can get a Windows compatible version here.
0
 
Donald StewartNetwork AdministratorCommented:
0
 
oBdACommented:
Save this as GetOS.vbs or Whatever.vbs; you can run it using cscript or wscript, and either pass the computer to query in the command line ("GetOS.vbs /computer:SomeMachine"), or it will prompt you for a name:
Function Get_Host()
	If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
		Get_Host = "GUI"
	Else
		Get_Host = "CLI"
	End If
End Function

Function Get_Input(strHost, strPrompt)
	If (strHost = "GUI") Then
		Get_Input = InputBox(strPrompt)
	Else
		WScript.StdOut.Write strPrompt
		Get_Input = WScript.StdIn.ReadLine
	End If
End Function

Function Write_Output(strHost, strLine)
	If (strHost = "GUI") Then
		MsgBox(strLine)
	Else
		WScript.StdOut.Write strLine & vbCRLF
	End If
End Function

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

'MAIN
strHost = Get_Host
Set objArgsNamed = WScript.Arguments.Named
strComputer = objArgsNamed("Computer")
If (strComputer = "") Then
	strComputer = Get_Input(strHost, "Please enter the computer name: ")
End If
If (strComputer = "") Then
	strComputer = "."
End if
strOSVersion = Get_OS(strComputer)
Write_Output strHost, strComputer & ": " & strOSVersion

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
waltforbesAuthor Commented:
To oBdA: your script and switch is 99% perfect! I love it! The 1% outstanding: How can I direct output to a text file? I tried "GetOS.vbs /computer:SomeMachine > textfile.txt", but it created a zero-byte file and still sent the output to screen.

To dstewartjr: your excellent script provides more info than oBdA; however, how can I provide computer names from a file and redirect output to an output file? I want to do the following or similar - "yourscript.bat /sourcefile:servernames.txt > results-output.txt"
0
 
oBdACommented:
To send the output to a file, you'll need to use cscript as host; the message box output of the wscript version can't be redirected:
cscript.exe GetOS.vbs /computer:SomeMachine > textfile.txt
0
 
Donald StewartNetwork AdministratorCommented:
Sorry, not my script....probably would need to modify for sure.
0
 
oBdACommented:
Okay; this one does accept "/ComputerFile:<Serverlist>" and "/ResultFile:<Logfile>" as arguments as well. It will still work with both wscript and cscript, but the MsgBox of wscript doesn't scroll, so if there's a lot of machines in the file, you won't be able to see them all in the popup. For scripting, use cscript as described above.
Const intForReading = 1
Const intForWriting = 2 

Function Get_Host()
	If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
		Get_Host = "GUI"
	Else
		Get_Host = "CLI"
	End If
End Function

Function Get_Input(strHost, strPrompt)
	If (strHost = "GUI") Then
		Get_Input = InputBox(strPrompt)
	Else
		WScript.StdOut.Write strPrompt
		Get_Input = WScript.StdIn.ReadLine
	End If
End Function

Function Write_Output(strHost, strLine)
	If (strHost = "GUI") Then
		MsgBox(strLine)
	Else
		WScript.StdOut.Write strLine & vbCRLF
	End If
End Function

Function Get_OS(strComputer)
	On Error Resume Next
	Err.Clear
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colOS = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
	For Each objOS in colOS
		Get_OS = objOS.Caption & " " & objOS.Version
	Next
	If (Err.Number <> 0) Then
		Get_OS = "[Error]"
	End If
	On Error Goto 0
End Function

Function Get_Content(strFile)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(strFile, intForReading)
	strAllText = objFile.ReadAll
	objFile.Close
	Get_Content = Split(strAllText, vbCrLf)
End Function

Function Set_Content(arrContent, strFile)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	set objFile = objFSO.OpenTextFile(strFile, intForWriting, True)
	For i = 0 To (UBound(arrContent) - 1)
		objFile.WriteLine(arrContent(i))
	Next
End Function

'MAIN
strHost = Get_Host
Set objArgsNamed = WScript.Arguments.Named
strInFile = objArgsNamed("ComputerFile")
strOutFile = objArgsNamed("ResultFile")
strComputer = objArgsNamed("Computer")
If (strInFile <> "") Then
	arrComputers = Get_Content(strInFile)
	If (strHost = "GUI") Then
		Write_Output strHost, "Processing of " & UBound(arrComputers) & " machines will start after you click OK; be aware that this might take a while."
	End If
	If (strOutFile <> "") Then
		strOutput = "Result file: " & strOutfile & vbCRLF
	Else
		strOutput = ""
	End If
	For i = 0 To (UBound(arrComputers) - 1)
		strOSVersion =  Get_OS(arrComputers(i))
		If (strHost = "CLI") Then
			Write_Output strHost, arrComputers(i) & ": " & strOSVersion
		Else
			strOutput = strOutput & arrComputers(i) & ": " & strOSVersion & vbCRLF
		End If
		arrComputers(i) = arrComputers(i) & vbTab & strOSVersion
	Next
	If (strOutFile <> "") Then
		Set_Content arrComputers, strOutFile
	End If
	If (strHost = "GUI") Then
		Write_Output strHost, strOutput
	End If
Else
	If (strComputer = "") Then
		strComputer = Get_Input(strHost, "Please enter the computer name (empty for local host): ")
	End If
	If (strComputer = "") Then
		strComputer = "."
	End if
	strOSVersion = Get_OS(strComputer)
	Write_Output strHost, strComputer & ": " & strOSVersion
End If

Open in new window

0
 
waltforbesAuthor Commented:
To oBdA:
1. I created a "servers.txt" file containing the server names - one per line.
2. I created the script "GetOS-InputFile.vbs" containing the contents of your 2nd script.
3. At the command prompt I changed to the directory containing both vbs & txt files.
4. I typed: "cscript.exe GetOS-InputFile.vbs /ComputerFile:servers.txt /ResultFile:results.log"
...and
...IT WORKED!!
You saved the day! Thank you!
0
 
waltforbesAuthor Commented:
1. oBdA provided me the perfect solution.
2. dstewartjr provided me a good solution for dealing with a few machines at a time.
I am well satisfied.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now