Scripts Currently Running

Change Page: 12 > | Showing page 1 of 2, messages 1 to 20 of 21
Author Message
dm_4ever

  • Total Posts : 3687
  • Scores: 82
  • Reward points : 0
  • Joined: 6/29/2006
  • Location: Orange County, California
  • Status: offline
Scripts Currently Running Thursday, January 11, 2007 6:50 PM (permalink)
4
Seems there are some admins that have scripts running in the background monitoring or gathering information they need or want.  I've seen some posts where people want to know what scripts are running since you really can't tell by looking in task manager; all you see is wscript.exe or cscript.exe and maybe the PID.  So I was at the end of running a fever and wanted to do something to take my mind off feeling ill and so I put this HTA together.  I apologize if someone has already posted something like this.  Also, forgive me if I didn't comment it well or didn't format it in the manner you are accustomed to (I have my preferred method of structuring my scripts that makes it easy for me to read). 

Save the code below into a HTA file (i.e. "Get Running Scripts.hta") It uses WMI so you need to have Admin rights on the machine you run it on or query.
I've only tested the HTA on an XP & 2003 Server box. ConvertDT Function in the example below only work on XP or higher. Look at the end for a replacement function that should work on Win2k.

NOTE: The commandline property of the Win32_Process class was introduced with WinXP/Win2k3 so you can only see the name of the script running on those boxes.  Also, the commandline property will return NULL if the HTA is launched using the RUNAS command (thanks to kirrilian for discovering this bug).

There are three version of this HTA. You can find two below and one submitted by NssB further down below.

Original Version:
 <html>
 <head>
 <script language="vbscript">
 Option Explicit 
 
 Dim intRefreshRt : intRefreshRt = 30000 '30 Second Refresh Rate in Milliseconds
 
 Sub Window_OnLoad
 '         On Error Resume Next
   
   Dim iTimerID
   
   window.document.title = "Scripts currently running on: " & GetLocCompName 'set title with local computername by calling function
   window.document.getElementById("PCName").InnerHTML = GetLocCompName 'set computername in header <h1> tag
   GetScripts
   window.document.getElementById("strComputerName").focus 'focus to the inputbox to specify another pc
   iTimerID = window.setInterval("GetScripts", intRefreshRt)
 End Sub
   
 Sub GetScripts
   'On Error Resume Next
   
   Dim wmiQuery, colItems, objItem, strCmdLine, strOutput, intPID, strComputer
   
   strComputer = UCase(window.document.getElementById("strComputerName").Value) 'get computername from input box
   If strComputer = "" Then
       strComputer = GetLocCompName 'if input box is empty, use local computer name by calling a function
   End If
   
   If Ping(strComputer) = False Then 'test connectivity with ping function
       alert "Computer specified is unreachable!!"
       window.document.getElementById("strComputerName").Value = "" 'reset input box if the machine is unreachable
       Exit Sub
   End If
   
   window.document.title = "Scripts currently running on: " & strComputer 'set title to new computer name if different from local
   window.document.getElementById("PCName").innerHTML = strComputer 'set computer name in header if different from local
   window.document.getElementById("TimeStamp").innerHTML = Now()
   
   wmiQuery = "Select * From Win32_Process Where (Name='wscript.exe' Or Name='cscript.exe')"  'define WMI query
   ' begin building table
   strOutput = "<table border=""0"" width=""100%"" id=""table1""><tr>" & _
       "<td><b>Script Name:</b></td><td><b>Process ID:</b></td>" & _
       "<td><b>Running Since:</b></td><td><b>Action</b></td></tr>"
   
   Set colItems = objWMI(strComputer, wmiQuery) 'retrieve WMI collection by calling the objWMI function
   
   For Each objItem In colItems 'loop through the collection
       strCmdLine = Replace(Replace(objItem.CommandLine, Chr(34), ""), "cscript.exe ", "")
       strCmdLine = Trim(strCmdLine) 'format string to pull out the script name being run
       intPID = objItem.ProcessID
       strOutput = strOutput & "<tr><td title='" & objItem.CommandLine & "'>" & Right(strCmdLine, Len(strCmdLine) - InStrRev(strCmdLine, "\")) & _
       "</td><td>" & intPID & "</td><td>" & ConvertDT(objItem.CreationDate) & "</td>" & _
       "<td><input type=""button"" value=""Terminate"" onclick=""KillScript('" & intPID & "')""></td></tr>"
   Next
   window.document.getElementById("RScripts").innerHTML = strOutput & "</table>" 'display data
   Set colItems = Nothing
 End Sub
   
 Sub KillScript(intPID)
   'On Error Resume Next
   
   Dim wmiQuery, colItems, objItem, strResponse, strComputer
   
   strResponse = MsgBox("Are you sure you want to terminate the script with Process ID of: " & intPID & "?", 36, "Confirm Script Termination")
   If strResponse = vbNo Then Exit Sub
   
   strComputer = UCase(window.document.getElementById("strComputerName").Value)
   If strComputer = "" Then
       strComputer = GetLocCompName
   End If
   
   wmiQuery = "Select * From Win32_Process Where ProcessID = " & intPID 'define WMI query
   
   Set colItems = objWMI(strComputer, wmiQuery)'retrieve WMI collection by calling the objWMI function
   
   For Each objItem In colItems
       objItem.Terminate 'terminate process with PID specified
   Next
   Set colItems = Nothing
   GetScripts 'refresh the process list displayed
 End Sub
   
 Function GetLocCompName
 '         On Error Resume Next
   
   Dim objNetwork
   
   Set objNetwork = CreateObject("WScript.Network")
   GetLocCompName = UCase(objNetwork.ComputerName) 'get local computer name
   Set objNetwork = Nothing
 End Function
   
 Function Ping(strRmtPC)
 '         On Error Resume Next
   
   Dim wmiQuery, objPing, objStatus, blnStatus
   
   wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strRmtPC & "'"
   
   Set objPing = objWMI(".", wmiQuery)
   
   For Each objStatus in objPing
       If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
           blnStatus = False 'Not Reachable
       Else
           blnStatus = True 'Reachable
       End If
   Next
   
   Ping = blnStatus
   Set objPing = Nothing
 End Function
   
 Function objWMI(strComputer, strWQL)
 '         On Error Resume Next
   
   Dim wmiNS, objWMIService
   
   wmiNS = "\root\cimv2"
   
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS) 'connect to WMI
   Set objWMI = objWMIService.ExecQuery(strWQL) 'execute query
   Set objWMIService = Nothing
 End Function
   
 Function ConvertDT(strDT)
 '         On Error Resume Next
   
   Dim objTime
   
   Set objTime = CreateObject("WbemScripting.SWbemDateTime")
   objTime.Value = strDT
   ConvertDT = objTime.GetVarDate 'convert UTC to Standard Time
   Set objTime = Nothing
 End Function
 </script>
 <hta:application
 applicationname="Get Running Scripts"    
 border="dialog"
 borderstyle="normal"
 caption="Get Running Scripts"
 contextmenu="yes"
 icon="images\icon.ico"
 maximizebutton="yes"
 minimizebutton="yes"
 navigable="yes"
 scroll="no"
 selection="yes"
 showintaskbar="yes"
 singleinstance="yes"
 sysmenu="yes"
 version="1.0"
 windowstate="normal"
 >
 <style type="text/css">
 td {
 font-family: "Times New Roman", Times, serif;
 font-size: 18px;
 font-style: normal;
 font-weight: normal;
 font-variant: normal;
 color: #FFFFFF;
 vertical-align: top;
 }
 </style>
 </head>
 <body bgcolor="#272936" style="overflow:auto;color:#FFFFFF;">
 <div align="center">
 <h1>VBScripts Currently Running on: <span id="PCName"></span></h1>
 Connect To: <input type="text" id="strComputerName" onKeyPress="if window.event.keycode = 13 then GetScripts">&nbsp;&nbsp;<input type="button" value="Connect" onclick="GetScripts">
 <br /><br />
 Last Updated: <span id="TimeStamp"></span>
 </div>
 <br />
 <span id="RScripts"></span>
 </body>
 </html>
 


Version that accepts alternate credentials (thanks to NssB's contribution):
NOTE: You can NOT provide alternate credentials for the local machine. Queries will be executed using your credentials (impersonate) if no username/password is provided.

Updated 2/4/2007
 <html>
 <head>
 <script language="vbscript">
 Option Explicit 
 Dim intRefreshRt : intRefreshRt = 30000 '30 Second Refresh Rate in Milliseconds
 Sub Window_OnLoad
 '  On Error Resume Next
   
 Dim iTimerID
 
 window.document.title = "Scripts currently running on: " & GetLocCompName 'set title with local computername by calling function
 window.document.getElementById("PCName").InnerHTML = GetLocCompName 'set computername in header <h1> tag
 GetScripts
 window.document.getElementById("strComputerName").focus 'focus to the inputbox to specify another pc
 iTimerID = window.setInterval("GetScripts", intRefreshRt)
 End Sub
   
 Sub GetScripts
 '  On Error Resume Next
   
 Dim wmiQuery, colItems, objItem, strCmdLine, strOutput, intPID, strComputer
 
 strComputer = UCase(window.document.getElementById("strComputerName").Value) 'get computername from input box
 If strComputer = "" Or strComputer = GetLocCompName Or strComputer = "LOCALHOST" Or strComputer = "127.0.0.1" Or strComputer = "." Then
  strComputer = GetLocCompName 'if input box is empty, use local computer name by calling a Function
  window.document.getElementById("strComputerName").Value = strComputer
  window.document.getElementById("PCName").innerHTML = strComputer & " (Local)"
 Else
  window.document.getElementById("PCName").innerHTML = strComputer 'set computer name in header if different from local
 End If
 
 If Ping(strComputer) = False Then 'test connectivity with ping Function
  alert "Computer specified is unreachable!!"
  window.document.getElementById("strComputerName").Value = "" 'reset input box if the machine is unreachable
  Exit Sub
 End If 
   
 window.document.title = "Scripts currently running on: " & strComputer 'set title to new computer name if different from local
 window.document.getElementById("TimeStamp").innerHTML = Now()
 wmiQuery = "Select * From Win32_Process Where (Name='wscript.exe' Or Name='cscript.exe' Or Name='mshta.exe')"  'define WMI query
 ' begin building table
 strOutput = "<table border=""0"" width=""100%"" id=""table1""><tr>" & _
 "<td><b>Script Name:</b></td><td><b>Process ID:</b></td>" & _
 "<td><b>Running Since:</b></td><td><b>Action</b></td></tr>"
 
 On Error Resume Next
 Set colItems = objWMI(strComputer, wmiQuery) 'retrieve WMI collection by calling the objWMI function
 If IsEmpty(colItems) Then
  window.document.getElementById("RScripts").innerHTML = ""
  Exit Sub
 End If
 On Error GoTo 0
 
 For Each objItem In colItems 'loop through the collection
  strCmdLine = Replace(Replace(objItem.CommandLine, Chr(34), ""), "cscript.exe ", "")
  strCmdLine = Trim(strCmdLine) 'format string to pull out the script name being run
  intPID = objItem.ProcessID
  strOutput = strOutput & "<tr><td title='" & objItem.CommandLine & "'>" & Right(strCmdLine, Len(strCmdLine) - InStrRev(strCmdLine, "\")) & _
  "</td><td>" & intPID & "</td><td>" & ConvertDT(objItem.CreationDate) & "</td>" & _
  "<td><input type=""button"" value=""Terminate"" onclick=""KillScript('" & intPID & "')""></td></tr>"
 Next
 window.document.getElementById("RScripts").innerHTML = strOutput & "</table>" 'display data
 Set colItems = Nothing
 End Sub
   
 Sub KillScript(intPID)
 '  On Error Resume Next
 
 Dim wmiQuery, colItems, objItem, strResponse, strComputer
 
 strResponse = MsgBox("Are you sure you want to terminate the script with Process ID of: " & intPID & "?", 36, "Confirm Script Termination")
 If strResponse = vbNo Then Exit Sub
 
 strComputer = UCase(window.document.getElementById("strComputerName").Value)
  If strComputer = "" Or strComputer = GetLocCompName Or strComputer = "LOCALHOST" Or strComputer = "127.0.0.1" Or strComputer = "." Then
  strComputer = GetLocCompName 'if input box is empty, use local computer name by calling a function
 End If
 
 wmiQuery = "Select * From Win32_Process Where ProcessID = " & intPID 'define WMI query
 
 Set colItems = objWMI(strComputer, wmiQuery)'retrieve WMI collection by calling the objWMI function
 
 For Each objItem In colItems
  objItem.Terminate 'terminate process with PID specified
 Next
 Set colItems = Nothing
 GetScripts 'refresh the process list displayed
 End Sub
   
 Function GetLocCompName
 '  On Error Resume Next
   
 Dim objNetwork
 
 Set objNetwork = CreateObject("WScript.Network")
 GetLocCompName = UCase(objNetwork.ComputerName) 'get local computer name
 Set objNetwork = Nothing
 End Function
   
 Function Ping(strRmtPC)
 '  On Error Resume Next
   
   Dim wmiQuery, objPing, objStatus, blnStatus
   
   wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strRmtPC & "'"
   Set objPing = objWMI(".", wmiQuery)
   
   For Each objStatus in objPing
       If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
           blnStatus = False 'Not Reachable
       Else
           blnStatus = True 'Reachable
       End If
   Next
   Ping = blnStatus
   Set objPing = Nothing
 End Function
   
 Function objWMI(strComputer, strWQL)
 '  On Error Resume Next
 Dim wmiNS, objWMIService, objSWbemLocator, objSWbemServices
 Dim strUID, strPwd
 
 wmiNS = "\root\cimv2"
 strUID = window.document.getElementById("strUserID").value
 strPwd = window.document.getElementById("strPass").value
 
 If strComputer = "." Or strComputer = GetLocCompName Then
  strUID = ""
  strPwd = ""
 End If
 
 Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
 On Error Resume Next
 Set objSWbemServices = objSWbemLocator.ConnectServer _
 (strComputer, wmiNS, strUID, strPwd)
  Select Case Err.Number
   Case -2147024891
    window.document.getElementById("accessdenied").innerHTML = "Access Denied! Please check the credentials supplied."
    Exit Function
  End Select
 On Error GoTo 0
 window.document.getElementById("accessdenied").innerHTML = ""
 Set objWMI = objSWbemServices.ExecQuery(strWQL)
 Set objSWbemServices = Nothing 
 Set objSWbemLocator = Nothing
 End Function
   
 Function ConvertDT(strDT)
 '  On Error Resume Next
 
 Dim objTime: Set objTime = CreateObject("WbemScripting.SWbemDateTime")
 objTime.Value = strDT
 ConvertDT = objTime.GetVarDate 'convert UTC to Standard Time
 Set objTime = Nothing
 End Function
 </script>
 <hta:application
 applicationname="Get Running Scripts"    
 border="dialog"
 borderstyle="normal"
 caption="Get Running Scripts"
 contextmenu="yes"
 icon="images\icon.ico"
 maximizebutton="yes"
 minimizebutton="yes"
 navigable="yes"
 scroll="no"
 selection="yes"
 showintaskbar="yes"
 singleinstance="yes"
 sysmenu="yes"
 version="1.0"
 windowstate="normal"
 >
 <style type="text/css">
 td {
 font-family: "Times New Roman", Times, serif;
 font-size: 18px;
 font-style: normal;
 font-weight: normal;
 font-variant: normal;
 color: #FFFFFF;
 vertical-align: top;
 }
 .access {
 color:#ffffff;
 font-size:20px;
 font-family:"Times New Roman", Times, serif;
 }
 </style>
 </head>
 <body bgcolor="#272936" style="overflow:auto;color:#FFFFFF;">
 <div align="left">
 <h1>VBScripts Currently Running on: <span id="PCName"></span></h1>
 Connect To: <input type="text" id="strComputerName" onKeyPress="if window.event.keycode = 13 then GetScripts">&nbsp;&nbsp;
 UserName: <input type="text" id="strUserID" value="">&nbsp;&nbsp; Password: <input type="password" id="strPass" value="">&nbsp;&nbsp; <input type="submit" value="Connect" onclick="GetScripts">
 <br /><br />
 Last Updated: <span id="TimeStamp"></span>
 </div>
 <div align="center" class="access"><span id="accessdenied"></span></div>
 <br />
 <span id="RScripts"></span>
 </body>
 </html>


UPDATED: 1/12/2007
Date/Time Conversition Function for Win2k, just replace the ConverDT function in the example above with this one:
 Function ConvertDT(strDT)
   ConvertDT = _
       CDate(Mid(strDT, 5, 2) &_
       "/" &_
       Mid(strDT, 7, 2) &_
       "/" &_
       Left(strDT, 4) &_
       " " &_
       Mid (strDT, 9, 2) &_
       ":" &_
       Mid(strDT, 11, 2) &_
       ":" &_
       Mid(strDT, 13, 2))
 End Function
 

<message edited by dm_4ever on Thursday, February 08, 2007 10:04 AM>
dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
 
#1
    TNO

    • Total Posts : 2094
    • Scores: 36
    • Reward points : 0
    • Joined: 12/18/2004
    • Location: Earth
    • Status: offline
    RE: Scripts Currently Running Thursday, January 11, 2007 9:17 PM (permalink)
    0
    Holy Running Scripts BatMan
    To iterate is human, to recurse divine. -- L. Peter Deutsch
     
    #2
      Country73

      • Total Posts : 754
      • Scores: 10
      • Reward points : 0
      • Status: offline
      RE: Scripts Currently Running Friday, January 12, 2007 4:28 AM (permalink)
      0
      Pretty slick; I believe I will be able to put this to some good use!
       
      #3
        mrmore

        • Total Posts : 5
        • Scores: 0
        • Reward points : 0
        • Joined: 11/9/2006
        • Status: offline
        RE: Scripts Currently Running Monday, January 15, 2007 9:51 AM (permalink)
        0
        Nice one. Thank you
         
        #4
          DiGiTAL.SkReAM

          • Total Posts : 1259
          • Scores: 7
          • Reward points : 0
          • Joined: 9/7/2005
          • Location: Clearwater, FL, USA
          • Status: offline
          RE: Scripts Currently Running Tuesday, January 16, 2007 1:28 AM (permalink)
          0
          Very nice.  Unfortunately, we have about 600+ NT 4.0 servers that we have to support, so I can't use this right now.  *sounds of cursing*
           
          "Would you like to touch my monkey?" - Dieter (Mike Meyers)

          "It is better to die like a tiger, than to live like a pussy."
          -Master Wong, from Balls of Fury
           
          #5
            kirrilian

            • Total Posts : 629
            • Scores: 3
            • Reward points : 0
            • Joined: 3/15/2005
            • Location:
            • Status: offline
            RE: Scripts Currently Running Friday, January 19, 2007 4:52 AM (permalink)
            0

            ORIGINAL: DiGiTAL.SkReAM

            Very nice.  Unfortunately, we have about 600+ NT 4.0 servers that we have to support, so I can't use this right now.  *sounds of cursing*



            no wonder youre a little "off" somtimes LOL
            Have you searched [url="http://www.google.com"]here [/url]?
            [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
            [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
             
            #6
              DiGiTAL.SkReAM

              • Total Posts : 1259
              • Scores: 7
              • Reward points : 0
              • Joined: 9/7/2005
              • Location: Clearwater, FL, USA
              • Status: offline
              RE: Scripts Currently Running Friday, January 19, 2007 9:02 AM (permalink)
              0
              Yeah, well..... I have a bigger smiley avatar than you do!  So there!
              "Would you like to touch my monkey?" - Dieter (Mike Meyers)

              "It is better to die like a tiger, than to live like a pussy."
              -Master Wong, from Balls of Fury
               
              #7
                dm_4ever

                • Total Posts : 3687
                • Scores: 82
                • Reward points : 0
                • Joined: 6/29/2006
                • Location: Orange County, California
                • Status: offline
                RE: Scripts Currently Running Friday, January 19, 2007 2:02 PM (permalink)
                0
                Just wanted to say thank you for the compliments and laughs.  You guys are great



                ORIGINAL: kirrilian
                no wonder youre a little "off" somtimes LOL


                and



                ORIGINAL: DiGiTAL.SkReAM
                Yeah, well..... I have a bigger smiley avatar than you do!  So there!



                dm_4ever

                My philosophy: K.I.S.S - Keep It Simple Stupid
                Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
                Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
                 
                #8
                  kirrilian

                  • Total Posts : 629
                  • Scores: 3
                  • Reward points : 0
                  • Joined: 3/15/2005
                  • Location:
                  • Status: offline
                  RE: Scripts Currently Running Friday, January 19, 2007 6:06 PM (permalink)
                  0
                  DOH!!

                  <--- pwned!!

                  Just for that, here is a dancing pink elephant and an angry off topic "smiley"



                  (i know its a cop out...)

                  <message edited by kirrilian on Friday, January 19, 2007 6:11 PM>
                  Have you searched [url="http://www.google.com"]here [/url]?
                  [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
                  [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
                   
                  #9
                    kirrilian

                    • Total Posts : 629
                    • Scores: 3
                    • Reward points : 0
                    • Joined: 3/15/2005
                    • Location:
                    • Status: offline
                    RE: Scripts Currently Running Friday, January 19, 2007 6:11 PM (permalink)
                    0
                    btw, dm_4ever, back on topic, when i ran your hta as another user (i do my everyday work as a normal unprivileged user, so i had to run it under my admin acct) i got an error when i tried to run against the local machine. i dont think it was returning the name of the script that was running when i put in a 'on error resume next' statement. i didnt dig in too deeply as to why, i will do that on monday when i get back to work

                    excellent hta otherwise
                    Have you searched [url="http://www.google.com"]here [/url]?
                    [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
                    [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
                     
                    #10
                      dm_4ever

                      • Total Posts : 3687
                      • Scores: 82
                      • Reward points : 0
                      • Joined: 6/29/2006
                      • Location: Orange County, California
                      • Status: offline
                      RE: Scripts Currently Running Sunday, January 21, 2007 5:04 AM (permalink)
                      0
                      Definitely let me know what error you're getting so I can take a look. I tried it in a Virtual Machine (WinXP) with an unprivileged user and it seemed to work.  
                      dm_4ever

                      My philosophy: K.I.S.S - Keep It Simple Stupid
                      Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
                      Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
                       
                      #11
                        kirrilian

                        • Total Posts : 629
                        • Scores: 3
                        • Reward points : 0
                        • Joined: 3/15/2005
                        • Location:
                        • Status: offline
                        RE: Scripts Currently Running Monday, January 22, 2007 2:59 AM (permalink)
                        0
                        i think you misunderstood the user part of it, both of my users are local admins on my workstation, but in order to have admin access on the servers i have to use runas. so i was running your script as my superuser and when it tried to pull the data from the local machine it failed on the script name

                        this is the process i followed:
                        use runas to start cmd as my superuser account
                        start the hta via the terminal
                        as soon as it opens i get this error:
                        Line: 48
                        Error: Invalid use of Null: 'Replace'
                        I uncommented your on error resume next and it works, but it doesnt print out the script name.
                        Have you searched [url="http://www.google.com"]here [/url]?
                        [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
                        [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
                         
                        #12
                          DiGiTAL.SkReAM

                          • Total Posts : 1259
                          • Scores: 7
                          • Reward points : 0
                          • Joined: 9/7/2005
                          • Location: Clearwater, FL, USA
                          • Status: offline
                          RE: Scripts Currently Running Monday, January 22, 2007 5:42 AM (permalink)
                          0
                          As an aside, to view which scripts were running on our NT4.0 boxen, I modified the batch file that calls the script (we use AT to schedule our jobs, and have AT run a cmd file that then runs the script, rather than having it call the .vbs directly.)
                          ScriptRunner.cmd
                           @echo off
                           set OwnName=%1
                           if not exist %windir%\system32\ws_%OwnName%.exe copy %windir%\system32\wscript.exe %windir%\system32\ws_%OwnName%.exe
                           if exist c:\batch\daily\%OwnName%.vbs start %windir%\system32\ws_%OwnName%.exe c:\batch\daily\%OwnName%.vbs
                           exit
                           

                           
                          And then once a night, we have another cmd file that ckicks off that does nothing but delete the extra copies of wscript.exe that we have laying around.
                          It is kind of a kludge, but... this way we can just pull a list of processes and see what ws_<scriptname>.exe's are running.
                           
                          "Would you like to touch my monkey?" - Dieter (Mike Meyers)

                          "It is better to die like a tiger, than to live like a pussy."
                          -Master Wong, from Balls of Fury
                           
                          #13
                            dm_4ever

                            • Total Posts : 3687
                            • Scores: 82
                            • Reward points : 0
                            • Joined: 6/29/2006
                            • Location: Orange County, California
                            • Status: offline
                            RE: Scripts Currently Running Monday, January 22, 2007 2:48 PM (permalink)
                            0
                            Thank you for that information kirrilian.  I followed your steps and was able to duplicate the error.  It appears that when querying the Win32_Process class after using the runas cmd, the commandline property appears as null.  I'm not too sure how to fix this or get around it at this time, but I'll take a look...perhaps find an alternate method to get the info.
                            <message edited by dm_4ever on Monday, January 22, 2007 2:51 PM>
                            dm_4ever

                            My philosophy: K.I.S.S - Keep It Simple Stupid
                            Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
                            Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
                             
                            #14
                              kirrilian

                              • Total Posts : 629
                              • Scores: 3
                              • Reward points : 0
                              • Joined: 3/15/2005
                              • Location:
                              • Status: offline
                              RE: Scripts Currently Running Saturday, January 27, 2007 6:26 PM (permalink)
                              0

                              ORIGINAL: dm_4ever

                              Thank you for that information kirrilian. I followed your steps and was able to duplicate the error. It appears that when querying the Win32_Process class after using the runas cmd, the commandline property appears as null. I'm not too sure how to fix this or get around it at this time, but I'll take a look...perhaps find an alternate method to get the info.


                              ive thought about it since, it may be related to the fact that runas dumps most of your environment somewhat like cron does in linux, it runs in its own space. i havent tried the runas switches that allow runas to replicate the current environment, but that may be an option.
                              Have you searched [url="http://www.google.com"]here [/url]?
                              [url="http://tinyurl.com/as7xm"]VBScript Fundamentals[/url]
                              [url="http://kirrilian.dyndns.org/projects/code/"]My Site[/url]
                               
                              #15
                                NssB

                                • Total Posts : 31
                                • Scores: 0
                                • Reward points : 0
                                • Joined: 6/26/2006
                                • Status: offline
                                RE: Scripts Currently Running Tuesday, January 30, 2007 4:30 AM (permalink)
                                0
                                Hi,


                                Made a few ammendments to this code. See objWMI function and GetScripts sub. Also a little HTML modded.
                                Using the SWbem locator/services to connect to a WMI namespace with credentials provided where a remote host is specified.
                                Normal mgmt: used where host is local. CommandLine method still proving elusive, but perhaps a little easier to manage alternate credentials :)
                                Oh, and since I like HTA's, I added the MSHTA host controller. My bad
                                Please do comment.

                                <html>
                                 <head>
                                 <script language="vbscript">
                                 Option Explicit 
                                 
                                 Dim intRefreshRt : intRefreshRt = 30000 '30 Second Refresh Rate in Milliseconds
                                 
                                 Sub Window_OnLoad
                                 '         On Error Resume Next
                                     
                                     Dim iTimerID
                                     
                                     window.document.title = "Scripts currently running on: " & GetLocCompName 'set title with local computername by calling function
                                     window.document.getElementById("PCName").InnerHTML = GetLocCompName 'set computername in header <h1> tag
                                     GetScripts
                                     window.document.getElementById("strComputerName").focus 'focus to the inputbox to specify another pc
                                     iTimerID = window.setInterval("GetScripts", intRefreshRt)
                                 End Sub
                                     
                                 Sub GetScripts
                                     'On Error Resume Next
                                     
                                     Dim wmiQuery, colItems, objItem, strCmdLine, strOutput, intPID, strComputer, intStat
                                     intStat = 0     
                                 
                                     strComputer = UCase(window.document.getElementById("strComputerName").Value) 'get computername from input box
                                 
                                     If strComputer = "" Then
                                         strComputer = "LOCALHOST" 'if input box is empty, use local computer name by calling a function
                                     End If
                                     If strComputer = "LOCALHOST" or strComputer = "127.0.0.1" then
                                         intStat = 1
                                     End If
                                     If intStat = 0 then
                                     If strUserID.value = "" OR strPass.Value = "" then
                                         MsgBox "You must enter a username AND password for remote hosts"
                                             Exit Sub
                                         End If
                                     End If
                                     If Ping(strComputer) = False Then 'test connectivity with ping function
                                         alert "Computer specified is unreachable!!"
                                         window.document.getElementById("strComputerName").Value = "" 'reset input box if the machine is unreachable
                                         Exit Sub
                                     End If
                                     
                                     window.document.title = "Scripts currently running on: " & strComputer 'set title to new computer name if different from local
                                     window.document.getElementById("PCName").innerHTML = strComputer 'set computer name in header if different from local
                                     window.document.getElementById("TimeStamp").innerHTML = Now()
                                 
                                     wmiQuery = "Select * From Win32_Process Where (Name='wscript.exe' Or Name='cscript.exe' Or Name='mshta.exe')"  'define WMI query
                                     ' begin building table
                                     strOutput = "<table border=""0"" width=""100%"" id=""table1""><tr>" & _
                                         "<td><b>Script Name:</b></td><td><b>Process ID:</b></td>" & _
                                         "<td><b>Running Since:</b></td><td><b>Action</b></td></tr>"
                                     
                                     Set colItems = objWMI(strComputer, wmiQuery, intStat) 'retrieve WMI collection by calling the objWMI function
                                     
                                     For Each objItem In colItems 'loop through the collection
                                         strCmdLine = Replace(Replace(objItem.CommandLine, Chr(34), ""), "cscript.exe ", "")
                                         strCmdLine = Trim(strCmdLine) 'format string to pull out the script name being run
                                         intPID = objItem.ProcessID
                                         strOutput = strOutput & "<tr><td title='" & objItem.CommandLine & "'>" & Right(strCmdLine, Len(strCmdLine) - InStrRev(strCmdLine, "\")) & _
                                         "</td><td>" & intPID & "</td><td>" & ConvertDT(objItem.CreationDate) & "</td>" & _
                                         "<td><input type=""button"" value=""Terminate"" onclick=""KillScript('" & intPID & "')""></td></tr>"
                                     Next
                                     window.document.getElementById("RScripts").innerHTML = strOutput & "</table>" 'display data
                                     Set colItems = Nothing
                                 End Sub
                                     
                                 Sub KillScript(intPID)
                                     On Error Resume Next
                                     
                                     Dim wmiQuery, colItems, objItem, strResponse, strComputer
                                     
                                     strResponse = MsgBox("Are you sure you want to terminate the script with Process ID of: " & intPID & "?", 36, "Confirm Script Termination")
                                     If strResponse = vbNo Then Exit Sub
                                     
                                     strComputer = UCase(window.document.getElementById("strComputerName").Value)
                                     If strComputer = "" Then
                                         strComputer = GetLocCompName
                                     End If
                                     
                                     wmiQuery = "Select * From Win32_Process Where ProcessID = " & intPID 'define WMI query
                                     
                                     Set colItems = objWMI(strComputer, wmiQuery)'retrieve WMI collection by calling the objWMI function
                                     
                                     
                                     For Each objItem In colItems
                                         objItem.Terminate 'terminate process with PID specified
                                     Next
                                     Set colItems = Nothing
                                     GetScripts 'refresh the process list displayed
                                 End Sub
                                     
                                 Function GetLocCompName
                                 '         On Error Resume Next
                                     
                                     Dim objNetwork
                                     
                                     Set objNetwork = CreateObject("WScript.Network")
                                     GetLocCompName = UCase(objNetwork.ComputerName) 'get local computer name
                                     Set objNetwork = Nothing
                                 End Function
                                     
                                 Function Ping(strRmtPC)
                                 '         On Error Resume Next
                                     
                                     Dim wmiQuery, objPing, objStatus, blnStatus, intStat
                                     
                                     wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strRmtPC & "'"
                                     intStat = 1
                                     Set objPing = objWMI(".", wmiQuery, intStat)
                                     
                                     For Each objStatus in objPing
                                         If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
                                             blnStatus = False 'Not Reachable
                                         Else
                                             blnStatus = True 'Reachable
                                         End If
                                     Next
                                     
                                     Ping = blnStatus
                                     Set objPing = Nothing
                                 End Function
                                     
                                 Function objWMI(strComputer, strWQL, intStat)
                                     'On Error Resume Next
                                     Dim wmiNS, objWMIService, objSWbemLocator, objSWbemServices
                                     wmiNS = "\root\cimv2"
                                     if intStat = 0 then
                                         Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                                         Set objSWbemServices = objSWbemLocator.ConnectServer _
                                           (strComputer, wmiNS, strUserID.value, strPass.value)
                                         Set objWMI = objSWbemServices.ExecQuery(strWQL)
                                         Set objSWbemServices = nothing 
                                     Else
                                         Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS) 'connect to WMI
                                         Set objWMI = objWMIService.ExecQuery(strWQL) 'execute query
                                         Set objWMIService = Nothing
                                     End If
                                 End Function
                                     
                                 Function ConvertDT(strDT)
                                 '         On Error Resume Next
                                     
                                     Dim objTime
                                     
                                     Set objTime = CreateObject("WbemScripting.SWbemDateTime")
                                     objTime.Value = strDT
                                     ConvertDT = objTime.GetVarDate 'convert UTC to Standard Time
                                     Set objTime = Nothing
                                 End Function
                                 </script>
                                 <hta:application
                                 applicationname="Get Running Scripts"    
                                 border="dialog"
                                 borderstyle="normal"
                                 caption="Get Running Scripts"
                                 contextmenu="yes"
                                 icon="images\icon.ico"
                                 maximizebutton="yes"
                                 minimizebutton="yes"
                                 navigable="yes"
                                 scroll="no"
                                 selection="yes"
                                 showintaskbar="yes"
                                 singleinstance="yes"
                                 sysmenu="yes"
                                 version="1.0"
                                 windowstate="normal"
                                 >
                                 <style type="text/css">
                                 td {
                                 font-family: "Times New Roman", Times, serif;
                                 font-size: 18px;
                                 font-style: normal;
                                 font-weight: normal;
                                 font-variant: normal;
                                 color: #FFFFFF;
                                 vertical-align: top;
                                 }
                                 </style>
                                 </head>
                                 <body bgcolor="#272936" style="overflow:auto;color:#FFFFFF;">
                                 <div align="left">
                                 <h1>VBScripts Currently Running on: <span id="PCName"></span></h1>
                                 Connect To: <input type="text" id="strComputerName" onKeyPress="if window.event.keycode = 13 then GetScripts">&nbsp;&nbsp;
                                 UserName: <input type="text" name="strUserID" value="">&nbsp;&nbsp; Password: <input type="password" name="strPass" value="">&nbsp;&nbsp; <input type="submit" value="Connect" onclick="GetScripts">
                                 <br /><br />
                                 Last Updated: <span id="TimeStamp"></span>
                                 </div>
                                 <br />
                                 <span id="RScripts"></span>
                                 </body>
                                 </html>
                                <message edited by NssB on Tuesday, January 30, 2007 4:40 AM>
                                Its all about the code!
                                 
                                #16
                                  dm_4ever

                                  • Total Posts : 3687
                                  • Scores: 82
                                  • Reward points : 0
                                  • Joined: 6/29/2006
                                  • Location: Orange County, California
                                  • Status: offline
                                  RE: Scripts Currently Running Tuesday, January 30, 2007 6:11 AM (permalink)
                                  0
                                  I was actually thinking of changing the method of connection to use SWbemLocator "connectserver" to allow for alternate credentials, so thanks! 

                                  I just noticed the KillScript Sub needs to be updated to reflect the changes.
                                  <message edited by dm_4ever on Tuesday, January 30, 2007 6:42 AM>
                                  dm_4ever

                                  My philosophy: K.I.S.S - Keep It Simple Stupid
                                  Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
                                  Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
                                   
                                  #17
                                    dm_4ever

                                    • Total Posts : 3687
                                    • Scores: 82
                                    • Reward points : 0
                                    • Joined: 6/29/2006
                                    • Location: Orange County, California
                                    • Status: offline
                                    RE: Scripts Currently Running Tuesday, January 30, 2007 7:06 AM (permalink)
                                    0
                                    I made a few more changes.

                                    - KillScript sub should work
                                    - Added error handeling if credentials supplied are invalid

                                     <html>
                                     <head>
                                     <script language="vbscript">
                                     Option Explicit 
                                     
                                     Dim intRefreshRt : intRefreshRt = 30000 '30 Second Refresh Rate in Milliseconds
                                     
                                     Sub Window_OnLoad
                                     '         On Error Resume Next
                                        
                                         Dim iTimerID
                                         
                                         window.document.title = "Scripts currently running on: " & GetLocCompName 'set title with local computername by calling function
                                         window.document.getElementById("PCName").InnerHTML = GetLocCompName 'set computername in header <h1> tag
                                         GetScripts
                                         window.document.getElementById("strComputerName").focus 'focus to the inputbox to specify another pc
                                         iTimerID = window.setInterval("GetScripts", intRefreshRt)
                                         End Sub
                                        
                                     Sub GetScripts
                                        'On Error Resume Next
                                        
                                         Dim wmiQuery, colItems, objItem, strCmdLine, strOutput, intPID, strComputer
                                         
                                         strComputer = UCase(window.document.getElementById("strComputerName").Value) 'get computername from input box
                                     
                                         If strComputer = "" Or strComputer = GetLocCompName Or strComputer = "LOCALHOST" Or strComputer = "127.0.0.1" Or strComputer = "." Then
                                             strComputer = GetLocCompName 'if input box is empty, use local computer name by calling a function
                                         Else
                                             If strUserID.value = "" OR strPass.Value = "" then
                                                 MsgBox "You must enter a username AND password for remote hosts"
                                                 Exit Sub
                                             End If
                                         End If
                                         
                                         If Ping(strComputer) = False Then 'test connectivity with ping Function
                                             alert "Computer specified is unreachable!!"
                                             window.document.getElementById("strComputerName").Value = "" 'reset input box if the machine is unreachable
                                             Exit Sub
                                         End If    
                                        
                                         window.document.title = "Scripts currently running on: " & strComputer 'set title to new computer name if different from local
                                         window.document.getElementById("PCName").innerHTML = strComputer 'set computer name in header if different from local
                                         window.document.getElementById("TimeStamp").innerHTML = Now()
                                     
                                         wmiQuery = "Select * From Win32_Process Where (Name='wscript.exe' Or Name='cscript.exe' Or Name='mshta.exe')"  'define WMI query
                                         ' begin building table
                                         strOutput = "<table border=""0"" width=""100%"" id=""table1""><tr>" & _
                                         "<td><b>Script Name:</b></td><td><b>Process ID:</b></td>" & _
                                         "<td><b>Running Since:</b></td><td><b>Action</b></td></tr>"
                                         
                                         On Error Resume Next
                                         Set colItems = objWMI(strComputer, wmiQuery) 'retrieve WMI collection by calling the objWMI function
                                         If IsEmpty(colItems) Then
                                             window.document.getElementById("RScripts").innerHTML = ""
                                             Exit Sub
                                         End If
                                         On Error GoTo 0
                                         
                                         For Each objItem In colItems 'loop through the collection
                                             strCmdLine = Replace(Replace(objItem.CommandLine, Chr(34), ""), "cscript.exe ", "")
                                             strCmdLine = Trim(strCmdLine) 'format string to pull out the script name being run
                                             intPID = objItem.ProcessID
                                             strOutput = strOutput & "<tr><td title='" & objItem.CommandLine & "'>" & Right(strCmdLine, Len(strCmdLine) - InStrRev(strCmdLine, "\")) & _
                                             "</td><td>" & intPID & "</td><td>" & ConvertDT(objItem.CreationDate) & "</td>" & _
                                             "<td><input type=""button"" value=""Terminate"" onclick=""KillScript('" & intPID & "')""></td></tr>"
                                         Next
                                         window.document.getElementById("RScripts").innerHTML = strOutput & "</table>" 'display data
                                         Set colItems = Nothing
                                     End Sub
                                        
                                     Sub KillScript(intPID)
                                         On Error Resume Next
                                         
                                         Dim wmiQuery, colItems, objItem, strResponse, strComputer
                                         
                                         strResponse = MsgBox("Are you sure you want to terminate the script with Process ID of: " & intPID & "?", 36, "Confirm Script Termination")
                                         If strResponse = vbNo Then Exit Sub
                                         
                                         strComputer = UCase(window.document.getElementById("strComputerName").Value)
                                             If strComputer = "" Or strComputer = GetLocCompName Or strComputer = "LOCALHOST" Or strComputer = "127.0.0.1" Or strComputer = "." Then
                                             strComputer = GetLocCompName 'if input box is empty, use local computer name by calling a function
                                         End If
                                         
                                         wmiQuery = "Select * From Win32_Process Where ProcessID = " & intPID 'define WMI query
                                         
                                         Set colItems = objWMI(strComputer, wmiQuery)'retrieve WMI collection by calling the objWMI function
                                         
                                         For Each objItem In colItems
                                             objItem.Terminate 'terminate process with PID specified
                                         Next
                                         Set colItems = Nothing
                                         GetScripts 'refresh the process list displayed
                                     End Sub
                                        
                                     Function GetLocCompName
                                     '         On Error Resume Next
                                        
                                         Dim objNetwork
                                         
                                         Set objNetwork = CreateObject("WScript.Network")
                                         GetLocCompName = UCase(objNetwork.ComputerName) 'get local computer name
                                         Set objNetwork = Nothing
                                     End Function
                                        
                                     Function Ping(strRmtPC)
                                     '         On Error Resume Next
                                        
                                        Dim wmiQuery, objPing, objStatus, blnStatus
                                        
                                        wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strRmtPC & "'"
                                     
                                        Set objPing = objWMI(".", wmiQuery)
                                        
                                        For Each objStatus in objPing
                                            If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
                                                blnStatus = False 'Not Reachable
                                            Else
                                                blnStatus = True 'Reachable
                                            End If
                                        Next
                                        Ping = blnStatus
                                        Set objPing = Nothing
                                     End Function
                                        
                                     Function objWMI(strComputer, strWQL)
                                         'On Error Resume Next
                                         Dim wmiNS, objWMIService, objSWbemLocator, objSWbemServices
                                         Dim strUID, strPwd
                                         
                                         wmiNS = "\root\cimv2"
                                         strUID = window.document.getElementById("strUserID").value
                                         strPwd = window.document.getElementById("strPass").value
                                         
                                         If strComputer = "." Or strComputer = GetLocCompName Then
                                             strUID = ""
                                             strPwd = ""
                                         End If
                                         
                                         Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                                         On Error Resume Next
                                         Set objSWbemServices = objSWbemLocator.ConnectServer _
                                         (strComputer, wmiNS, strUID, strPwd)
                                             Select Case Err.Number
                                                 Case -2147024891
                                                     alert "Access Denied! Please check the credentials supplied."
                                                     Exit Function
                                             End Select
                                         On Error GoTo 0
                                         Set objWMI = objSWbemServices.ExecQuery(strWQL)
                                         Set objSWbemServices = Nothing 
                                     End Function
                                        
                                     Function ConvertDT(strDT)
                                     '         On Error Resume Next
                                        
                                        Dim objTime
                                        
                                        Set objTime = CreateObject("WbemScripting.SWbemDateTime")
                                        objTime.Value = strDT
                                        ConvertDT = objTime.GetVarDate 'convert UTC to Standard Time
                                        Set objTime = Nothing
                                     End Function
                                     </script>
                                     <hta:application
                                     applicationname="Get Running Scripts"    
                                     border="dialog"
                                     borderstyle="normal"
                                     caption="Get Running Scripts"
                                     contextmenu="yes"
                                     icon="images\icon.ico"
                                     maximizebutton="yes"
                                     minimizebutton="yes"
                                     navigable="yes"
                                     scroll="no"
                                     selection="yes"
                                     showintaskbar="yes"
                                     singleinstance="yes"
                                     sysmenu="yes"
                                     version="1.0"
                                     windowstate="normal"
                                     >
                                     <style type="text/css">
                                     td {
                                     font-family: "Times New Roman", Times, serif;
                                     font-size: 18px;
                                     font-style: normal;
                                     font-weight: normal;
                                     font-variant: normal;
                                     color: #FFFFFF;
                                     vertical-align: top;
                                     }
                                     </style>
                                     </head>
                                     <body bgcolor="#272936" style="overflow:auto;color:#FFFFFF;">
                                     <div align="left">
                                     <h1>VBScripts Currently Running on: <span id="PCName"></span></h1>
                                     Connect To: <input type="text" id="strComputerName" onKeyPress="if window.event.keycode = 13 then GetScripts">&nbsp;&nbsp;
                                     UserName: <input type="text" id="strUserID" value="">&nbsp;&nbsp; Password: <input type="password" id="strPass" value="">&nbsp;&nbsp; <input type="submit" value="Connect" onclick="GetScripts">
                                     <br /><br />
                                     Last Updated: <span id="TimeStamp"></span>
                                     </div>
                                     <br />
                                     <span id="RScripts"></span>
                                     </body>
                                     </html>
                                     
                                    <message edited by dm_4ever on Tuesday, January 30, 2007 7:09 AM>
                                    dm_4ever

                                    My philosophy: K.I.S.S - Keep It Simple Stupid
                                    Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
                                    Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
                                     
                                    #18
                                      NssB

                                      • Total Posts : 31
                                      • Scores: 0
                                      • Reward points : 0
                                      • Joined: 6/26/2006
                                      • Status: offline
                                      RE: Scripts Currently Running Tuesday, January 30, 2007 9:14 AM (permalink)
                                      0
                                      Good work DM. Lets hope we can sort the remote host commandline method.
                                      I've done a bit of searching and cannot find anything like that method for 'remote' hosts.


                                      Can anyone reading advise as to why the commandline method exists locally, but not remotely?



                                      NssB



                                      EDIT: Scrap the above, just noticed edit in original post. XP/2003 Server machine only accepting CommandLine property for the win32::process
                                      <message edited by NssB on Tuesday, January 30, 2007 8:14 PM>
                                      Its all about the code!
                                       
                                      #19
                                        NssB

                                        • Total Posts : 31
                                        • Scores: 0
                                        • Reward points : 0
                                        • Joined: 6/26/2006
                                        • Status: offline
                                        RE: Scripts Currently Running Thursday, February 08, 2007 12:33 AM (permalink)
                                        0
                                        ok, ok you could probably hack HTA's to death, but I thought I'd post this anyway. Its the one I use as it keeps me in check.
                                        Feel free to tidy and repost.

                                         <html>
                                         <head>
                                         <script language="vbscript">
                                         Option Explicit 
                                         
                                         Dim intRefreshRt : intRefreshRt = 30000 '30 Second Refresh Rate in Milliseconds
                                         
                                         Sub Window_OnLoad
                                         '         On Error Resume Next
                                           
                                            Dim iTimerID
                                            displayEvents
                                            window.document.title = "Scripts currently running on: " & GetLocCompName 'set title with local computername by calling function
                                            window.document.getElementById("PCName").InnerHTML = GetLocCompName 'set computername in header <h1> tag
                                            GetScripts
                                            'window.document.getElementById("strComputerName").focus 'focus to the inputbox to specify another pc
                                            'window.document.ge
                                            iTimerID = window.setInterval("GetScripts", intRefreshRt)
                                            End Sub
                                         
                                         Sub displayEvents
                                             dim strOption
                                             for each strOption in radioOption
                                             if strOption.checked AND strOption.Value = "Localhost" then
                                                 remoteparams.style.display="none"
                                                     window.document.getElementById("strComputerName").Value = "Localhost"
                                                 window.document.getElementById("strUserID").Value = ""
                                                 window.document.getElementById("strPass").Value = ""
                                                     GetScripts
                                                 exit sub
                                             else
                                                 remoteparams.style.display = "block"
                                                     window.document.getElementById("strComputerName").Value = ""
                                                 exit sub
                                             end if
                                             next
                                         End Sub
                                           
                                         Sub GetScripts
                                           'On Error Resume Next
                                           
                                            Dim wmiQuery, colItems, objItem, strCmdLine, strOutput, intPID, strComputer
                                            
                                            strComputer = UCase(window.document.getElementById("strComputerName").Value) 'get computername from input box
                                         
                                            If strComputer = "" Or strComputer = GetLocCompName Or strComputer = "LOCALHOST" Or strComputer = "127.0.0.1" Or strComputer = "." Then
                                                strComputer = GetLocCompName
                                            Else
                                                If strUserID.value = "" OR strPass.Value = "" then
                                                    MsgBox "You must enter a username AND password for remote hosts"
                                                    Exit Sub
                                                End If
                                            End If
                                            
                                            If Ping(strComputer) = False Then 'test connectivity with ping Function
                                                alert "Computer specified is unreachable!!"
                                                window.document.getElementById("strComputerName").Value = "" 'reset input box if the machine is unreachable
                                                Exit Sub
                                            End If    
                                           
                                            window.document.title = "Scripts currently running on: " & strComputer 'set title to new computer name if different from local
                                            window.document.getElementById("PCName").innerHTML = strComputer 'set computer name in header if different from local
                                            window.document.getElementById("TimeStamp").innerHTML = Now()
                                         
                                            wmiQuery = "Select * From Win32_Process Where (Name='wscript.exe' Or Name='cscript.exe' Or Name='mshta.exe')"  'define WMI query
                                            ' begin building table
                                            strOutput = "<table border=""0"" width=""100%"" id=""table1""><tr>" & _
                                            "<td><b>Script Name:</b></td><td><b>Process ID:</b></td>" & _
                                            "<td><b>Running Since:</b></td><td><b>Action</b></td></tr>"
                                            
                                            On Error Resume Next
                                            Set colItems = objWMI(strComputer, wmiQuery) 'retrieve WMI collection by calling the objWMI function
                                            If IsEmpty(colItems) Then
                                                window.document.getElementById("RScripts").innerHTML = ""
                                                Exit Sub
                                            End If
                                            On Error GoTo 0
                                            
                                            For Each objItem In colItems 'loop through the collection
                                                strCmdLine = Replace(Replace(objItem.CommandLine, Chr(34), ""), "cscript.exe ", "")
                                                strCmdLine = Trim(strCmdLine) 'format string to pull out the script name being run
                                                intPID = objItem.ProcessID
                                                strOutput = strOutput & "<tr><td title='" & objItem.CommandLine & "'>" & Right(strCmdLine, Len(strCmdLine) - InStrRev(strCmdLine, "\")) & _
                                                "</td><td>" & intPID & "</td><td>" & ConvertDT(objItem.CreationDate) & "</td>" & _
                                                "<td><input type=""button"" value=""Terminate"" onclick=""KillScript('" & intPID & "')""></td></tr>"
                                            Next
                                            window.document.getElementById("RScripts").innerHTML = strOutput & "</table>" 'display data
                                            Set colItems = Nothing
                                         End Sub
                                           
                                         Sub KillScript(intPID)
                                            On Error Resume Next
                                            
                                            Dim wmiQuery, colItems, objItem, strResponse, strComputer
                                            
                                            strResponse = MsgBox("Are you sure you want to terminate the script with Process ID of: " & intPID & "?", 36, "Confirm Script Termination")
                                            If strResponse = vbNo Then Exit Sub
                                            
                                            strComputer = UCase(window.document.getElementById("strComputerName").Value)
                                                If strComputer = "" Or strComputer = GetLocCompName Or strComputer = "LOCALHOST" Or strComputer = "127.0.0.1" Or strComputer = "." Then
                                                strComputer = GetLocCompName 'if input box is empty, use local computer name by calling a function
                                            End If
                                            
                                            wmiQuery = "Select * From Win32_Process Where ProcessID = " & intPID 'define WMI query
                                            
                                            Set colItems = objWMI(strComputer, wmiQuery)'retrieve WMI collection by calling the objWMI function
                                            
                                            For Each objItem In colItems
                                                objItem.Terminate 'terminate process with PID specified
                                            Next
                                            Set colItems = Nothing
                                            GetScripts 'refresh the process list displayed
                                         End Sub
                                           
                                         Function GetLocCompName
                                         '         On Error Resume Next
                                           
                                            Dim objNetwork
                                            
                                            Set objNetwork = CreateObject("WScript.Network")
                                            GetLocCompName = UCase(objNetwork.ComputerName) 'get local computer name
                                            Set objNetwork = Nothing
                                         End Function
                                           
                                         Function Ping(strRmtPC)
                                         '         On Error Resume Next
                                           
                                           Dim wmiQuery, objPing, objStatus, blnStatus
                                           
                                           wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strRmtPC & "'"
                                         
                                           Set objPing = objWMI(".", wmiQuery)
                                           
                                           For Each objStatus in objPing
                                               If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
                                                   blnStatus = False 'Not Reachable
                                               Else
                                                   blnStatus = True 'Reachable
                                               End If
                                           Next
                                           Ping = blnStatus
                                           Set objPing = Nothing
                                         End Function
                                           
                                         Function objWMI(strComputer, strWQL)
                                            'On Error Resume Next
                                            Dim wmiNS, objWMIService, objSWbemLocator, objSWbemServices
                                            Dim strUID, strPwd
                                            
                                            wmiNS = "\root\cimv2"
                                            strUID = window.document.getElementById("strUserID").value
                                            strPwd = window.document.getElementById("strPass").value
                                            
                                            If strComputer = "." Or strComputer = GetLocCompName Then
                                                strUID = ""
                                                strPwd = ""
                                            End If
                                            
                                            Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
                                            On Error Resume Next
                                            Set objSWbemServices = objSWbemLocator.ConnectServer _
                                            (strComputer, wmiNS, strUID, strPwd)
                                                Select Case Err.Number
                                                    Case -2147024891
                                                        alert "Access Denied! Please check the credentials supplied."
                                                        Exit Function
                                                End Select
                                            On Error GoTo 0
                                            Set objWMI = objSWbemServices.ExecQuery(strWQL)
                                            Set objSWbemServices = Nothing 
                                         End Function
                                           
                                         Function ConvertDT(strDT)
                                         '         On Error Resume Next
                                           
                                           Dim objTime
                                           
                                           Set objTime = CreateObject("WbemScripting.SWbemDateTime")
                                           objTime.Value = strDT
                                           ConvertDT = objTime.GetVarDate 'convert UTC to Standard Time
                                           Set objTime = Nothing
                                         End Function
                                         </script>
                                         <hta:application
                                         applicationname="Get Running Scripts"    
                                         border="dialog"
                                         borderstyle="normal"
                                         caption="Get Running Scripts"
                                         contextmenu="yes"
                                         icon="images\icon.ico"
                                         maximizebutton="yes"
                                         minimizebutton="yes"
                                         navigable="yes"
                                         scroll="no"
                                         selection="yes"
                                         showintaskbar="yes"
                                         singleinstance="yes"
                                         sysmenu="yes"
                                         version="1.0"
                                         windowstate="normal"
                                         >
                                         <style type="text/css">
                                         td {
                                         font-family: "Times New Roman", Times, serif;
                                         font-size: 18px;
                                         font-style: normal;
                                         font-weight: normal;
                                         font-variant: normal;
                                         color: #FFFFFF;
                                         vertical-align: top;
                                         }
                                         </style>
                                         </head>
                                         <body bgcolor="#272936" style="overflow:auto;color:#FFFFFF;">
                                         <div align="left">
                                         <h1>VBScripts Currently Running on: <span id="PCName"></span></h1>
                                         <input type="radio" name="RadioOption" value="Localhost" checked onClick="displayEvents">Localhost<BR>
                                         <input type="radio" name="RadioOption" value="RemoteHost" onClick="displayEvents">Remote Host<BR>
                                         </div>
                                         <div id="remoteparams" align="left">
                                         Connect To: <input type="text" id="strComputerName" onKeyPress="if window.event.keycode = 13 then GetScripts">&nbsp;&nbsp;
                                         UserName: <input type="text" id="strUserID" value="">&nbsp;&nbsp; Password: <input type="password" id="strPass" value="">&nbsp;&nbsp; <input type="submit" value="Connect" onclick="GetScripts">
                                         </div>
                                         <br /><br />
                                         Last Updated: <span id="TimeStamp"></span>
                                         
                                         <br />
                                         <span id="RScripts"></span>
                                         </body>
                                         </html>
                                         
                                         





                                        Regards
                                        NssB
                                        Its all about the code!
                                         
                                        #20

                                          Online Bookmarks Sharing: Share/Bookmark
                                          Change Page: 12 > | Showing page 1 of 2, messages 1 to 20 of 21

                                          Jump to:

                                          Current active users

                                          There are 0 members and 1 guests.

                                          Icon Legend and Permission

                                          • New Messages
                                          • No New Messages
                                          • Hot Topic w/ New Messages
                                          • Hot Topic w/o New Messages
                                          • Locked w/ New Messages
                                          • Locked w/o New Messages
                                          • Read Message
                                          • Post New Thread
                                          • Reply to message
                                          • Post New Poll
                                          • Submit Vote
                                          • Post reward post
                                          • Delete my own posts
                                          • Delete my own threads
                                          • Rate post

                                          2000-2012 ASPPlayground.NET Forum Version 3.9