Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Saving Script Output to a text file?

Posted on 2006-03-23
Medium Priority
Last Modified: 2008-01-09
This is a question on a previus posting: Q_21545048.html

I am trying to run the script and save the output (serial number) to a text file. I am trying to save it to a network share but am testing in on my local computer \\swernette\scripting.  I have also tried saving it to C:\scripting which is where the scripting folder is located.

The error that I get is:
Script:   C:\SerialNumber.vbs
Line:      9
Char:     2
Error:    Bad file name or number
Code:    800A0034
Source: Microsoft VBScript runtime error

Here is the script:
 set fso = createobject("scripting.filesystemobject")
 arAddresses = GetIPAddresses()

 strComputer = "."
 Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")

 set ts = fso.createtextfile ("\\swernette\scripting\" & araddresses(0))

 For Each objSMBIOS In colSMBIOS
  ts.writeline "Serial Number: " & objSMBIOS.SerialNumber
  ts.writeline "Asset Tag: " & objSMBIOS.SMBIOSAssetTag

 set ts = nothing
 set fso = nothing

Function GetIPAddresses()

' Returns array of IP Addresses as output
' by ipconfig or winipcfg...
' Win98/WinNT have ipconfig (Win95 doesn't)
' Win98/Win95 have winipcfg (WinNt doesn't)
' Note: The PPP Adapter (Dial Up Adapter) is
' excluded if not connected (IP address will be
' and included if it is connected.
  set sh = createobject("wscript.shell")

  Set Env = sh.Environment("PROCESS")
  if Env("OS") = "Windows_NT" then
    workfile = fso.gettempname
    sh.run "%comspec% /c ipconfig > " & workfile,0,true
    'winipcfg in batch mode sends output to
    'filename winipcfg.out
    workfile = "winipcfg.out"
    sh.run "winipcfg /batch" ,0,true
  end if
  set sh = nothing
  set ts = fso.opentextfile(workfile)
  data = split(ts.readall,vbcrlf)
  fso.deletefile workfile
  arIPAddress = array()
  index = -1
  for n = 0 to ubound(data)
    if instr(data(n),"IP Address") then
      parts = split(data(n),":")
      if trim(parts(1)) <> "" then
        index = index + 1
        ReDim Preserve arIPAddress(index)
        arIPAddress(index)= trim(cstr(parts(1)))
      end if
    end if
  GetIPAddresses = arIPAddress
End Function
Question by:cmushaun
  • 2
LVL 10

Accepted Solution

fostejo earned 500 total points
ID: 16272149

I suspect there's a bug in the GetIPAddresses function. It's returning the machines IP address with a CR appended to it which the VB's CreateTextFile function objects to.

To fix, you could either correct the function itself by getting it to take the additional character off the values it returns or, alternatively, do the same thing at the top of the script once you've got the array off the function.  I'd suggest the former as it will ensure that when you re-use the script elsewhere you don't run into the same issue!  Add the following line just below the "arIPAddress(index)= trim(cstr(parts(1)))" line in the function.




Author Comment

ID: 16272430
That worked wonderfully. I'm just learning scripting so it all seems overwhelming but very intriguing. I may be posting more question to take this project further so be looking for me :-)
LVL 10

Expert Comment

ID: 16275742
No problem cmushaun, I'll keep an eye out for you!  It'll get easier with practice!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Simple Linear Regression

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question