'*****************************************************
'		Script Witten by Larry Heintz
'		Jan 2005 www.windowsadminscripts.com
' This script will query all computer in your AD or
' Work Group that you have Admin access to. You can
' then choose a AD or Work Group from the pull down
' list. The script will then query info regarding the
' OS that is installed on the computers.
' This script is also done in a comma delimited format
' for ease of exporting data.
'*****************************************************
On Error Resume Next
' Dims stuff
Dim containername,container,computer
Dim objStdOut,args
' Set Stuff
Set objStdOut = Wscript.stdOut
Set args = Wscript.Arguments.Named
containername = args.Item("adwg")
' Connects to AD Domain or Work Group Name and gets all computers
set container = getobject("WinNT://" & containername)
' Puts all computers in array
container.filter = array("computer")

' Writes out info to screen
wscript.echo "Computer Name, OS Name, OS Serial Number:"
wscript.echo "========================================="

if wscript.arguments.count = 1 then
	for each computer in container
		For each ObjOS in GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computer.name & "").ExecQuery("Select * from Win32_OperatingSystem")
			if not (errorChecking (computer.name)) then
				wscript.echo computer.name & "," & ObjOS.caption & "," & ObjOS.serialnumber
			end if
		next
		set ObjOS = nothing
	next
	Set container = nothing
else
	objStdOut.Write "Usage: cscript osinfo.vbs /adwg:[AD Domain or Work Group Name]"
	objStdOut.close
	wscript.quit
end if

' Error Checking Function
Function errorChecking(ComputerName) 
errorChecking = False 
if err.number <> 0 then 
	if err.number = "462" then
		wscript.echo ComputerName & ",Error,Unable to connect"
      		err.Clear () 
      		errorChecking = True
	elseif err.number = "70" then   'General access denied error
		wscript.echo ComputerName & ",Error,Permission Denied while trying to connect."
      		err.Clear () 
     		errorChecking = True
	elseif err.number = "451" then	'HEX Error is: 1C3
      		err.Clear () 
     		errorChecking = True
   	elseif err.number = "-2147023174" then   'The RPC server is unavailable.
		wscript.echo ComputerName & ",Error,Unable to connect error number -2147023174."
      		err.Clear () 
     		errorChecking = True
	elseif err.number = "-2147023836" then
		wscript.echo ComputerName & ",Error,Unable to connect error number --2147023836."
      		err.Clear () 
     		errorChecking = True
	elseif err.number = "-2147022986" then
		wscript.echo ComputerName & ",Error,Unable to connect error number -2147022986."
      		err.Clear () 
     		errorChecking = True
	else
		wscript.echo "Error is: " & err.number
		wscript.echo "HEX Error is: " & hex(err.number)
		wscript.echo "Desc. is: " & err.Description
		wscript.echo "Source is: " & err.Source
		err.Clear ()
	end if 
end if 
end Function 
