Solved

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

Posted on 2006-11-16
10
3,379 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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