Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripts Currently Running 1

Status
Not open for further replies.

dm4ever

Technical User
Jul 3, 2006
991
US
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 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.
NOTE: Watch out for line wrapping
Code:
<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>

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I've only tested the HTA on an XP box. The UTC to Standard Time conversion function only work on XP. I'll post an alternate function that should work with 2k. By the way, is there a way to update my post here?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
i used js previously for the time converstion stuff which worked on old machines...if i recall, thanks for the post, i am learning hta, html and this will come in useful as a ref :)
 
Date/Time Conversition Function for Win2k, just replace the ConverDT function in the example above with this one:
Code:
   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

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
'this is what i used as i was comparing file/folder date and times between servers around the world which had different timezone settings etc

'jscript of course but called from a wsf file

function GetDate(drvPath) {
var fs, d, s;
d = new Date(drvPath);
s = d.toUTCString() ;
return s;
}
 
Thank you for that function mrmovie! Since this is an HTA, this function could be dropped in there and called from the vbscript section.


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever, good job! Thought you deserved a star.
 
Thank you! I hope some find it useful.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top