Outputting Standard Out information from a VBS Script

Posted on 2004-11-30
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")
' "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") "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

    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
    LVL 1

    Author Comment

    Thanks BlueDevil,

    Really appreciate it!!!


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    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…
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 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

    17 Experts available now in Live!

    Get 1:1 Help Now