Solved

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

Posted on 2006-11-16
10
3,396 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help to rewrite script 3 64
Recursive folder search vbscript 7 40
Add and format columns in vb6 7 46
Prevent user closing word document opened with VB6 6 67
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

911 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

28 Experts available now in Live!

Get 1:1 Help Now