Hi,
Please forgive me, I'm new to scripting so I haven't got much experience.
I have a script which works okay. It queries some ip addresses, then it reports their Asset details in a spreadsheet, like it's make, model, os etc. I have pasted the code at the bottom of this post.
I have two queries. One is, how can i get it to check if the ip address is online or not before carrying on with the rest of the script. At the moment, if the address is not responding, the script just hangs. I would like it to check it's available, then carry on with the script, if it isn't, i want it to move on to the next address etc. I've done a bit of digging and found example scripts using ping and win32_pingstatus, but I can't seem to get their code to work with mine.
My next query is that at the moment whilst i'm creating/testing, the script uses a handful of ip addresses which are stated in the code. How can i get it to use the contents of a text file instead? I tried entering InputFile="c:\scripts\Servers.txt", Set ifile = iFSO.OpenTextFile(inputfile) but it doesn't seem to work.
Does anybody have any ideas as to where I am going wrong please?
Thank you,
H
This is the working code I use:
'=-=============================
On Error Resume Next
Set WshNetwork = WScript.CreateObject("WScript.Network")
'unc location of the spreadsheet.
strWorkBook = "\\E6400-6HGZ1L1.dudley.local\c$\scripts\audit.xls"
' Create Excel Spreadsheet
Set app = CreateObject("Excel.Application")
app.Workbooks.Open strWorkBook
Set wb = app.ActiveWorkbook
app.Visible = False
wb.Activate
Set ws = wb.Worksheets(1)
ws.Cells(1,1).Value = "Computername"
ws.Columns(1).ColumnWidth = 20
ws.Cells(1,11).Value = "Username"
ws.Columns(11).ColumnWidth = 30
ws.Cells(1,2).Value = "Manufacturer"
ws.Columns(2).ColumnWidth = 20
ws.Cells(1,3).Value = "Model"
ws.Columns(3).ColumnWidth = 20
ws.Cells(1,4).Value = "Serial Number"
ws.Columns(4).ColumnWidth = 30
ws.Cells(1,5).Value = "System Type"
ws.Columns(5).ColumnWidth = 40
ws.Cells(1,10).Value = "Service Pack"
ws.Columns(10).ColumnWidth = 20
ws.Cells(1,12).Value = "Audit Date"
ws.Columns(12).ColumnWidth = 20
ws.Cells(1,9).Value = "Operating System"
ws.Columns(9).ColumnWidth = 40
ws.Cells(1,7).Value = "Domain"
ws.Columns(7).ColumnWidth = 40
ws.Cells(1,8).Value = "Primary Owner"
ws.Columns(8).ColumnWidth = 40
ws.Cells(1,6).Value = "Name Details"
ws.Columns(6).ColumnWidth = 40
ws.Rows(1).Font.Bold = True
arrComputers = Array("192.168.70.24","192.168.70.19","192.168.70.18","192.168.70.17","192.168.70.14","192.168.70.12")
For Each strComputer In arrComputers
' Get the serial number first to see if it already exists in the spreadsheet
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSerialNumber = "" & objItem.SerialNumber
Next
Set objWMIService = Nothing
Set colItems = Nothing
intRowToUse = -1
For intRowCount = 2 To ws.UsedRange.Rows.Count
If Trim(strSerialNumber) = Trim(ws.Cells(intRowCount,5).Value) Then
intRowToUse = intRowCount
End If
Next
If intRowToUse = -1 Then
intRowToUse = ws.UsedRange.Rows.Count + 1
End If
' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,1).Value = "" & objItem.Caption
ws.Cells(intRowToUse,11).Value = "" & objItem.UserName
ws.Cells(intRowToUse,2).Value = "" & objItem.Manufacturer
ws.Cells(intRowToUse,3).Value = "" & objItem.Model
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Output the Serial Number
ws.Cells(intRowToUse,4).Value = strSerialNumber
'
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,9).Value = "" & objItem.Caption
ws.Cells(intRowToUse,8).Value = "" & objItem.CSDVersion
ws.Cells(intRowToUse,10).Value = "" & FormatNumber(objItem.TotalVisibleMemorySize/1024,0)
Next
' Get System type details Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,5).Value = "" & objItem.SystemType
Next
' Get Domain details Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,7).Value = "" & objItem.Domain
Next
' Get primary owner name Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,8).Value = "" & objItem.PrimaryOwnerName
Next
' Get name Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,6).Value = "" & objItem.Name
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get & Writeout current Date
ws.Cells(intRowToUse,12).value = "" & Day(Now) & "-" & Month(Now) & "-" & Year(Now)
next
'Autofit all columns
app.ActiveSheet.Columns.EntireColumn.AutoFit
' Save Audit File
app.DisplayAlerts = False
'wb.Saved = True
wb.Close True
app.DisplayAlerts = False
app.quit
MsgBox "Done"
'====================
Please forgive me, I'm new to scripting so I haven't got much experience.
I have a script which works okay. It queries some ip addresses, then it reports their Asset details in a spreadsheet, like it's make, model, os etc. I have pasted the code at the bottom of this post.
I have two queries. One is, how can i get it to check if the ip address is online or not before carrying on with the rest of the script. At the moment, if the address is not responding, the script just hangs. I would like it to check it's available, then carry on with the script, if it isn't, i want it to move on to the next address etc. I've done a bit of digging and found example scripts using ping and win32_pingstatus, but I can't seem to get their code to work with mine.
My next query is that at the moment whilst i'm creating/testing, the script uses a handful of ip addresses which are stated in the code. How can i get it to use the contents of a text file instead? I tried entering InputFile="c:\scripts\Servers.txt", Set ifile = iFSO.OpenTextFile(inputfile) but it doesn't seem to work.
Does anybody have any ideas as to where I am going wrong please?
Thank you,
H
This is the working code I use:
'=-=============================
On Error Resume Next
Set WshNetwork = WScript.CreateObject("WScript.Network")
'unc location of the spreadsheet.
strWorkBook = "\\E6400-6HGZ1L1.dudley.local\c$\scripts\audit.xls"
' Create Excel Spreadsheet
Set app = CreateObject("Excel.Application")
app.Workbooks.Open strWorkBook
Set wb = app.ActiveWorkbook
app.Visible = False
wb.Activate
Set ws = wb.Worksheets(1)
ws.Cells(1,1).Value = "Computername"
ws.Columns(1).ColumnWidth = 20
ws.Cells(1,11).Value = "Username"
ws.Columns(11).ColumnWidth = 30
ws.Cells(1,2).Value = "Manufacturer"
ws.Columns(2).ColumnWidth = 20
ws.Cells(1,3).Value = "Model"
ws.Columns(3).ColumnWidth = 20
ws.Cells(1,4).Value = "Serial Number"
ws.Columns(4).ColumnWidth = 30
ws.Cells(1,5).Value = "System Type"
ws.Columns(5).ColumnWidth = 40
ws.Cells(1,10).Value = "Service Pack"
ws.Columns(10).ColumnWidth = 20
ws.Cells(1,12).Value = "Audit Date"
ws.Columns(12).ColumnWidth = 20
ws.Cells(1,9).Value = "Operating System"
ws.Columns(9).ColumnWidth = 40
ws.Cells(1,7).Value = "Domain"
ws.Columns(7).ColumnWidth = 40
ws.Cells(1,8).Value = "Primary Owner"
ws.Columns(8).ColumnWidth = 40
ws.Cells(1,6).Value = "Name Details"
ws.Columns(6).ColumnWidth = 40
ws.Rows(1).Font.Bold = True
arrComputers = Array("192.168.70.24","192.168.70.19","192.168.70.18","192.168.70.17","192.168.70.14","192.168.70.12")
For Each strComputer In arrComputers
' Get the serial number first to see if it already exists in the spreadsheet
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem In colItems
strSerialNumber = "" & objItem.SerialNumber
Next
Set objWMIService = Nothing
Set colItems = Nothing
intRowToUse = -1
For intRowCount = 2 To ws.UsedRange.Rows.Count
If Trim(strSerialNumber) = Trim(ws.Cells(intRowCount,5).Value) Then
intRowToUse = intRowCount
End If
Next
If intRowToUse = -1 Then
intRowToUse = ws.UsedRange.Rows.Count + 1
End If
' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,1).Value = "" & objItem.Caption
ws.Cells(intRowToUse,11).Value = "" & objItem.UserName
ws.Cells(intRowToUse,2).Value = "" & objItem.Manufacturer
ws.Cells(intRowToUse,3).Value = "" & objItem.Model
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
'Output the Serial Number
ws.Cells(intRowToUse,4).Value = strSerialNumber
'
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,9).Value = "" & objItem.Caption
ws.Cells(intRowToUse,8).Value = "" & objItem.CSDVersion
ws.Cells(intRowToUse,10).Value = "" & FormatNumber(objItem.TotalVisibleMemorySize/1024,0)
Next
' Get System type details Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,5).Value = "" & objItem.SystemType
Next
' Get Domain details Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,7).Value = "" & objItem.Domain
Next
' Get primary owner name Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,8).Value = "" & objItem.PrimaryOwnerName
Next
' Get name Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem In colItems
ws.Cells(intRowToUse,6).Value = "" & objItem.Name
Next
Set objWMIService = Nothing
Set colItems = Nothing
'
' Get & Writeout current Date
ws.Cells(intRowToUse,12).value = "" & Day(Now) & "-" & Month(Now) & "-" & Year(Now)
next
'Autofit all columns
app.ActiveSheet.Columns.EntireColumn.AutoFit
' Save Audit File
app.DisplayAlerts = False
'wb.Saved = True
wb.Close True
app.DisplayAlerts = False
app.quit
MsgBox "Done"
'====================