ericb1
asked on
Output script results to text file
I'm not a big vbscript guy, so I'm not sure what I'm doing here. I have a vb script (a .vbs file on my pc) running this script to get the serial # of the pc:
On Error Resume Next
StrComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S elect * from Win32_BIOS",,48)
For Each objItem in colItems
Wscript.echo "SerialNumber: " & objItem.SerialNumber
Next
However, this just outputs the serial # to the screen. I need to put these serial numbers into a text file. So I added some lines to do this:
Open ("C:\results.txt") For Output As #1
Write #1, "SerialNumber: " & objItem.SerialNumber
But I keep getting an error: Expected End of Statement on the Open line right at the "For" part.
Am I doing this right? I'm sure it's just something stupid I'm doing, any help is appreciated, thanks!
On Error Resume Next
StrComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S
For Each objItem in colItems
Wscript.echo "SerialNumber: " & objItem.SerialNumber
Next
However, this just outputs the serial # to the screen. I need to put these serial numbers into a text file. So I added some lines to do this:
Open ("C:\results.txt") For Output As #1
Write #1, "SerialNumber: " & objItem.SerialNumber
But I keep getting an error: Expected End of Statement on the Open line right at the "For" part.
Am I doing this right? I'm sure it's just something stupid I'm doing, any help is appreciated, thanks!
or you can open for append if you want to add to the file:
Open "C:\results.txt" For append As #1
Print #1, "SerialNumber: " & objItem.SerialNumber
Close #1
Open "C:\results.txt" For append As #1
Print #1, "SerialNumber: " & objItem.SerialNumber
Close #1
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S elect * from Win32_BIOS", , 48)
Open "C:\results.txt" For Output As #1
For Each objItem In colItems
Print #1, "SerialNumber: " & objItem.SerialNumber
Next
Close #1
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S
Open "C:\results.txt" For Output As #1
For Each objItem In colItems
Print #1, "SerialNumber: " & objItem.SerialNumber
Next
Close #1
ASKER
I still get the same thing: a compilation error: Expected End of Statement on line 5, Character 23 which is the F in For Output statement.
I'm running this directly from my pc as a .vbs file, not sure if that makes a difference? Thanks!
I'm running this directly from my pc as a .vbs file, not sure if that makes a difference? Thanks!
use fso:
Dim fso,txtFile
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
Set txtFile = fso.CreateTextFile("C:\res ults.txt", True)
txtFile.WriteLine "SerialNumber: " & objItem.SerialNumber
txtFile.Close
Set txtFile = Nothing
Set fso = Nothing
Dim fso,txtFile
Set fso = CreateObject("Scripting.Fi
Set txtFile = fso.CreateTextFile("C:\res
txtFile.WriteLine "SerialNumber: " & objItem.SerialNumber
txtFile.Close
Set txtFile = Nothing
Set fso = Nothing
or maybe:
Dim fso,txtFile
On Error Resume Next
StrComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S elect * from Win32_BIOS",,48)
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
Set txtFile = fso.CreateTextFile("C:\res ults.txt", True)
For Each objItem in colItems
txtFile.WriteLine "SerialNumber: " & objItem.SerialNumber
next
txtFile.Close
Set txtFile = Nothing
Set fso = Nothing
Dim fso,txtFile
On Error Resume Next
StrComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S
Set fso = CreateObject("Scripting.Fi
Set txtFile = fso.CreateTextFile("C:\res
For Each objItem in colItems
txtFile.WriteLine "SerialNumber: " & objItem.SerialNumber
next
txtFile.Close
Set txtFile = Nothing
Set fso = Nothing
Don't use anything. If its plain VBS just PIPE it to a file like :
C:\cscript yourfile.vbs >> result.txt
C:\cscript yourfile.vbs >> result.txt
Hiding the scripting logo would be like :
C:\cscript yourfile.vbs >> result.txt //nologo
note : In batch or scripting the ECHO command parses the text to screen. The parsed text can always be PIPED to a textfile.
C:\cscript yourfile.vbs >> result.txt //nologo
note : In batch or scripting the ECHO command parses the text to screen. The parsed text can always be PIPED to a textfile.
ASKER
no dice. The fso method seemed to work, it didn't give any errors, but the txt file wasn't created.
If you remove the On Error line,does it cause an error?
ASKER
Oops, yes it does. I get an error on creating the text file: ActiveX component can't create object: 'Scripting.FileSystemObjec t'
You might need to install this:
http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp
http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp
ASKER
Not sure how to allow the file to be created, do I have to allow ActiveX permissions somewhere? Any help is appreciated, thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
vinnyd79 has the correct answer. Remove "On Error Resume Next" to see what if anything is causing an error for you...
Open "C:\results.txt" For Output As #1
Print #1, "SerialNumber: " & objItem.SerialNumber
Close #1