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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help troubleshooting ASP Page: Function not running

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
I have been staring at this too long and just not finding what I have wrong here. Can anyone tell me what the problem is? My page displays correctly, but the buttons do not execute the functions. Thanks in advance.

Code:
<%@ LANGUAGE="VBSCRIPT" %>
<%
On Error Resume Next
Dim strComputer
strComputer = "TSPSERVER"

Sub StopService(ServiceName)
    Set objWMI = getobject("winmgmts://" & strComputer)
    queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
    set results = objWMI.execquery(queryString)
    for each service in results
	    if service.state = "Running" then
             service.stopService
        end if
     next
     Response.Redirect "Servicemanagement.asp"
End Sub


Sub StartService(ServiceName)
	Set objWMI = getobject("winmgmts://" & strComputer)
	queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
    set results = objWMI.execquery(queryString)
    for each service in results
	    if service.state <> "Running" then
             service.startService
        end if
    next
    Response.Redirect "Servicemanagement.asp"
End Sub


%>
<html>
<head>
<title>Service Management</title>
</head>


<body bgcolor="#99CCFF" style="text-align: center" link="#000000" vlink="#000000" alink="#99CCFF">

<table width="80%" border="1">
<tr bgcolor="white"><th style="width: 412px">Service</th>
	<th style="width: 180px">State</th><th>Action</th></tr>
<tr><td style="width: 412px"></td><td style="width: 180px"></td>
	
<%
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each objItem in colItems
	Response.Write "<tr><td>" 
	Response.Write objItem.DisplayName 
	Response.Write "</td><td>" 
	Response.Write objItem.State 
	Response.Write "</td><td>"
	If objItem.State = "Running" Then
		Response.Write "<input type='Button' Value='Stop' onclick='StopService(" & objItem.Name & ")'>"
	Else 
		Response.Write "<input type='Button' Value='Start' onclick='StartService(" & objItem.Name & ")'>"
	End If 

	Response.Write "</td></tr>"
Next

%>

</table>

</body>
</html>

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
I'm no ASP expert, but are you sure the problem is with the function calls? It could be that your functions are getting called, but that there are errors within the functions that prevent them from executing properly.

I'm not sure, but I don't think you can 'GetObject' in ASP. You may want to change that to, Server.CreateObject.

To see what I mean...

Code:
Sub StartService(ServiceName)
    [!]Response.Write("In Function StartService")
    Response.End[/!]
    Set objWMI = getobject("winmgmts://" & strComputer)
    queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
    set results = objWMI.execquery(queryString)
    for each service in results
        if service.state <> "Running" then
             service.startService
        end if
    next
    Response.Redirect "Servicemanagement.asp"
End Sub

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Are you sure you are setting your objWMI to the correct location?

I see outside the function you have:
Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer [!]& "\root\cimv2"[/!])

But inside the function you have:
Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer)

And in your queryString, you are referencing Win32_Service in all cases.

Also I'm not sure, but is Win32_Service case sensitive?? If so, you have it all lowercase in your subroutines.





[monkey][snake] <.
 
I had to rethink how I was checking for the buttons, and then that caused me to redo how I was calling the functions. I now have a working page. For anyone else wanting to check it out, here it is.

Code:
<%
'==========================================================================
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================

''
' A Button Handler is a utility object for working with buttons. It allows
' them to store more information than just a name by using a dot delimiter
' in the name attribute of the input tag.
'
' For example, you can create a series of buttons like this:
' <input type="submit" name="cmdDelete.23422341.9080" value="Add"/>
' <input type="submit" name="cmdDelete.89562331.1024" value="Add"/>
' <input type="submit" name="cmdDelete.99381200.7453" value="Add"/>
'
' In the above button, cmdDelete is the "base name," and the two numeric
' portions are "button parameters." Using a ButtonHandler, I can determine
' whether any button with a particular base name has been pressed, and
' extract the parameters.
'
' All routines in this class are functions, so that a consistent calling
' syntax can be used to access them.
'
Class ButtonHandler

   '' The form that this button handler works on (a Request.Form object).
   Private m_form

   '' Inspector for m_form.
   Public Property Get form()
      Set form = m_form
   End Property 'get form

   '' Mutator for m_form.
   Public Property Set form(p_form)
      Set m_form = p_form
   End Property 'set form

   ''
   ' Determines if any button with the specified base name was pressed
   ' on the form.
   ' Example:
   '   If bh.wasPressed("cmdAdd") Then Response.Write "User wants to add" 
   '
   ' @param strButtonBaseName the base name of a button being looked for.
   ' @return true if the button was pressed, false if not.
   '
   Public Function wasPressed(strButtonBaseName)
      Dim strField 'a key in the form

      wasPressed = false
      
      'make sure a form has been provided
      If NOT IsObject(m_form) Then
         'no form present in m_form
         Exit Function 'wasPressed will be false
      End If 'is a form loaded in m_form?
         
      For Each strField In m_form
         If Left(strField, Len(strButtonBaseName)) = strButtonBaseName Then
            wasPressed = true
            Exit Function 'no use sticking around
         End If 'if the button was present as a key in the form         
      Next 'strField
      
   End Function 'wasPressed
   
   ''
   ' Allows the caller to get a particular parameter that was specified in
   ' the button name attribute.
   ' Parameter numbers start at 1. 0 will return the base name.
   ' For example, if my button name was "cmdDelete.123441.true", then
   '    bh.requestParameter("cmdDelete", 0) returns "123441"
   '    bh.requestParameter("cmdDelete", 1) returns "true"
   '
   ' @param strButtonBaseName the base name of the button to look up.
   ' @param nParam            the number of the parameter to retrieve, where
   '                          0 is the base name and 1 is the first parameter.
   ' @return                  the value of the parameter requested, or "" if
   '                          the specified button/parameter does not exist.
   '
   Public Function requestParameter(strButtonBaseName, nParam)
      Dim strField      'a key in the form
      Dim strFoundField 'a key in the form that matches the requested base name
      Dim arrFoundField 'strFoundField, split up into an array around ".".
   
      strFoundField = ""
      
      For Each strField In m_form
         If Left(strField, Len(strButtonBaseName)) = strButtonBaseName Then
            strFoundField = strField
            'response.write "basename found"
            Exit For 'we found a matching button, no need to look further
         End If 'if the button was present as a key in the form
      Next 'strField
      
      ' At this point, if strFoundField is blank, then a matching button was
      ' not found: return blank.  If strFoundField is populated, then a
      ' matching button was found: look for the requested parameter.
      If strFoundField = "" Then
         requestParameter = ""
         Exit Function
      End If 'did we fail to find a matching button?
      
      'split the field string up around the "." delimiter
      arrFoundField = Split(strFoundField, ".")

      If nParam > UBound(arrFoundField) OR nParam < LBound(arrFoundField) Then
         'specified parameter does not exist
         requestParameter = ""
      Else 'i.e. specified parameter does exist
         requestParameter = arrFoundField(nParam)
      End If 'was the specified parameter in the valid range of parameters?
      
   End Function 'requestParameter

End Class 'ButtonHandler
%>

You must change the value for strComputer in the following page. Use a server name or IP address.

Code:
<%@ LANGUAGE="VBSCRIPT" %>
<!-- #include file="buttonHandler.inc" -->

<%
'==========================================================================
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' DATE  : 3/21/2007
' COPYRIGHT (c) 2007 All Rights Reserved
'
' COMMENT: 
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================
Dim strComputer, objWMIService
strComputer = "TSPSERVER"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Dim bh
Set bh = New ButtonHandler
Set bh.form = Request.Form

Service = bh.requestParameter("ActionButton", 1)


If Len(Service) > 0 Then
	ChangeService(Service)
End If

Sub ChangeService(ServiceName)
	'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='"& ServiceName & "'")
	For Each objService in colServiceList           
	    if objService.state = "Running" then
             objService.stopService()
        else
        	 objService.startService()
        end if
     next
     
     Response.Redirect "Servicemanagement.asp"
End Sub


%>
<html>
<head>
<title>Service Management</title>
</head>


<body bgcolor="#99CCFF" style="text-align: center" link="#000000" vlink="#000000" alink="#99CCFF">
<form action="Servicemanagement.asp" method="post">
<table width="80%" border="1">
<tr bgcolor="white"><th colspan="3">Services List For <% Response.Write strComputer %></th></tr>

<tr bgcolor="white"><th style="width: 412px">Service</th>
	<th style="width: 180px">State</th><th>Action</th></tr>
<tr><td style="width: 412px"></td><td style="width: 180px"></td>
	
<%
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each objItem in colItems
	ServiceCount = ServiceCount +1
Next
Dim SvcSortedArray
ReDim SvcSortedArray(ServiceCount)
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)

For Each objItem in colItems
	SvcSortedArray(x) = objItem.DisplayName & "@" & objItem.State & "@" & objItem.Name
	x = x +1
Next

for i = UBound(SvcSortedArray) - 1 To 0 Step -1
    for j= 0 to i
        if SvcSortedArray(j)>SvcSortedArray(j+1) then
            temp=SvcSortedArray(j+1)
            SvcSortedArray(j+1)=SvcSortedArray(j)
            SvcSortedArray(j)=temp
        end if
    next
next 

For Each Line In SvcSortedArray
	Dim QuickArray
	QuickArray = Split(Line,"@")
	If UBound(QuickArray) = 2 Then
		ServiceDisplayName = QuickArray(0)
		ServiceState = QuickArray(1)
		ServiceName = QuickArray(2)
	
		Response.Write "<tr><td>" 
		Response.Write ServiceDisplayName 
		Response.Write "</td><td>"
		
		Select Case QuickArray(1)
		Case "Running"
		    Response.Write "<font color='green'>"
		Case "Stopped" 
   		    Response.Write "<font color='red'>"
   		Case Else
	   		Response.Write "<font color='blue'>"
	 	End Select
	 	
	 	Response.Write ServiceState 
		Response.Write "</font></td><td>"
		If QuickArray(1) = "Running" Then
			Response.Write "<input type='Submit' Name='ActionButton." & ServiceName  &"' Value='Stop' size='35'>"
		Else 
			Response.Write "<input type='Submit' Name='ActionButton." & ServiceName  &"' Value='Start' size='35'>"
		End If 
	
		Response.Write "</td></tr>"
		
	End If
Next


%>

</table>
</form>
</body>
</html>

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top