title: VBScript - Error 0x80041017 (null)
body: <p><strong>IMPORTANT UPDATE:</strong></p>
  
  <p><strong>As Cheran S stated below, it's a good idea to avoid using "taskmgr" for this script.  I'm not going to edit the code, as I feel it's best to maintain the original question as much as possible since doing so would partially invalidate &amp; obfuscate Cheran's answer &amp; comment.</strong></p>
  
  <p><strong>A good alternative to "taskmgr" would be "CharMap" (for simple &amp; fast testing).</strong></p>
  
  <hr />
  
  <p>Running Windows XP Professional (32-bit) and I've got this script that's throwing up this error:</p>
  
  <pre><code>Script:   C:\test.vbs
  Line:     40
  Char:     3
  Error:    0x80041017
  Code:     80041017
  Source:   (null)
  </code></pre>
  
  <p>Here's the code:</p>
  
  <pre><code>Set objWshShell = Wscript.CreateObject("Wscript.Shell")
  
  Set objWshNet = CreateObject("Wscript.Network")
  strComputer = objWshNet.ComputerName
  
  Set objWMIService = GetObject("winmgmts:" &amp; _
      "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
  
  Dim arrWinTitle(2)
      arrWinTitle(0) = "My Documents"
      arrWinTitle(1) = "Control Panel"
  
  Dim arrProcName(3)
      arrProcName(0) = "'taskmgr.exe'"
      arrProcName(1) = "'calc.exe'"
      arrProcName(2) = "'notepad.exe'"
  
  Sub srBlockWindow(strWinTitle, intWinTitle, strProcName, intProcName)
      i = 0
      Do While i = 0
          If objWshShell.AppActivate(strWinTitle(intWinTitle)) Then
              objWshShell.AppActivate(strWinTitle(intWinTitle))
              Wscript.Sleep 100
              objWshShell.SendKeys "%{F4}"
              Wscript.Sleep 100
          End If
  
          If intWinTitle = 0 Then
              intWinTitle = intWinTitle + 1
          Else
              intWinTitle = 0
          End If
  
          Wscript.Sleep 100
  
          Set colProcesses = objWMIService.ExecQuery _
              ("SELECT * FROM Win32_Process WHERE NAME = " &amp; strProcName(intProcName))
  
          For Each objProcess In colProcesses
              objProcess.Terminate()
          Next
  
          If intProcName &gt;= 0 Then
              intProcName = intProcName + 1
          ElseIf intProcName &gt;= 5 Then
              intProcName = 0
          End If
  
          Set colProcesses = Nothing
      Loop
  End Sub
  
  Call srBlockWindow(arrWinTitle, 0, arrProcName, 0)
  </code></pre>
  
  <p>I've reviewed <a href="http://www.winserverkb.com/Uwe/Forum.aspx/windows-server-scripting/11494/process-kill-script" rel="nofollow">this</a>, but I believe my script doesn't have any issues with the quotes.  For the sake of clarity, I'm getting the error at the start of the "For Each ..." string.</p>
  
  <p>What's peculiar is that it will run fine the first time, but once it loops, that's when I get the error.  So, it will close all the desired Windows/Applications, but once it goes through it's second iteration, I get the error.  I've inserted "On Error Resume Next", but that doesn't resolve it (I will add it later, since it's required to resolve the conflict when the Window/Process Starts simultaneously with Close/End/Stop attempts made by the Script).</p>
  
  <p><strong>I think it's because I should be conditionally checking if the process exists; problem is, I'm not quite sure how to do that with this code (I've never been good with Collections).  Anybody have suggestions on how to do it with this code specifically?</strong></p>
  
  <hr />
  
  <p>I reviewed <a href="http://www.techimo.com/forum/applications-operating-systems/117464-need-vbulletin-script-check-see-if-process-running.html" rel="nofollow">this</a> and tried to write a quick alternative script, but it didn't really work.  Here's the code:</p>
  
  <pre><code>Set service = GetObject("winmgmts:")
  
  Dim arrProcName(3)
      arrProcName(0) = "'taskmgr.exe'"
      arrProcName(1) = "'calc.exe'"
      arrProcName(2) = "'notepad.exe'"
  
  Sub srTest(strProc, intProc)
      i = 0
      Do While i = 0
          For Each Process In Service.InstancesOf("Win32_Process")
              If Process.Name = strProc(intProc) Then
                  Process.Name.Terminate
                  Process.Terminate
              End If
          Next
  
          If intProc = 0 Then
              intProc = intProc + 1
          ElseIf intProc &gt;= 3 Then
              intProc = 0
          End If
      Loop
  End Sub
  
  Call srTest(arrProcName, 0)
  </code></pre>
  
  <p>As you can see, I tried both "Process.Terminate" &amp; "Process.Name.Terminate", but neither yielded anything (not even an error).  I further tested it with "Wscript.Echo Process.Name" &amp; "Wscript.Echo strProc(intProc)", but neither of these worked too.</p>
  
  <p>Now that I've failed at this alternative solution, I'm feeling that I'm wildly stabbing in the dark for solutions, so I'll defer these esoteric challenges to the community that is vastly superior to me.</p>
  
  <hr />
  
  <p>There might be a few here who are reading this and wondering why I'm targeting My Documents, Control Panel, taskmgr.exe, calc.exe, &amp; notepad.exe.  Almost everybody reading this will probably be able to extrapolate on their own, but I'll make sure I'm clear on this for those who need it.  I'm doing this because it makes it easier to test, since all of these can be accessed simply by using the "Run" shortcut (Windows Key + R) &amp; then entering the following strings (one at a time, of course):</p>
  
  <ul>
  <li>My Documents</li>
  <li>Control</li>
  <li>taskmgr</li>
  <li>calc</li>
  <li>notepad</li>
  </ul>
  
  <p>You likely knew the keywords, but I just wanted to highlight <em>why</em> I'm using these specific ones (speed &amp; simplicity).</p>
  
  <hr />
  
  <p><strong>I'll remove this if Cheran adds the final code to the answer posted</strong></p>
  
  <p>Final Solution:</p>
  
  <pre><code>Set objWshShell = Wscript.CreateObject("Wscript.Shell")
  
  Set objWshNet = CreateObject("Wscript.Network")
  strComputer = objWshNet.ComputerName
  
  Set objWMIService = GetObject("winmgmts:" &amp; _
      "{impersonationLevel=impersonate}!\\" &amp; strComputer &amp; "\root\cimv2")
  
  Dim arrWinTitle
      arrWinTitle = Array("My Documents", "Control Panel")
  
  Dim arrProcName
      arrProcName = Array("'charmap.exe'", "'calc.exe'", "'notepad.exe'")
  
  Sub srBlockWindow(strWinTitle, intWinTitle, strProcName, intProcName)
      i = 0
      Do While i = 0
          On Error Resume Next
              ' In the Event of Conflict w/Initiation of Window or Process
          If objWshShell.AppActivate(strWinTitle(intWinTitle)) Then
              objWshShell.AppActivate(strWinTitle(intWinTitle))
              Wscript.Sleep 100
              objWshShell.SendKeys "%{F4}"
              Wscript.Sleep 100
          End If
  
          intWinTitle = (intWinTitle + 1) Mod (UBound(strWinTitle) + 1)
  
          Wscript.Sleep 100
  
          Set colProcesses = objWMIService.ExecQuery _
              ("SELECT * FROM Win32_Process WHERE NAME = " &amp; strProcName(intProcName))
  
          For Each objProcess In colProcesses
              objProcess.Terminate()
          Next
  
          intProcName = (intProcName + 1) Mod (UBound(strProcName) + 1)
  
          Set colProcesses = Nothing
      Loop
  End Sub
  
  Call srBlockWindow(arrWinTitle, 0, arrProcName, 0)
  </code></pre>
  
  <p>Here's a quick script I threw together to test it against:</p>
  
  <pre><code>Set objWshShell = Wscript.CreateObject("Wscript.Shell")
  
  i = 0
  Do While i = 0
      objWshShell.Run "explorer.exe /e, C:\Documents and Settings\User\My Documents"
      Wscript.Sleep 200
      objWshShell.Run "CharMap.exe"
      Wscript.Sleep 200
      objWshShell.Run "Control.exe"
      Wscript.Sleep 200
      objWshShell.Run "calc.exe"
      Wscript.Sleep 200
      objWshShell.Run "notepad.exe"
      Wscript.Sleep 200
  Loop
  </code></pre>
  
  <p><strong>BE CAREFUL!  Adjust the timings so that you can end "Wscript.exe" without too many problems.  Best to run both scripts simultaneously to see how it works.</strong></p>