Solved

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

Posted on 2006-11-16
10
3,488 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

688 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