Redirect script output to log file

Home Forums Scripting General Scripting Redirect script output to log file

This topic contains 2 replies, has 2 voices, and was last updated by Avatar igor7 8 years, 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • Avatar
    igor7
    Member
    #157049

    Hi!!
    I need redirect output of following script into log file. The script bellows retrieve WAN IP address using IE:

    Option Explicit
    Dim dtmStart, lngSeconds

    dtmStart = Now
    WScript.Echo “InternetExplorer.Application ” _
    & MyIP_IE( ) & ” (” _
    & DateDiff( “s”, dtmStart, Now ) & ” seconds)”

    Function MyIP_IE( )
    ‘ Name: MyIP_IE
    ‘ Function: Display your WAN IP address using Internet Explorer
    ‘ Usage: ret = MyIP_IE( )
    ‘ Returns: WAN (or global) IP address

    ‘ This script uses WhatIsMyIP.com’s automation page
    ‘ [URL]http://automation.whatismyip.com/n09230945.asp[/URL]
    ‘ Script Source [URL=”http://www.robvanderwoude.com/”]http://www.robvanderwoude.com[/URL]
    Dim blnTimedOut, i, objIE, objMatch, objRE, strText, strURL
    ‘ Return value if IP address couldn’t be retrieved
    MyIP_IE = “0.0.0.0”
    ‘ Open the appropriate URL in Internet Explorer
    strURL = “[URL]http://automation.whatismyip.com/n09230945.asp[/URL]”
    Set objIE = CreateObject( “InternetExplorer.Application” )
    objIE.Visible = False
    objIE.Navigate2 strURL
    ‘ Wait till IE is ready
    i = 0
    blnTimedOut = False
    Do While objIE.Busy
    WScript.Sleep 100
    i = i + 1
    ‘ Time out after 10 seconds
    If i > 100 Then
    blnTimedOut = True
    Exit Do
    End If
    Loop
    ‘ Retrieve the URL’s text
    If Not blnTimedOut Then MyIP_IE = objIE.Document.Body.InnerText
    ‘ Close the Internet Explorer session
    objIE.Quit
    Set objIE = Nothing
    End Function
    [/CODE]

    I finnd similar thread: [URL]http://forums.petri.com/showthread.php?t=29121[/URL] where Rems advice to run the script from the command line (with cscript) and redirect the output to a text file. I’ve created shortcut, edited his properties as following:

    [CODE]
    %comspec% /c “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log”
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]But whenever I’m running this shortcut it create an empty WANIP.log file on the desktop (I’m running it on Windows 7 computer with elevated privileges).[/FONT][/COLOR]
    [COLOR=#555a5f][FONT=Verdana]By the same token, script itself working normally,- it return echo message with my current WAN IP address. So, why it doesn’t redirect script output into the log file?[/FONT][/COLOR][CODE]
    Option Explicit
    Dim dtmStart, lngSeconds

    dtmStart = Now
    WScript.Echo “InternetExplorer.Application ” _
    & MyIP_IE( ) & ” (” _
    & DateDiff( “s”, dtmStart, Now ) & ” seconds)”

    Function MyIP_IE( )
    ‘ Name: MyIP_IE
    ‘ Function: Display your WAN IP address using Internet Explorer
    ‘ Usage: ret = MyIP_IE( )
    ‘ Returns: WAN (or global) IP address

    ‘ This script uses WhatIsMyIP.com’s automation page
    http://automation.whatismyip.com/n09230945.asp
    ‘ Script Source http://www.robvanderwoude.com
    Dim blnTimedOut, i, objIE, objMatch, objRE, strText, strURL
    ‘ Return value if IP address couldn’t be retrieved
    MyIP_IE = “0.0.0.0”
    ‘ Open the appropriate URL in Internet Explorer
    strURL = “http://automation.whatismyip.com/n09230945.asp
    Set objIE = CreateObject( “InternetExplorer.Application” )
    objIE.Visible = False
    objIE.Navigate2 strURL
    ‘ Wait till IE is ready
    i = 0
    blnTimedOut = False
    Do While objIE.Busy
    WScript.Sleep 100
    i = i + 1
    ‘ Time out after 10 seconds
    If i > 100 Then
    blnTimedOut = True
    Exit Do
    End If
    Loop
    ‘ Retrieve the URL’s text
    If Not blnTimedOut Then MyIP_IE = objIE.Document.Body.InnerText
    ‘ Close the Internet Explorer session
    objIE.Quit
    Set objIE = Nothing
    End Function
    [/CODE]

    I finnd similar thread: http://forums.petri.com/showthread.php?t=29121 where Rems advice to run the script from the command line (with cscript) and redirect the output to a text file. I’ve created shortcut, edited his properties as following:

    %comspec% /c “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log”
    [/CODE]

    [COLOR=#555a5f][FONT=Verdana]But whenever I’m running this shortcut it create an empty WANIP.log file on the desktop (I’m running it on Windows 7 computer with elevated privileges).[/FONT][/COLOR]
    [COLOR=#555a5f][FONT=Verdana]By the same token, script itself working normally,- it return echo message with my current WAN IP address. So, why it doesn’t redirect script output into the log file?[/FONT][/COLOR][CODE]
    %comspec% /c “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log”
    [/CODE]

    But whenever I’m running this shortcut it create an empty WANIP.log file on the desktop (I’m running it on Windows 7 computer with elevated privileges).
    By the same token, script itself working normally,- it return echo message with my current WAN IP address. So, why it doesn’t redirect script output into the log file?

    Rems
    Rems
    Moderator
    #228093

    Re: Redirect script output to log file

    igor7;249987 wrote:
    I’ve created shortcut, edited his properties as following:

    %comspec% /c “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log”
    [/CODE]
    [/QUOTE]

    The difference between your command line and the one in the example is that [B]cmd /c[/B] in your script is directly followed by a double quote. A behavior is if the first character after cmd /c (or cmd /k) is a quote character and there are more than two quote signs used in the complete command line, the leading character and the last quote character will be removed, preserving any text after the last quote character. This behavior causes the command line will be start like this:
    [B][COLOR=”Orange”]%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log[/COLOR][/B]

    The line should therefore be like:
    [CODE]
    %comspec% /c “”%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log””
    [/CODE]
    OR like,
    [CODE]
    %comspec% /c >”%userprofile%DesktopWANIP.log” “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” [/CODE]

    (I’ve edited the post you referred to, thanks)

    /Rems[CODE]
    %comspec% /c “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log”
    [/CODE]

    The difference between your command line and the one in the example is that cmd /c in your script is directly followed by a double quote. A behavior is if the first character after cmd /c (or cmd /k) is a quote character and there are more than two quote signs used in the complete command line, the leading character and the last quote character will be removed, preserving any text after the last quote character. This behavior causes the command line will be start like this:
    %windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log

    The line should therefore be like:

    %comspec% /c “”%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log””
    [/CODE]
    OR like,
    [CODE]
    %comspec% /c >”%userprofile%DesktopWANIP.log” “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” [/CODE]

    (I’ve edited the post you referred to, thanks)

    /Rems[CODE]
    %comspec% /c “”%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” >”%userprofile%DesktopWANIP.log””
    [/CODE]
    OR like,

    %comspec% /c >”%userprofile%DesktopWANIP.log” “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” [/CODE]

    (I’ve edited the post you referred to, thanks)

    /Rems[CODE]
    %comspec% /c >”%userprofile%DesktopWANIP.log” “%windir%system32cscript.exe” //NoLogo “%userprofile%DesktopWANIP.vbs” [/CODE]

    (I’ve edited the post you referred to, thanks)

    /Rems

    Avatar
    igor7
    Member
    #295078

    Re: Redirect script output to log file

    Rems,
    Thank you for excellent explanation!! Now it work perfect!!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.