Solved

Find PID of the current VB-Script (wscript.exe)

Posted on 2006-11-16
10
3,416 Views
Last Modified: 2012-05-05
Hello Experts,
I have an application that runs several VB-Scripts at the same time.
It may even start the same Script more than one times at the same time.
In the taskmanager, I can find the corresponding wscript.exes with their corresponding PIDs o.k.
But in the logfile, that is written by the scripts, I cannot distinguish which line was written by which instance of the scripts.
To solve that, I want to write the PID into each line of the logfile.
But I have not found a way of determining the PID from within the VB-Script.
Does anybody have an answer?
Thanks in advance!
0
Comment
Question by:Arthur_2000
  • 6
  • 4
10 Comments
 
LVL 10

Expert Comment

by:Clif
ID: 17956612
How are you starting your scripts?
0
 

Author Comment

by:Arthur_2000
ID: 17957102
I start them from an application that runs as a Windows-service.
The application is written in VB.net
Here is the part of the sourcecode that actually starts the application

                    objProcess = New Process

                    objProcess.StartInfo.UseShellExecute = True
                    objProcess.StartInfo.CreateNoWindow = True
                    objProcess.StartInfo.RedirectStandardError = False
                    objProcess.StartInfo.FileName() = Manager.ScriptsPath & Scripts(curScript)
                    objProcess.StartInfo.Arguments() = Arguments(curScript)
                    objProcess.StartInfo.UseShellExecute = True
                    objProcess.StartInfo.WorkingDirectory = Manager.ScriptsPath

                    objProcess.Start()
                    PID = objProcess.Id

Arthur
0
 
LVL 10

Expert Comment

by:Clif
ID: 17957282
objProcess.Id doesn't return the pid?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Arthur_2000
ID: 17957571
Yes, it does,
the application that calls the scripts knows the PID.
But the script itself does not know its own PID and so I cannot distinguish which instance of the same script has written which line into the logfile. (The logfiles are written from the script, not from the application that launches the scripts)

Arthur
0
 
LVL 10

Expert Comment

by:Clif
ID: 17957669
Oh, I see.

How about this:

Private Declare Function GetCurrentProcessId Lib "kernel32.dll" () As Long
Private Sub Form_Load()
    MsgBox "This program's current Process ID is " & GetCurrentProcessId
End Sub
0
 
LVL 10

Expert Comment

by:Clif
ID: 17957680
Oops, sorry, a script wouldn't have a form.

But the basics should still work.
0
 
LVL 10

Expert Comment

by:Clif
ID: 17958526
Ok, that's not going to work, since scripts won't let you access the API.

Sorry about that.

I do have a rather low tech idea, though.  If both the scripts and the service are running on the same machine (or have access to a mutual drive), you might have the service create a text file that contains the PID (which it gets from PID = objProcess.Id).  Then the script can read it.
0
 

Author Comment

by:Arthur_2000
ID: 17960173
I was thinking about a solution like this before,
but if we have e.g. a script named myscript.vbs that is launched two times in parallell (we have two wscript.exe in the taskmanager with different PIDs), how should they know which textfile belongs to which instance of the script?
Arthur
0
 
LVL 10

Accepted Solution

by:
Clif earned 250 total points
ID: 17960476
Well, you could pass a unique value to the script and then add that value to the text file.

Something like this:
                    Dim sTimer As String = Microsoft.VisualBasic.DateAndTime.Timer.ToString   '<-- Added Line
                    objProcess = New Process

                    objProcess.StartInfo.UseShellExecute = True
                    objProcess.StartInfo.CreateNoWindow = True
                    objProcess.StartInfo.RedirectStandardError = False
                    curScript &= sTimer                                                                                  '<-- Added Line
                    objProcess.StartInfo.FileName() = Manager.ScriptsPath & Scripts(curScript)
                    objProcess.StartInfo.Arguments() = Arguments(curScript)
                    objProcess.StartInfo.UseShellExecute = True
                    objProcess.StartInfo.WorkingDirectory = Manager.ScriptsPath

                    objProcess.Start()
                    PID = objProcess.Id

                    Dim sPIDFileName As String = "PID" & sTimer & ".txt"                                                '<-- Added Line
                    '
                    'Add code to write text file

Then, in the vbs file:

                    Dim sTimer = Wscript.Arguments.Item(0)
                    Dim sPIDFileName = "PID" & sTimer & ".txt"
                    '
                    'Add code to read the file

This is going the long way around, but it's about the only way I can think of.
0
 

Author Comment

by:Arthur_2000
ID: 17960682
Hey,
this is a good trick,
I think I can do it the way you proposed.
Thank you very much for your help, I appreciate it very much!
Best wishes, Arthur
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 connector to mongodb 2 83
Add and format columns in vb6 7 57
VB Script to add site to Java Exception List 4 76
Problem to With line 4 57
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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