matrixnz
asked on
Help with VBScript
Hi there
I'm trying to use the following script to give me a report on Server Uptime and I can get it to work using a single instance but can't get it to read the MachineList.Txt file
Can anyone help.
Cheers
strComputer = "MachineList.Txt"
'strComputer = InputBox("Enter Machine Name")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set objExcel = CreateObject("Excel.Applic ation")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "IP Address"
objExcel.Cells(1, 3).Value = "MAC Address"
objExcel.Cells(1, 4).Value = "Days"
objExcel.Cells(1, 5).Value = "Hours"
objExcel.Cells(1, 6).Value = "Minutes"
objExcel.Cells(1, 7).Value = "Report Time Stamp"
Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfig uration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
objExcel.Cells(intRow, 1).Value = objAdapter.DNSHostName
If Not IsNull(objAdapter.IPAddres s) Then
For i = 0 To UBound(objAdapter.IPAddres s)
objExcel.Cells(intRow, 2).Value = objAdapter.IPAddress(i)
Next
End If
objExcel.Cells(intRow, 3).Value = objAdapter.MACAddress
Next
Set colObjects = objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfOS_S ystem")
For Each objWmiObject In colObjects
intPerfTimeStamp = objWmiObject.Timestamp_Obj ect
intPerfTimeFreq = objWmiObject.Frequency_Obj ect
intCounter = objWmiObject.SystemUpTime
Next
iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFre q
sUptime = ConvertTime(iUptimeInSec)
Function ConvertTime(seconds)
ConvDays = seconds \ (3600 * 24)
ConvHour = (seconds Mod (3600 * 24)) \ 3600
ConvMin = (seconds Mod 3600) \ 60
objExcel.Cells(intRow, 4).Value = ConvDays
objExcel.Cells(intRow, 5).Value = ConvHour
objExcel.Cells(intRow, 6).Value = ConvMin
End Function
objExcel.Cells(intRow, 7).Value = Now()
intRow = intRow + 1
objExcel.Range("A1:G1").Se lect
objExcel.Selection.Interio r.ColorInd ex = 19
objExcel.Selection.Font.Co lorIndex = 11
objExcel.Selection.Font.Bo ld = True
objExcel.Cells.EntireColum n.AutoFit
Set objWMIService = Nothing
Set objExcel = Nothing
Set colAdapters = Nothing
Set colObjects = Nothing
Wscript.Echo "Done"
I'm trying to use the following script to give me a report on Server Uptime and I can get it to work using a single instance but can't get it to read the MachineList.Txt file
Can anyone help.
Cheers
strComputer = "MachineList.Txt"
'strComputer = InputBox("Enter Machine Name")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Set objExcel = CreateObject("Excel.Applic
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "IP Address"
objExcel.Cells(1, 3).Value = "MAC Address"
objExcel.Cells(1, 4).Value = "Days"
objExcel.Cells(1, 5).Value = "Hours"
objExcel.Cells(1, 6).Value = "Minutes"
objExcel.Cells(1, 7).Value = "Report Time Stamp"
Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfig
For Each objAdapter in colAdapters
objExcel.Cells(intRow, 1).Value = objAdapter.DNSHostName
If Not IsNull(objAdapter.IPAddres
For i = 0 To UBound(objAdapter.IPAddres
objExcel.Cells(intRow, 2).Value = objAdapter.IPAddress(i)
Next
End If
objExcel.Cells(intRow, 3).Value = objAdapter.MACAddress
Next
Set colObjects = objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfOS_S
For Each objWmiObject In colObjects
intPerfTimeStamp = objWmiObject.Timestamp_Obj
intPerfTimeFreq = objWmiObject.Frequency_Obj
intCounter = objWmiObject.SystemUpTime
Next
iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFre
sUptime = ConvertTime(iUptimeInSec)
Function ConvertTime(seconds)
ConvDays = seconds \ (3600 * 24)
ConvHour = (seconds Mod (3600 * 24)) \ 3600
ConvMin = (seconds Mod 3600) \ 60
objExcel.Cells(intRow, 4).Value = ConvDays
objExcel.Cells(intRow, 5).Value = ConvHour
objExcel.Cells(intRow, 6).Value = ConvMin
End Function
objExcel.Cells(intRow, 7).Value = Now()
intRow = intRow + 1
objExcel.Range("A1:G1").Se
objExcel.Selection.Interio
objExcel.Selection.Font.Co
objExcel.Selection.Font.Bo
objExcel.Cells.EntireColum
Set objWMIService = Nothing
Set objExcel = Nothing
Set colAdapters = Nothing
Set colObjects = Nothing
Wscript.Echo "Done"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Okay, the counter's not incrementing properly then.
As for line 21 - it would appear that for whatever reason strComputer was empty...
You could troubleshoot it by placing a
wscript.echo strComputer
just before that line (or msgbox in place of wscript.echo).
Aside from that - can you verify that your text file is seperated by hard carriage returns (or if they're delimeted some other way, let me know).
I'll look at the counter now...
As for line 21 - it would appear that for whatever reason strComputer was empty...
You could troubleshoot it by placing a
wscript.echo strComputer
just before that line (or msgbox in place of wscript.echo).
Aside from that - can you verify that your text file is seperated by hard carriage returns (or if they're delimeted some other way, let me know).
I'll look at the counter now...
I don't completely follow the logic, but I believe this would remedy the counter problem...
Move these lines:
objExcel.Cells(intRow, 7).Value = Now()
intRow = intRow + 1
just after your
sUptime = ConvertTime(iUptimeInSec)
(line 41)
Let me know if that fixes things...
Move these lines:
objExcel.Cells(intRow, 7).Value = Now()
intRow = intRow + 1
just after your
sUptime = ConvertTime(iUptimeInSec)
(line 41)
Let me know if that fixes things...
ASKER
Hi Sirbounty
Hard Carriage returns
Also thanks for the tip regarding "wscript.echo strComputer" the issue was ending I had a hard carriage return after the last server, so it read this line as well, removing the last HCR allowed the script to complete successfully.
Cheers
Hard Carriage returns
Also thanks for the tip regarding "wscript.echo strComputer" the issue was ending I had a hard carriage return after the last server, so it read this line as well, removing the last HCR allowed the script to complete successfully.
Cheers
ASKER
Perfect!!
Cheers for that
Cheers for that
Happy to have helped - thanx!
~sirbounty
~sirbounty
ASKER
Thanks for your quick response
When I use your script, it opens Excel, and inserts the first server, and than replaces it with the second server and so on. So I only get the first row.
I also receive an error
Script: C:\Uptime.vbs
Line: 21
Char: 3
Error: 0x80041021
Code: 80041021
Source: (null)
This seems to refer to the following line:
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=imper
Cheers