Page 1 of 1

Determine packaged script run

Posted: Mon Mar 21, 2011 1:16 am
by clum09
Hello,

Is there any way to determine whether a script instance is launched when it is packaged?

I am writing a function to determine whether my vbscript is launched under a packaged script so that I can have another function determine the script's current folder.

I am thinking about using a WMI query to check for the process of the current script's executable, but I want to know if there is another better way to determine the script's execution under a packaged script.

Thank you in advance.


Determine packaged script run

Posted: Mon Mar 21, 2011 1:16 am
by clum09
Hello,

Is there any way to determine whether a script instance is launched when it is packaged?

I am writing a function to determine whether my vbscript is launched under a packaged script so that I can have another function determine the script's current folder.

I am thinking about using a WMI query to check for the process of the current script's executable, but I want to know if there is another better way to determine the script's execution under a packaged script.

Thank you in advance.


Determine packaged script run

Posted: Mon Mar 21, 2011 2:45 am
by clum09
Thank you jvierra for the response.

Maybe I can explain why I need to know whether the script instance is launched under a packaged script.

I want to write all scripts to be able to output the log files to the script's folder whether the script is packaged or not packaged when I point the extracted file location to point to another folder rather than the script's folder.

When the script is not packaged, the shell.CurrentDirectory will point to the script's folder. However, when the script is packaged, Wscript.Path is the current directory instead when the temp folder is used for the extracted files.

I think I can determine the packaged script process by using the WMI query.

Thanks again.




Determine packaged script run

Posted: Mon Mar 21, 2011 3:02 am
by jvierra
Here is an example of how to calculate a time dependent percentage. Note that we need to grab two succesive readings and use the timestamp to calculate teh actual percentage.

Code: Select all

	
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" _
    & strComputer & "rootcimv2")
	
While (True)
    Set object1 = objWMIService.Get("Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N1 = object1.PercentProcessorTime
    D1 = object1.TimeStamp_Sys100NS
    Wscript.Sleep(1000)
    set object2 = objWMIService.Get( _
    "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N2 = object2.PercentProcessorTime
    D2 = object2.TimeStamp_Sys100NS
	
    ' CounterType - PERF_100NSEC_TIMER_INV
    ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
    PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100
	
    Wscript.Echo "% Processor Time=" , PercentProcessorTime
	
Wend

This is the MSDN example for processor percentage. Process percentage works the same way.


Determine packaged script run

Posted: Mon Mar 21, 2011 7:23 am
by clum09
Jvierra,

I wrote a function to check the process that runs under the packaged script. Basically, it checks to see if the script's .exe file process is running or not. If the function finds the process running, it will return True. Otherwise, it will return False. Below is the code:

Private Function getProcess()Dim strComputer, strProcName, objWMIService, colProcesses strComputer = "." strProcName = Mid(WScript.ScriptFullName,_ InStrRev(WScript.ScriptFullName,"")+1) On Error Resume Next Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!" & _ strComputer & "rootcimv2") Set colProcesses = objWMIService.ExecQuery _ ("SELECT * FROM Win32_Process WHERE Name='" _ & strProcName & "'") If colProcesses.Count = 0 Then getProcess = False Else getProcess = True End If'Clean up variablesSet objWMIService = Nothing: Set colProcesses = NothingEnd Function

Thank you any way for your help.

Determine packaged script run

Posted: Mon Mar 21, 2011 7:55 am
by jvierra
Why does everybody do everything the hard way.

Do this ->

Code: Select all

	
If InStr(WScript.ScriptFullName,".exe") Then
	
    ' we are runing in a package
	
Else
	
   ' we are not running in a package
	
End If

It only takes one line of code to detect teh package execution environment - not a major world class program.

Yeesh! Heavens to murgatroid! We have to teach you kids everything.

Think of this like a game. Assume you are up against some 22nd century super-hacker out to destroy the world. You are humanities last hope but your batteries ( oh yeah! - i forget to mention - you are an android) are going to run out so you need to conserve power and thinking takes power and time.

The first question you must always ask is "What do I already know that tells me the answer to this question?"

If you approach every solution with "What kind of information can I GET to solve my problem?" - you are just telling yourself that you 'DON'T KNOW' something that you really already know or have in your bag.

Remember how we collect things in a game because they may be useful later. Same is true here.

May the 'Farts' be with you!
jvierra2011-03-21 15:56:41