Any suggestions on how to clean up/optimize the following script?
V/r,
SGT Key
United States Army
Code:
'---INSTRUCTIONS:---------------------------------------------------------------------
'
' 1. Obtain list of computers from AD (IP address will work also)
' 2. Copy list of computers into a text file named "computers.txt"
'
' NOTE: Computers (or IP's) have to be on their own line within the text file.
' For example:
' XXXXXXXXXXXX002
' XXXXXXXXXXXX003
' XXXXXXXXXXXX004
' XXXXXXXXXXXX005
'
' 3. Save that text in the same folder as the "OStype.vbs" script
' 4. Run the "OStype.vbs"
' 5. Open "Results.csv" file to see the results
'
'--------------------------------------------------------------------------------
'---FUTURE UPDATES:--------------------------------------------------------------
'
' 1. Add column names for CPU Supports and Current OS
' (Add info to easier identifiablity)
' COMPLETED: 14 July 2015
' **2. Find user last logged on
' (identify main users of said system)
' **3. Find lastLogon in AD for computers not online
' (to see how stale)
' **4. If possible, find 182 computers in AD
' (eliminate use of computers.txt)
' 5. Rename "results.csv" output file to include date
' (eliminate multiple scans in same output file)
' COMPLETED: 14 July 2015
' **6. Find disk size of each computer
' (identify full C:\ drives)
' 7. Identify time since last restart and restart system if it has been over 2 days
' (mitigate non-IA compliance)
' COMPLETED: 14 July 2015
' 7a. Restart system if it has been over 24 hours
' (mitigate non-IA compliance) [2 hour delay when sending restart]
' COMPLETED: 14 July 2015
' **8. Identify current printers installed
' (clean up multiple printers installed)
' **9. Last re-image date
' (identify old images)
'
'--------------------------------------------------------------------------------
On Error Resume Next
Const ForAppending = 8
Dim objWMIService, colItems
Dim arrIP
Dim strOnline, strIP, strComputer, strManufact, strModel, strSerial, strMAC
Dim objFile, objFolder, objFSO, objFileToRead
Dim strFolder, strFile, strLine, strHeader
Dim SNSet
Dim strQuery, colPingResults
Dim strOStype, strProcessor
'---Open Files----------------------------------------------------------------
strFolder = "."
strFile = "\Results - " & FormatDateTime(Now(),vbLongDate) & ".csv"
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(".\computers.txt",1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile (strFolder & strFile, ForAppending, True)
strHeader = "Online:,Computer Name:,Manufacturer:,Model:,Serial:,MAC:,IP Address:,CPU Supports:,Current OS:,Last Restart:"
objFile.WriteLine(strHeader)
'---Read Computer List File----------------------------------------------------
do while not objFileToRead.AtEndOfStream
strComputer = objFileToRead.ReadLine()
'---Ping Test------------------------------------------------------------------
strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'"
Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
For Each objPingResult In colPingResults
If objPingResult.StatusCode = 0 Then
Wscript.Echo
Wscript.Echo strComputer & " is ONLINE!"
Wscript.Echo
strOnline = "Yes"
'---Computer Name, Manufacturer, and Model-------------------------------------
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "NAME: " & objItem.Name
strComputer = objItem.Name
Wscript.Echo "MANUFACTURER: " & objItem.Manufacturer
strManufact = objItem.Manufacturer
Wscript.Echo "MODEL: " & objItem.Model
strModel = objItem.Model
Next
'---Serial Number-------------------------------------
Set SNSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"& strComputer &"").InstancesOf ("Win32_BIOS")
for each SN in SNSet
'msgbox
Wscript.Echo "SERIAL NUMBER: " & SN.SerialNumber
strSerial = SN.SerialNumber
Next
'---MAC and IP-------------------------------------
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
ReDim arrIP(-1)
For Each objItem in colItems
Wscript.Echo "MAC: " & objItem.MACAddress
strMac = objItem.MACAddress
For Each addr In objItem.IPAddress
ReDim Preserve arrIP(UBound(arrIP)+1)
arrIP(UBound(arrIP)) = addr
Next
Next
strIP = Join(arrIP, ",")
Wscript.Echo "IP: " & strIP
'---Current CPU & Current OS-------------------------------------
Set ObjWMI = GetObject("WINMGMTS:" & "{ImpersonationLevel=Impersonate,AuthenticationLevel=Pkt}!\\" & StrComputer & "\Root\CIMV2")
Set ColSettings = ObjWMI.ExecQuery ("SELECT * FROM Win32_Processor")
For Each ObjProcessor In ColSettings
WScript.Echo
WScript.Echo "Processor: " & ObjProcessor.DataWidth & "-Bit"
strOSProcessor = "Processor: " & ObjProcessor.DataWidth & "-Bit"
WScript.Echo "Operating System: " & ObjProcessor.AddressWidth & "-Bit"
strOSType = "Operating System: " & ObjProcessor.AddressWidth & "-Bit"
Next
'---Identify time since last Roboot and reboot if over 24 Hours-------------------------------------
strWarning = "Due to network maintenance, this computer must be restarted. You have 2 hours to save your work from the start of this countdown. Sorry for any inconvenience caused."
strDelay = 7200
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
strUptime = dtmSystemUptime & " hours ago"
If dtmSystemUptime >= 24 then
wscript.echo
wscript.echo "Last Rebooted: " & dtmSystemUptime & " hours ago"
wscript.echo
WshShell.Run "C:\Windows\System32\shutdown.exe /m \\" & strComputer & " /r /f -t " & strDelay & " /c " & Chr(34) & strWarning & Chr(34)
Else
Wscript.Echo strComputer & " has been rebooted within the past 24 Hours."
End If
Set dtmSystemUptime = Nothing
Next
'---Prep all info into CSV format-----------------------
Wscript.Echo strComputer & "," & strManufact & "," & strModel & "," & strSerial & "," & strMAC & "," & strIP & "," & strOSProcessor & "," & strOSType
strLine = strOnline & "," & strComputer & "," & strManufact & "," & strModel & "," & strSerial & "," & strMAC & "," & strIP & "," & strOSProcessor & "," & strOSType & "," & strUptime
'---Write to file and go to next computer until done--------------------------------
objFile.WriteLine(strLine)
Else
Wscript.Echo "nope"
strOnline = "No"
strLine = strOnline & "," & strComputer
objFile.WriteLine(strLine)
End If
Next
Set strLine = Nothing
Set strOnline = Nothing
Set strComputer = Nothing
Set strManufact = Nothing
Set strModel = Nothing
Set strSerial = Nothing
Set strMAC = Nothing
Set strIP = Nothing
Set strOSProcessor = Nothing
Set strOSType = Nothing
Set strOSType = Nothing
Set strOSProcessor = Nothing
Set strUptime = Nothing
Set strWarning = Nothing
Set strDelay = Nothing
Set dtmSystemUptime = Nothing
Set dtmLastBootupTime = Nothing
Set dtmBootup = Nothing
loop
objFileToRead.Close
objFile.Close
'---Clear Memory---------------------------------------
Set SNSet = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
set objFile = nothing
Set arrIP = Nothing
Set strIP = Nothing
Set strComputer = Nothing
Set strManufact = Nothing
Set strModel = Nothing
Set strSerial = Nothing
Set strMAC = Nothing
Set objFileToRead = Nothing
'---Function for Restart Date Calc---------------------
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
End Function
V/r,
SGT Key
United States Army