Outputting Standard Out information from a VBS Script

Posted on 2004-11-30
Medium Priority
Last Modified: 2010-04-13
Hi guys,

Im pretty new to VBScript, and would love your help.

What I have is the following:

========================================= BOF
bolIsRightSubnet = False
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
    ("Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
            If Left(IPConfig.IPAddress(i), 9) = "10.60.10." Or Left(IPConfig.IPAddress(i), 9) = "10.60.11." Then
    bolIsRightSubnet = True
   End If
    End If
' Hi there
If bolIsRightSubnet = True Then
    strComputerToPing = "tiger"
    bolRunCopyJob = False
    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
        ExecQuery("select * from Win32_PingStatus where address = '"_
            & strComputerToPing & "'")
    For Each objStatus in objPing
        If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
            WScript.Echo("machine " & strComputerToPing & " is not reachable")
      bolRunCopyJob = True
        End If
End If
If bolRunCopyJob = True Then
'      set WshShell = WScript.CreateObject("WScript.Shell")
'      WshShell.run "d:\a.bat"
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "d:\y.txt" , "h:\2\", OverwriteExisting
Else      set WshShell = WScript.CreateObject("WScript.Shell")
      WshShell.run "notepad", 3, False
End If
========================================= BOF

What it does:

It first enumerates the IP Address of the machine
If the IP address is on a particular subnet eg.10.60.10 or 10.60.11.x, it fires the backup job.
If not on this subnet, it doesnt copy.

What Im having trouble with is this.

Id like to write a log file of the vbscript activity.
I know how to do this in a batch file, for example:

net use s: \\server\share >> c:\nab.log 2>&1

Which outputs to the nab.log file the following:

"The command completed successfully"

In my vbscript:

Id like to be able to do the following, logging everything that is going on.

For example, in the log file, Id like:

Wed 27/10/2004
6:45 pm

The IP address of this system is "x.x.x.x"
Executing backup job
Copying file d:\y to H:\2
Backup completed at 6:47pm

Wed 27/10/2004

The IP address of this system is "x.x.x.x"
This system is not on the required subnet
Abort backup job

Any help on this would be greatly appreciated.


Question by:Simon336697
LVL 76

Accepted Solution

David Lee earned 2000 total points
ID: 12713380
Hello, Simon.

This isn't exactly what you asked for, but it accomplishes your goal.  The simple solution is to create the log file in the script using the following code.

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFSO.CreateTextFile("C:\Log.Txt")  '<-- Change the path and filename as needed

You can then write to the log file wherever you choose using this code:

    objTS.WriteLine "Write whatever you want, including the value(s) of variables"

At the end of your script you'll need to close the file like this:


And of course to avoid memory leaks all objects should be disposed of properly:

    Set objTS = Nothing
    Set objFSO = Nothing

Author Comment

ID: 12723673
Thanks BlueDevil,

Really appreciate it!!!


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
Applications for our next round of the Experts Exchange Scholarship Contest are starting to roll in. It made us wonder what our past winners are up to these days. Here's a look at what four winners experienced with the contest and what they're doing…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 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