
Stopping a Service or Killing a Process with VBScript and WMI in ASP
I have an ASP page that displays a lot of info regarding processes and
services. The value of varArray(2, i) in fWriteTableProcesses is the PID of
the Process, which I hope to use to kill the process. The value of
varArray(8, i) in fWriteTableServices is the Service Name (not Service
Display name),
which I hope to use to stop/start the service. As you can see the last <TD>
in each, fWriteTableServices and
fWriteTableProcesses =
http://url/path/server.asp?procHandle=PID&server=Nameoftargetserver or
http://url/path/server.asp?svcHandle=ServiceName&server=Nameoftargets....
How would I go about coding the Kill Process and Stop/Start functions?
I know next to nothing about ASP and am an amateur
VBScripter. Any help or
suggestion of a good ASP book would be appreciated.
-TIA
JM
query = "select displayname, state, startmode, servicetype, status,
startname, acceptpause, acceptstop, name " _
& "from win32_service"
'
' Execute the query and create the objInstance object
'
Set objInstance = objService.ExecQuery(query)
'
' Get the item count within ojbInstance and redimension the array
'
itemCount = objInstance.Count - 1
If itemCount > 1 Then
ReDim arrServices(8, CInt(itemCount))
Else
ReDim arrServices(8, 1)
End If
aCounter = 0
'
' Populate the array
'
For Each item In objInstance
arrServices(0, aCounter) = item.DisplayName
arrServices(1, aCounter) = item.State
arrServices(2, aCounter) = item.StartMode
arrServices(3, aCounter) = item.serviceType
arrServices(4, aCounter) = item.Status
arrServices(5, aCounter) = item.startname
arrServices(6, aCounter) = item.acceptPause
arrServices(7, aCounter) = item.acceptStop
arrServices(8, aCounter) = item.Name
aCounter = aCounter + 1
Next
Set objInstance = Nothing
'
' Write the Services heading and the column headings
'
fWriteHeading ("Services - " & ubound(arrServices, 2) + 1)
response.Write _
" <tr align=center bgcolor=#b6b6b6>" & vbCrLf & _
" <td class=category nowrap>Name</td>" & vbCrLf & _
" <td class=category nowrap>State</td>" & vbCrLf & _
" <td class=category nowrap>Start Mode</td>" & vbCrLf & _
" <td class=category nowrap>Type</td>" & vbCrLf & _
" <td class=category nowrap>Status</td>" & vbCrLf & _
" <td class=category nowrap>Start Name</td>" & vbCrLf & _
" <td class=category nowrap>Svc Mgmt</td>" & vbCrLf & _
" </tr>" & vbCrLf
I've added a cell to each row with a link to KILL a process or Stop/Start a
Service:
Private Sub fWriteTableProcesses(varArray)
'
' Takes an array and writes the processes section of the page
'
For i = 0 To UBound(varArray, 2)
response.Write _
" <tr>" & vbCrLf & _
" <TD bgcolor=#cccccc align=left class=result><a href=" & scriptPath &
"?procHandle=" & varArray(2, i) _
& "&server=" & strServer & ">" & varArray(0, i) & "</a></TD>" & vbCrLf & _
" <TD bgcolor=#99ccff align=center class=result>" & varArray(1, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#cccccc align=center class=result>" & varArray(2, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#99ccff align=center class=result>" & varArray(3, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#cccccc align=center class=result>" & varArray(4, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#99ccff align=center class=result>" & varArray(5, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#cccccc align=center class=result><a href=" & scriptPath2 &
"server.asp?procHandle=" & varArray(2, i) _ 'The Link that kills a
process.
& "&server=" & strServer & ">Kill</a></TD>" & vbCrLf & _
" </tr>" & vbCrLf
Next
End Sub
'_________________________________________________________________________
Private Sub fWriteTableServices(varArray)
'
' Takes an array and writes the services section of the page
'
For i = 0 To UBound(varArray, 2)
response.Write _
" <tr>" & vbCrLf & _
" <TD bgcolor=#cccccc align=left class=result>" & varArray(0, i) & "</td>" &
vbCrLf & _
" <TD bgcolor=#99ccff align=center class=result>" & varArray(1, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#cccccc align=center class=result>" & varArray(2, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#99ccff align=center class=result nowrap>" & varArray(3, i) &
"</TD>" & vbCrLf & _
" <TD bgcolor=#cccccc align=center class=result>" & varArray(4, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#99ccff align=center class=result>" & varArray(5, i) & "</TD>"
& vbCrLf & _
" <TD bgcolor=#cccccc align=center class=result><a href=" & scriptPath2 &
"server.asp?svcHandle=" & cstr(varArray(8, i)) _ ' The link that stop/start
a Svc
& "&server=" & strServer & ">Stop/Start</a></TD>" & vbCrLf & _
" </tr>" & vbCrLf
Next
End Sub