Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

WMI Object for BootupTime

$
0
0

Hello,

So the title is sort of misleading as I don't know what would be a proper way of putting this. Basically this is the flow of the script, and it works unless a computer has virtual adapters for VMware, at least from what I can tell. Here is the ugly flow:

Check an excel sheet for a list of computer names

set a variable strComputer = objExcel.Cells(intRow, 1).Value

Check if the computer is online, if not moves to the next computer else does the next lines

Connect to the WMI and get LastBootupTime

Do some conversions and math to get a value.

Problem 1:

Connecting to the WMI and doing "Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True", full code below, returns the virtual adapter to the VM Guest OS, which isn't accessible from the network, as it shouldn't be. The computer I am using to test has 2 guests. I am not sure if this is breaking me though. On computers without VMware I am not having this problem.

	Set objWMIService = GetObject("winmgmts:\\" &  strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
	For Each objItem in colItems
		If Err.Number <> 0 Then
			objExcel.Cells(intRow, 2).Value = ""
			objExcel.Cells(intRow, 3).Value = "Off Line"
			objExcel.Cells(intRow, 3).Font.Bold = TRUE
			objexcel.Cells(intRow, 3).Font.ColorIndex = 3
			Err.Clear
		Else
			objExcel.Cells(intRow, 2).Value = objItem.IPAddress
			objExcel.Cells(intRow, 3).Value = "On Line"
			objExcel.Cells(intRow, 3).Font.Bold = TRUE
			objexcel.Cells(intRow, 3).Font.ColorIndex = 1
			Online = 1
		End If
	Next

Problem 2 (The big problem):

		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
		Set colOperatingSystem = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
		For Each objItem In colOperatingSystem
			'some Code - just conversions which work
		Next

When I try and connect to these devices I get the same days from today to lastbootuptime, 97 or something like that. It doesnt seem to actually connect to the computer and give the computers information. If I run this against a computer without VMware Workstation installed it works, so I know the logic is correct.

Here is the whole script if someone wants to test it and tell me where I am wrong. I am sure there is some easier way of doing this, but I am not asking someone to correct the logic, just help me figure out why I am getting false information from some computers.

Thanks to those much smarter then I!

Set objExcel = CreateObject("Excel.Application")
Set Fso = CreateObject("Scripting.FileSystemObject")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\cgilbert\Desktop\powershelltest\List.xlsx")
Set InputFile = objWorkbook
objExcel.Visible = True

intRow = 2

objExcel.Cells(1, 1).Value = "Machine Name" 
objExcel.Cells(1, 2).Value = "IP Address"   
objExcel.Cells(1, 3).Value = "Status"
objExcel.Cells(1, 4).Value = "Last Boot Time"
objExcel.Cells(1, 5).Value = "Todays Date"
objExcel.Cells(1, 6).Value = "Days Online"

strCMDY = FormatDateTime(Date(),0)
strCYear = (Right(strCMDY, 4))

If (IsNumeric(Mid(strCMDY, 2, 1))) Then
'	wscript.echo "Month is in the double digits"
	strCMonth = (Left(strCMDY, 2))
Else
'	wscript.echo "Month is in the single digits"
	strCMonth = (Left(strCMDY, 1))
	SingleDigitMonth = 1
End If

If (IsNumeric(Mid(strCMDY, 4, 1))) Then
'	wscript.echo "Day is in the double digits"
	strCDay = (Mid(strCMDY, 3, 2))
Else
'	wscript.echo "Day is in the single digits"
	strCDay = (Mid(strCMDY, 3, 1))
	SingleDigitDay = 1
End If

If SingleDigitMonth = 1 Then
	strCMonth = "0" & strCMonth
End If

If SingleDigitDay = 1 Then
	strCDay = "0" & strCDay
End If

strCurrentOrganized = strCYear & "/" & strCMonth & "/" & strCDay

Do Until objExcel.Cells(intRow,1).Value = ""
	strComputer = objExcel.Cells(intRow, 1).Value
	Online = 0
	Invalid = 1
	wscript.echo "Checking " & strComputer
On Error Resume Next
	Set objWMIService = GetObject("winmgmts:\\" &  strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
	For Each objItem in colItems
		If Err.Number <> 0 Then
			objExcel.Cells(intRow, 2).Value = ""
			objExcel.Cells(intRow, 3).Value = "Off Line"
			objExcel.Cells(intRow, 3).Font.Bold = TRUE
			objexcel.Cells(intRow, 3).Font.ColorIndex = 3
			Err.Clear
		Else
			objExcel.Cells(intRow, 2).Value = objItem.IPAddress
			objExcel.Cells(intRow, 3).Value = "On Line"
			objExcel.Cells(intRow, 3).Font.Bold = TRUE
			objexcel.Cells(intRow, 3).Font.ColorIndex = 1
			Online = 1
		End If
	Next
	If Online = 1 Then
		Set objWMIService = GetObject("winmgmts:\\" &  strComputer & "\root\cimv2")
		Set colOperatingSystem = objWMIService.ExecQuery("Select * From Win32_OperatingSystem")
		For Each objItem In colOperatingSystem
			strBootUpTime = LEFT(objItem.LastBootUpTime,8)
			strBootYear = Left(strBootUpTime, 4)
			strBootMonth = Mid(strBootUpTime, 5, 2)
			strBootMonthValidate = Left(strBootMonth, 2)
			strBootDay = Right(strBootUpTime, 2)
			strBootDayConvert = strBootYear & "/" & strBootMonth & "/" & strBootDay
			objExcel.Cells(IntRow, 4).Value = strBootDayConvert
		Next
	objExcel.Cells(IntRow, 5).Value = strCurrentOrganized
	strOutputDays = (DateDiff("d", strBootDayConvert, strCurrentOrganized))
	objExcel.Cells(IntRow, 6).Value = strOutputDays	
	End If
intRow = intRow + 1
Loop

objExcel.Range("A1:F1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
Set objWorkbook = Nothing


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>