Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with VBScript

Posted on 2006-11-15
7
Medium Priority
?
612 Views
Last Modified: 2008-01-09
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=impersonate}!\\" & strComputer & "\root\cimv2")

Set objExcel = CreateObject("Excel.Application")
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_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objAdapter in colAdapters
objExcel.Cells(intRow, 1).Value = objAdapter.DNSHostName
If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)
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_System")
For Each objWmiObject In colObjects
intPerfTimeStamp = objWmiObject.Timestamp_Object
intPerfTimeFreq = objWmiObject.Frequency_Object
intCounter = objWmiObject.SystemUpTime
Next
iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFreq
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").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

Set objWMIService = Nothing
Set objExcel = Nothing
Set colAdapters = Nothing
Set colObjects = Nothing

Wscript.Echo "Done"
0
Comment
Question by:matrixnz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 1000 total points
ID: 17952717
This is untested, but it should work...

Dim objFSO, strComputer, ts, strData, strFile
strFile = "MachineList.Txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ts = objFso.OpenTextFile(strFile)
strData=Split(ts.ReadAll,vbNewLine)

Set objExcel = CreateObject("Excel.Application")
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"

For each strComputer in strData  'Loop through each element in the strData array
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
  For Each objAdapter in colAdapters
    objExcel.Cells(intRow, 1).Value = objAdapter.DNSHostName
    If Not IsNull(objAdapter.IPAddress) Then
      For i = 0 To UBound(objAdapter.IPAddress)
        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_System")
  For Each objWmiObject In colObjects
    intPerfTimeStamp = objWmiObject.Timestamp_Object
    intPerfTimeFreq = objWmiObject.Frequency_Object
    intCounter = objWmiObject.SystemUpTime
  Next

  iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFreq
  sUptime = ConvertTime(iUptimeInSec)
Next

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").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

Set objWMIService = Nothing
Set objExcel = Nothing
Set colAdapters = Nothing
Set colObjects = Nothing

Wscript.Echo "Done"
0
 
LVL 29

Author Comment

by:matrixnz
ID: 17953097
Hi Sirbounty

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=impersonate}!\\" & strComputer & "\root\cimv2")

Cheers
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17953151
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...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 67

Expert Comment

by:sirbounty
ID: 17953179
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...
0
 
LVL 29

Author Comment

by:matrixnz
ID: 17953197
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
0
 
LVL 29

Author Comment

by:matrixnz
ID: 17953278
Perfect!!

Cheers for that
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 17953549
Happy to have helped - thanx!
~sirbounty
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question