title: Getting Script Name from wscript.exe Process
body: <p>I am using this code:</p>
  
  <pre><code>Dim name
  name = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%computername%")
  Set wmi = GetObject("winmgmts:" _
      &amp; "{impersonationLevel=impersonate}!\\" _ 
      &amp; name &amp; "\root\cimv2")
  For Each hwnd In wmi.InstancesOf("Win32_Process")
      If hwnd.Name = "wscript.exe" Then
          'get name and possibly location of currently running script
      End If
  Next
  </code></pre>
  
  <p>I am successfully listing all processes and picking out the <code>wscript.exe</code>. However, I have searched and have found no way to find the name of the script running in <code>wscript.exe</code>, ie. is it <code>myscript.vbs</code> or <code>jscript.js</code> or anything. Bonus if there is a way to find the whole path of the script.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>With more searching, I found a solution. In the above script, the <code>hwnd</code> variable stores the handle for the <code>wscript.exe</code> process. There is a property for handles: <code>hwnd.CommandLine</code>. It shows how to call it from the command line, so it would be something like:</p>
  
  <pre><code>"C:\Windows\System32\wscript.exe" "C:\path\to\script.vbs"
  </code></pre>
  
  <p>So I can parse the <code>hwnd.CommandLine</code> string to find the path and name of all running scripts.</p>