Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help outputting information to multi txt files 1

Status
Not open for further replies.

Dontbyteme

IS-IT--Management
May 14, 2014
8
US
I have a script that looks at a computerList.txt file, query's each ComputerName listed in the txt file for information and then outputs all that information in 1 txt file. What I'd like to do is have the information output into separate txt files, 1 file for each ComputerName (ComputerName.txt). Here is my code

Code:
'File name for remote system results
        'strComputer= "computerInfo2"
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFile = objFSO.CreateTextFile("ComputerInfoList.txt", True)
        Set objComputerlist = objFSO.OpenTextFile("C:\Users\username.adm\Documents\PatchScriptTest\ComputerListTest.txt", 1)

        On Error Resume Next
'Query network PC's for information
        Do Until objComputerlist.AtEndOfStream
                strComputer = objComputerlist.ReadLine
                Set objGroup = GetObject("" & strComputer)

                For Each objMember In objGroup.Members
                        Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                        objFile.WriteLine "Today is:" & FormatDateTime(Now,0)
                        objFile.WriteLine "Computer:" & strComputer
                        objFile.WriteLine "Serial Number: " & objSMBIOS.SerialNumber

'Find serial number
        Set colSMBIOS = wmi.ExecQuery("Select * from Win32_SystemEnclosure")
        For Each objSMBIOS in colSMBIOS
        objFile.WriteLine "Serial Number: " & objSMBIOS.SerialNumber
        Next

'Find MAC & IP
        Set IPConfigSet = wmi.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True")
        For Each IPConfig in IPConfigSet
                objFile.WriteLine "IP Address:   " & Join(IPConfig.IPAddress,"|")
                objFile.WriteLine "MAC Address:  " & IPConfig.MACAddress
        Next

'Query system to installed KB's
        oTS.WriteLine "INSTALLED HOTFIXES"
        Set colItems = wmi.ExecQuery("Select * from Win32_QuickFixEngineering")
        For Each objItem in colItems
                objFile.WriteLine "" & objItem.HotFixID
        Next

'Print out list of installed software
        objFile.WriteLine
        objFile.WriteLine "INSTALLED SOFTWARE"
        objFile.WriteLine

'Query system for installed software
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Product")
        For Each objItem in colItems
                objFile.WriteLine "Caption: "     & objItem.Caption
                objFile.WriteLine "Version: "     & objItem.Version
                objFile.WriteLine "Description: " & objItem.Description
                objFile.WriteLine 
        Next

                Next
        Loop

 
You can open create a new text file each time, using strComputer for the file name, like below.
Code:
Do Until objComputerlist.AtEndOfStream
   strComputer = objComputerlist.ReadLine
   Set objFile = objFSO.CreateTextFile(strComputer & ".txt", True)
   ...
   objFile.Close
Loop
Also, I would be wary of using [tt]On Error Resume Next[/tt] in this context, as you will never see any runtime errors which can lead to odd results.
 
Dontbyteme the code is already in what you doing just need to take step back and a min and look and I think you'll get more out of how the scripts are helping you accomplish whatever is is you want.
NOTE you should in fact close the file when done with it before exiting script.

Code:
''File name for remote system results
        'strComputer= "computerInfo2"
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objComputerlist = objFSO.OpenTextFile("C:\Users\username.adm\Documents\PatchScriptTest\ComputerListTest.txt", 1)

        On Error Resume Next
'Query network PC's for information
        Do Until objComputerlist.AtEndOfStream
                strComputer = objComputerlist.ReadLine
                Set objFile = objFSO.CreateTextFile(strComputer& ".txt", True)

                Set objGroup = GetObject("" & strComputer)

                For Each objMember In objGroup.Members
                        Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                        objFile.WriteLine "Today is:" & FormatDateTime(Now,0)
                        objFile.WriteLine "Computer:" & strComputer
                        objFile.WriteLine "Serial Number: " & objSMBIOS.SerialNumber

'Find serial number
        Set colSMBIOS = wmi.ExecQuery("Select * from Win32_SystemEnclosure")
        For Each objSMBIOS in colSMBIOS
        objFile.WriteLine "Serial Number: " & objSMBIOS.SerialNumber
        Next

'Find MAC & IP
        Set IPConfigSet = wmi.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True")
        For Each IPConfig in IPConfigSet
                objFile.WriteLine "IP Address:   " & Join(IPConfig.IPAddress,"|")
                objFile.WriteLine "MAC Address:  " & IPConfig.MACAddress
        Next

'Query system to installed KB's
        oTS.WriteLine "INSTALLED HOTFIXES"
        Set colItems = wmi.ExecQuery("Select * from Win32_QuickFixEngineering")
        For Each objItem in colItems
                objFile.WriteLine "" & objItem.HotFixID
        Next

'Print out list of installed software
        objFile.WriteLine
        objFile.WriteLine "INSTALLED SOFTWARE"
        objFile.WriteLine

'Query system for installed software
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Product")
        For Each objItem in colItems
                objFile.WriteLine "Caption: "     & objItem.Caption
                objFile.WriteLine "Version: "     & objItem.Version
                objFile.WriteLine "Description: " & objItem.Description
                objFile.WriteLine 
        Next

                Next
        Loop 

'==========================================================================

[yinyang] VulcanJedi[pc2]
 
Thank you guitarzan I added your line of code (Set objFile = objFSO.CreateTextFile(strComputer & ".txt", True)), plus did a bunch of editing to my code after finding several errors, and now it is working great. Thank you so much for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top