?
Solved

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

Posted on 2006-11-16
10
Medium Priority
?
3,523 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
Independent Software Vendors: 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 750 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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