LegroocH
Tue Dec 30 07:16:50 CST 2003
Thanks for answer!
That resolved my problem :)
Happy New Year!
U¿ytkownik "Roland Hall" <nobody@nowhere> napisa³ w wiadomo¶ci
news:uIsk4qszDHA.2412@TK2MSFTNGP10.phx.gbl...
> "LegroocH" wrote:
> : I have a code. When i run this command from command prompt, it works.
But
> : when i run it from a script, it doesn't works.
> : Where i made an error?
> :
> : Here is my code:
> :
> : Option Explicit
> : Const SYSTEM = "C:\Windows\System32\Portqry.exe"
> : Const TEMP = "C:\1.txt"
> : Const PORTNUMBER = 25
> : Const ForReading = 1
> :
> : Dim WshArguments, fso, WshShell
> : Dim plik
> : Dim dlugosc
> : Dim linia
> : Dim dziala
> :
> : Set fso = CreateObject("Scripting.FileSystemObject")
> : Set WshShell = CreateObject("WScript.Shell")
> :
> : 'If fso.FileExists(TEMP) Then
> : ' fso.DeleteFile (TEMP)
> : 'End if
> : WshShell.Run SYSTEM + " -n myserver -e " + CStr(PORTNUMBER) + " -l " +
> : "c:\1.txt", True
> : Wscript.Sleep 2000
> :
> : Set plik = fso.OpenTextFile (TEMP, ForReading)
> : While Not plik.AtEndOfStream
> : linia = plik.ReadLine
> : If Mid(linia, 1, 8) = "TCP port" Then
> : dlugosc = Len(linia)
> : If Mid(linia, dlugosc - 12, 13) = "NOT LISTENING" Then
> : dziala = False
> : Else
> : dziala = True
> : End if
> : End if
> : Wend
> : plik.close
> : 'If fso.FileExists(TEMP) Then
> : ' fso.DeleteFile (TEMP)
> : 'End if
> :
> : If dziala Then
> : MsgBox "Port " + CStr(PORTNUMBER) + " open"
> : Else
> : MsgBox "Port " + CStr(PORTNUMBER) + " close"
> : End if
>
> Hi LegroocH...
>
> The call you are making to portqry outputs a file. If you output the file
> the second time after c:\1.txt is created, it waits in a loop (sometimes)
> and portqry.exe never terminates. If you keep executing it, eventually
you
> have a lot of instances in memory. So, I added '-y' to the calling line
to
> overwrite the file if it exists.
>
> If you experienced that the file didn't exist, it was because of your
> wscript.sleep 2000 (2 second delay). If the file is not written within
this
> time frame a file does not exist will return as an error. I changed the
> delay to a loop looking for the file to exist:
>
> do while fso.FileExists(TEMP) = False
> wscript.sleep 100
> loop
>
> If you run this before the loop, you will get a return of -1 (true) when
it
> does exist and 0 (false) when it does not.
> wscript.echo fso.FileExists(TEMP)
>
> The loop will wait until it does so it does not attempt to read the file
> before it exists.
>
> I saw you had some commented code for removing the temp file. I believe
you
> commented these out because you experienced the same issues with the 2
> second delay. I put the 2nd one back in to remove the file so the -y
> parameter is no longer needed but not a bad idea to have in there anyway
in
> case the file does ever exist.
>
> I also put in some code to release memory used by the script and to
> terminate the script.
>
> Here is the modified code:
>
> Option Explicit
> Const SYSTEM = "C:\Windows\System32\Portqry.exe"
> Const TEMP = "C:\1.txt"
> Const PORTNUMBER = 25
> Const ForReading = 1
>
> Dim WshArguments, fso, WshShell
> Dim plik
> Dim dlugosc
> Dim linia
> Dim dziala
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set WshShell = CreateObject("WScript.Shell")
>
> WshShell.Run SYSTEM + " -n 192.168.0.9 -e " + CStr(PORTNUMBER) + " -l " +
> "c:\1.txt -y", True
> do while fso.FileExists(TEMP) = False
> wscript.sleep 100
> loop
>
> Set plik = fso.OpenTextFile (TEMP, ForReading)
> While Not plik.AtEndOfStream
> linia = plik.ReadLine
> If Mid(linia, 1, 8) = "TCP port" Then
> dlugosc = Len(linia)
> If Mid(linia, dlugosc - 12, 13) = "NOT LISTENING" Then
> dziala = False
> Else
> dziala = True
> End if
> End if
> Wend
> plik.close
> If fso.FileExists(TEMP) Then
> fso.DeleteFile (TEMP)
> End if
>
> If dziala Then
> wscript.echo "Port " + CStr(PORTNUMBER) + " open"
> Else
> wscript.echo "Port " + CStr(PORTNUMBER) + " close"
> End if
>
> ' Release memory
> set fso = nothing
> set wshShell = nothing
> set plik = nothing
> wscript.quit(0)
>
> --
> Roland
>
> This information is distributed in the hope that it will be useful, but
> without any warranty; without even the implied warranty of merchantability
> or fitness for a particular purpose.
>
> -Technet Script Center-
>
http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/default.asp
> -MSDN Library-
>
http://msdn.microsoft.com/library/default.asp
>
>