Saving Script Output to a text file?

Posted on 2006-03-23
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("")

  Set Env = sh.Environment("PROCESS")
  if Env("OS") = "Windows_NT" then
    workfile = fso.gettempname "%comspec% /c ipconfig > " & workfile,0,true
    'winipcfg in batch mode sends output to
    'filename winipcfg.out
    workfile = "winipcfg.out" "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
    LVL 10

    Accepted Solution


    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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    A short article about problems I had with the new location API and permissions in Marshmallow
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now