Ok, I’ve written a script that is suppose to search a list of IPs and return WMI info and write it to a csv file. The only thing I get returned though is “No Response.” I know the IPs I am searching are on line and working. Can someone please help me. I figure there is something wrong with the code.<\/p>\n
' NetworkFindInfo.vbs - Windows Logon Script.\n' VBScript - Look up a computers info. \n' Author Chris Collins\n' Version 1.5 - October 2018\n' ----------------------------------------------------------' \n\nOption Explicit\n\n' Constants - return values\nConst wbemFlagReturnImmediately = &h10\nConst wbemFlagForwardOnly = &h20\nConst PATH_TO_INPUT = \"C:\\Users\\ccollins\\Desktop\\NetworkFindingInfo\\ComputerList.txt\"\nConst PATH_TO_OUTPUT = \"C:\\Users\\ccollins\\Desktop\\NetworkFindingInfo\\ComputerInfo.csv\"\n\nDim fso\nSet fso = WScript.CreateObject(\"Scripting.FileSystemObject\")\n\nDim shl\nSet shl = WScript.CreateObject(\"WScript.Shell\")\n\nDim input\nSet input = fso.OpenTextFile(PATH_TO_INPUT)\n\n' Build Output File\nDim output\nSet output = fso.CreateTextFile(PATH_TO_OUTPUT, True)\n\noutput.WriteLine \"IP,Hostname,Domain,Serial Number,Make,Model,DHCP,BIOS Version,Operating System,CPU,Memory (MB),Disk Drives (MB),MAC Address\"\n\n' Declaration of global variables\nDim wmiService\nDim wmiResults\n\nDim IP\nDim hostname\nDim domain\nDim make\nDim model\nDim biosversion\nDim operatingSystem\nDim serialNumber\nDim cpu\nDim memory\nDim drives\nDim mac\nDim dhcp\n\nDim line\nDim exec\nDim pingResults\n\nWhile Not input.AtEndOfStream\nline = input.ReadLine\nIP = \"\"\nhostname = \"\"\ndomain = \"\"\nmake = \"\"\nmodel = \"\"\nbiosversion = \"\"\noperatingSystem = \"\"\nserialNumber = \"\"\ncpu = \"\"\nmemory = \"\"\ndrives = \"\"\nmac = \"\"\ndhcp = \"\"\n\nSet exec = shl.Exec(\"ping -n 2 -w 1000 \" & line)\npingResults = LCase(exec.StdOut.ReadAll)\n\nIf InStr(pingResults, \"reply from\") Then\nOn Error Resume Next\n\nSet wmiService = GetObject(\"winmgmts:\\\\\" & line & \"\\root\\CIMV2\")\n\nIf Err.Number > 0 Then\noutput.WriteLine line & \",Error: \" & Err.Description\n\nElse\n\nIP = line\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_BIOS\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nDim item\nFor Each item In wmiResults\nserialNumber = Trim(item.SerialNumber)\nbiosversion = Trim(item.SMBIOSBIOSVersion)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_ComputerSystem\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\nmake = Trim(item.Manufacturer)\nmodel = Trim(item.Model)\nhostname = Trim(item.Name)\ndomain = Trim(item.Domain)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_OperatingSystem\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\noperatingSystem = Trim(item.Name)\noperatingSystem = Split(operatingSystem, \"|\")(0)\nmemory = Round(Trim(item.TotalVisibleMemorySize) / 1024, 2)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_Processor\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\ncpu = Trim(item.Name)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = 1\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\nmac = Trim(item.MACAddress)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_NetworkAdapterconfiguration WHERE IPenabled = true\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\ndhcp = Trim(item.dhcpenabled)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_LogicalDisk WHERE DriveType=3\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\ndrives = drives & Trim(item.DeviceID) & \" \" & Round(Trim(item.Size) / (1024^2), 2) & \";\"\nNext\n\noutput.WriteLine IP & \",\" & hostname & \",\" & domain & \",\" & serialNumber & \",\" & make & \",\" & model & \",\" & dhcp & \",\" & biosversion & \",\" & operatingSystem & \",\" & cpu & \",\" & memory & \",\" & drives & \",\" & mac\nEnd If\nElse\noutput.WriteLine line & \",No Response\"\nEnd If\nWend\n\noutput.Close\ninput.Close\n\nSet wmiService = Nothing\nSet wmiresults = Nothing\n\nMsgbox(\"Done Collecting Data\")\n<\/code><\/pre>","upvoteCount":2,"answerCount":6,"datePublished":"2019-05-15T16:02:36.000Z","author":{"@type":"Person","name":"chriscollins13","url":"https://community.spiceworks.com/u/chriscollins13"},"acceptedAnswer":{"@type":"Answer","text":"\n\n
<\/div>\n Chris909191:<\/div>\n
\n
The MsgBox (line) gives me ÿþ1 and the next is blank.<\/p>\n<\/blockquote>\n<\/aside>\n
There are different methods of encoding text files that all end in the same extension. Assuming your input file has all of the necessary information, try saving it as another type of Encoding. Using notepad, you should see this right beside the save button. (eg. ASCII or UTF-8) and see if you get any different results in testing the ‘line’ data.<\/p>","upvoteCount":0,"datePublished":"2019-05-16T14:15:36.000Z","url":"https://community.spiceworks.com/t/network-finding-info-vbs/712115/5","author":{"@type":"Person","name":"aaronwatson8215","url":"https://community.spiceworks.com/u/aaronwatson8215"}},"suggestedAnswer":[{"@type":"Answer","text":"
Ok, I’ve written a script that is suppose to search a list of IPs and return WMI info and write it to a csv file. The only thing I get returned though is “No Response.” I know the IPs I am searching are on line and working. Can someone please help me. I figure there is something wrong with the code.<\/p>\n
' NetworkFindInfo.vbs - Windows Logon Script.\n' VBScript - Look up a computers info. \n' Author Chris Collins\n' Version 1.5 - October 2018\n' ----------------------------------------------------------' \n\nOption Explicit\n\n' Constants - return values\nConst wbemFlagReturnImmediately = &h10\nConst wbemFlagForwardOnly = &h20\nConst PATH_TO_INPUT = \"C:\\Users\\ccollins\\Desktop\\NetworkFindingInfo\\ComputerList.txt\"\nConst PATH_TO_OUTPUT = \"C:\\Users\\ccollins\\Desktop\\NetworkFindingInfo\\ComputerInfo.csv\"\n\nDim fso\nSet fso = WScript.CreateObject(\"Scripting.FileSystemObject\")\n\nDim shl\nSet shl = WScript.CreateObject(\"WScript.Shell\")\n\nDim input\nSet input = fso.OpenTextFile(PATH_TO_INPUT)\n\n' Build Output File\nDim output\nSet output = fso.CreateTextFile(PATH_TO_OUTPUT, True)\n\noutput.WriteLine \"IP,Hostname,Domain,Serial Number,Make,Model,DHCP,BIOS Version,Operating System,CPU,Memory (MB),Disk Drives (MB),MAC Address\"\n\n' Declaration of global variables\nDim wmiService\nDim wmiResults\n\nDim IP\nDim hostname\nDim domain\nDim make\nDim model\nDim biosversion\nDim operatingSystem\nDim serialNumber\nDim cpu\nDim memory\nDim drives\nDim mac\nDim dhcp\n\nDim line\nDim exec\nDim pingResults\n\nWhile Not input.AtEndOfStream\nline = input.ReadLine\nIP = \"\"\nhostname = \"\"\ndomain = \"\"\nmake = \"\"\nmodel = \"\"\nbiosversion = \"\"\noperatingSystem = \"\"\nserialNumber = \"\"\ncpu = \"\"\nmemory = \"\"\ndrives = \"\"\nmac = \"\"\ndhcp = \"\"\n\nSet exec = shl.Exec(\"ping -n 2 -w 1000 \" & line)\npingResults = LCase(exec.StdOut.ReadAll)\n\nIf InStr(pingResults, \"reply from\") Then\nOn Error Resume Next\n\nSet wmiService = GetObject(\"winmgmts:\\\\\" & line & \"\\root\\CIMV2\")\n\nIf Err.Number > 0 Then\noutput.WriteLine line & \",Error: \" & Err.Description\n\nElse\n\nIP = line\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_BIOS\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nDim item\nFor Each item In wmiResults\nserialNumber = Trim(item.SerialNumber)\nbiosversion = Trim(item.SMBIOSBIOSVersion)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_ComputerSystem\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\nmake = Trim(item.Manufacturer)\nmodel = Trim(item.Model)\nhostname = Trim(item.Name)\ndomain = Trim(item.Domain)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_OperatingSystem\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\noperatingSystem = Trim(item.Name)\noperatingSystem = Split(operatingSystem, \"|\")(0)\nmemory = Round(Trim(item.TotalVisibleMemorySize) / 1024, 2)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_Processor\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\ncpu = Trim(item.Name)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = 1\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\nmac = Trim(item.MACAddress)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_NetworkAdapterconfiguration WHERE IPenabled = true\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\ndhcp = Trim(item.dhcpenabled)\nNext\n\nSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_LogicalDisk WHERE DriveType=3\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\nFor Each item In wmiResults\ndrives = drives & Trim(item.DeviceID) & \" \" & Round(Trim(item.Size) / (1024^2), 2) & \";\"\nNext\n\noutput.WriteLine IP & \",\" & hostname & \",\" & domain & \",\" & serialNumber & \",\" & make & \",\" & model & \",\" & dhcp & \",\" & biosversion & \",\" & operatingSystem & \",\" & cpu & \",\" & memory & \",\" & drives & \",\" & mac\nEnd If\nElse\noutput.WriteLine line & \",No Response\"\nEnd If\nWend\n\noutput.Close\ninput.Close\n\nSet wmiService = Nothing\nSet wmiresults = Nothing\n\nMsgbox(\"Done Collecting Data\")\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2019-05-15T16:02:36.000Z","url":"https://community.spiceworks.com/t/network-finding-info-vbs/712115/1","author":{"@type":"Person","name":"chriscollins13","url":"https://community.spiceworks.com/u/chriscollins13"}},{"@type":"Answer","text":"This is where indents help, particularly when using VBS where you don’t have the option of brackets to separate code. When you break it down like this, you see that the final else statement is triggering because your first if statement isn’t returning a true result. (Your ping result variable doesn’t appear to contain “reply from” so your problem lies somewhere in that)<\/p>\n
If InStr(pingResults, \"reply from\") Then\n\tOn Error Resume Next\n\n\tSet wmiService = GetObject(\"winmgmts:\\\\\" & line & \"\\root\\CIMV2\")\n\n\tIf Err.Number > 0 Then\n\t\toutput.WriteLine line & \",Error: \" & Err.Description\n\n\tElse\n\n\t\tIP = line\n\n\t\tSet wmiResults = wmiService.ExecQuery(\"SELECT * FROM Win32_BIOS\", \"WQL\", wbemFlagReturnImmediately + wbemFlagForwardOnly)\n\n\t\t. . . \n\n\t\toutput.WriteLine IP & \",\" & hostname & \",\" & domain & \",\" & serialNumber & \",\" & make & \",\" & model & \",\" & dhcp & \",\" & biosversion & \",\" & \t\toperatingSystem & \",\" & cpu & \",\" & memory & \",\" & drives & \",\" & mac\n\tEnd If\nElse\n\toutput.WriteLine line & \",No Response\"\nEnd If\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-05-16T01:20:39.000Z","url":"https://community.spiceworks.com/t/network-finding-info-vbs/712115/2","author":{"@type":"Person","name":"aaronwatson8215","url":"https://community.spiceworks.com/u/aaronwatson8215"}},{"@type":"Answer","text":"Funny thing about this is that I condensed your code for testing and your ping SHOULD work when you explicitly use an IP address (an example test I used google)<\/p>\n
Set shl = WScript.CreateObject(\"WScript.Shell\")\nSet exec = shl.Exec(\"ping -n 2 -w 1000 \" & \"www.google.com\")\npingResults = LCase(exec.StdOut.ReadAll)\n\nIf InStr(pingResults, \"reply from\") Then msgbox pingResults\n<\/code><\/pre>\nThis test works which suggests your script may not have the right data in the ‘line’ variable when you’re trying to ping. Try adding the line:<\/p>\n
MsgBox line\n<\/code><\/pre>\n. . . just before the ping command in your script. This will give a popup with each line of your input file just before pinging it to help with troubleshooting.<\/p>","upvoteCount":0,"datePublished":"2019-05-16T13:24:43.000Z","url":"https://community.spiceworks.com/t/network-finding-info-vbs/712115/3","author":{"@type":"Person","name":"aaronwatson8215","url":"https://community.spiceworks.com/u/aaronwatson8215"}},{"@type":"Answer","text":"
The MsgBox (line) gives me ÿþ1 and the next is blank. Which tells me it is not reading the file properly. Though I don’t know what I’m missing.<\/p>","upvoteCount":0,"datePublished":"2019-05-16T13:50:16.000Z","url":"https://community.spiceworks.com/t/network-finding-info-vbs/712115/4","author":{"@type":"Person","name":"chriscollins13","url":"https://community.spiceworks.com/u/chriscollins13"}},{"@type":"Answer","text":"
That did it, it just couldn’t read the file.<\/p>","upvoteCount":0,"datePublished":"2019-05-16T14:38:32.000Z","url":"https://community.spiceworks.com/t/network-finding-info-vbs/712115/6","author":{"@type":"Person","name":"chriscollins13","url":"https://community.spiceworks.com/u/chriscollins13"}}]}}
Ok, I’ve written a script that is suppose to search a list of IPs and return WMI info and write it to a csv file. The only thing I get returned though is “No Response.” I know the IPs I am searching are on line and working. Can someone please help me. I figure there is something wrong with the code.
' NetworkFindInfo.vbs - Windows Logon Script.
' VBScript - Look up a computers info.
' Author Chris Collins
' Version 1.5 - October 2018
' ----------------------------------------------------------'
Option Explicit
' Constants - return values
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const PATH_TO_INPUT = "C:\Users\ccollins\Desktop\NetworkFindingInfo\ComputerList.txt"
Const PATH_TO_OUTPUT = "C:\Users\ccollins\Desktop\NetworkFindingInfo\ComputerInfo.csv"
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Dim shl
Set shl = WScript.CreateObject("WScript.Shell")
Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)
' Build Output File
Dim output
Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)
output.WriteLine "IP,Hostname,Domain,Serial Number,Make,Model,DHCP,BIOS Version,Operating System,CPU,Memory (MB),Disk Drives (MB),MAC Address"
' Declaration of global variables
Dim wmiService
Dim wmiResults
Dim IP
Dim hostname
Dim domain
Dim make
Dim model
Dim biosversion
Dim operatingSystem
Dim serialNumber
Dim cpu
Dim memory
Dim drives
Dim mac
Dim dhcp
Dim line
Dim exec
Dim pingResults
While Not input.AtEndOfStream
line = input.ReadLine
IP = ""
hostname = ""
domain = ""
make = ""
model = ""
biosversion = ""
operatingSystem = ""
serialNumber = ""
cpu = ""
memory = ""
drives = ""
mac = ""
dhcp = ""
Set exec = shl.Exec("ping -n 2 -w 1000 " & line)
pingResults = LCase(exec.StdOut.ReadAll)
If InStr(pingResults, "reply from") Then
On Error Resume Next
Set wmiService = GetObject("winmgmts:\\" & line & "\root\CIMV2")
If Err.Number > 0 Then
output.WriteLine line & ",Error: " & Err.Description
Else
IP = line
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
Dim item
For Each item In wmiResults
serialNumber = Trim(item.SerialNumber)
biosversion = Trim(item.SMBIOSBIOSVersion)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
make = Trim(item.Manufacturer)
model = Trim(item.Model)
hostname = Trim(item.Name)
domain = Trim(item.Domain)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_OperatingSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
operatingSystem = Trim(item.Name)
operatingSystem = Split(operatingSystem, "|")(0)
memory = Round(Trim(item.TotalVisibleMemorySize) / 1024, 2)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
cpu = Trim(item.Name)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter = 1", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
mac = Trim(item.MACAddress)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_NetworkAdapterconfiguration WHERE IPenabled = true", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
dhcp = Trim(item.dhcpenabled)
Next
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each item In wmiResults
drives = drives & Trim(item.DeviceID) & " " & Round(Trim(item.Size) / (1024^2), 2) & ";"
Next
output.WriteLine IP & "," & hostname & "," & domain & "," & serialNumber & "," & make & "," & model & "," & dhcp & "," & biosversion & "," & operatingSystem & "," & cpu & "," & memory & "," & drives & "," & mac
End If
Else
output.WriteLine line & ",No Response"
End If
Wend
output.Close
input.Close
Set wmiService = Nothing
Set wmiresults = Nothing
Msgbox("Done Collecting Data")
2 Spice ups
This is where indents help, particularly when using VBS where you don’t have the option of brackets to separate code. When you break it down like this, you see that the final else statement is triggering because your first if statement isn’t returning a true result. (Your ping result variable doesn’t appear to contain “reply from” so your problem lies somewhere in that)
If InStr(pingResults, "reply from") Then
On Error Resume Next
Set wmiService = GetObject("winmgmts:\\" & line & "\root\CIMV2")
If Err.Number > 0 Then
output.WriteLine line & ",Error: " & Err.Description
Else
IP = line
Set wmiResults = wmiService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
. . .
output.WriteLine IP & "," & hostname & "," & domain & "," & serialNumber & "," & make & "," & model & "," & dhcp & "," & biosversion & "," & operatingSystem & "," & cpu & "," & memory & "," & drives & "," & mac
End If
Else
output.WriteLine line & ",No Response"
End If
Funny thing about this is that I condensed your code for testing and your ping SHOULD work when you explicitly use an IP address (an example test I used google)
Set shl = WScript.CreateObject("WScript.Shell")
Set exec = shl.Exec("ping -n 2 -w 1000 " & "www.google.com")
pingResults = LCase(exec.StdOut.ReadAll)
If InStr(pingResults, "reply from") Then msgbox pingResults
This test works which suggests your script may not have the right data in the ‘line’ variable when you’re trying to ping. Try adding the line:
MsgBox line
. . . just before the ping command in your script. This will give a popup with each line of your input file just before pinging it to help with troubleshooting.
The MsgBox (line) gives me ÿþ1 and the next is blank. Which tells me it is not reading the file properly. Though I don’t know what I’m missing.
There are different methods of encoding text files that all end in the same extension. Assuming your input file has all of the necessary information, try saving it as another type of Encoding. Using notepad, you should see this right beside the save button. (eg. ASCII or UTF-8) and see if you get any different results in testing the ‘line’ data.
That did it, it just couldn’t read the file.