help with text file with computer names

Hey guys I am a newbie powershell guy and under normal circumstances would use the get-content command to pulll computer names. However, for this solution I only found that it can be done in vbscript. Can someone please kindly show me how I can do two things. The first is I have a text file full of computer names in a directory like c:\temp\computernames.txt I need to read from this instead of this array below.

Second thing is I need to put this info into a excel spread sheet. Please help thank you.

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

arrComputers = Array(".")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSNdis_CoDriverVersion", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
WScript.Echo "Active: " & objItem.Active
WScript.Echo "InstanceName: " & objItem.InstanceName
WScript.Echo "NdisCoDriverVersion: " & objItem.NdisCoDriverVersion
WScript.Echo
Next
Next
techdriveAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Try this change:

objOutfile.Write(strComputer & "," & objItem.Active & "," & objItem.InstanceName & "," & objItem.NdisCoDriverVersion)

Open in new window

~bp
0
 
Bill PrewCommented:
Give this a try.  Save as a VBS, and run passing two filenames as in:

cscript EE26938950.vbs "c:\temp\computers.txt" "c:\temp\output.csv"


Then just open the CSV in Excel.

On Error Resume Next

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

' Get input and output file names from command line parms
If (WScript.Arguments.Count > 0) Then
  strInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename for computers specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  strOutfile = WScript.Arguments(1)
Else
  WScript.Echo "No filename for output specified."
  WScript.Quit
End If

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set objInfile = objFSO.OpenTextFile(strInfile, ForReading, False, TriStateUseDefault)
strData = objInfile.ReadAll
objInfile.Close
Set objInfile = Nothing

' Open output CSV file
Set objOutfile = objFSO.OpenTextFile(strOutfile, ForWriting, True)
objOutfile.Write("Active,Instance,Version")

' Process each computer name from input file
arrComputers = Split(strData, vbCrLf)
For Each strComputer In arrComputers
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM MSNdis_CoDriverVersion", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems
      objOutfile.Write(objItem.Active & "," & objItem.InstanceName & "," & objItem.NdisCoDriverVersion)
   Next
Next 

' Cleanup and end
objOutfile.Close
Set objOutfile = Nothing
Set oFSO = Nothing
Wscript.Quit

Open in new window

~bp
0
 
techdriveAuthor Commented:
Works like a charm thanks and one last thing. I noticed on the output there is no way to tell what computer output what info. Is there a way to stick in some type of variable to output the hostname/computer name and then move on to the next machine.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.