Solved

calling WMI from asp

Posted on 2006-07-24
3
278 Views
Last Modified: 2012-05-05
experts this is my third post involving this proble. basically, all i want to do is to be able to restart (or stop and start) a service on a server from a web page. I have found the code bellow which will do this for me, if only I could get it working. I get the following error when I try and run the page:

Error Type:
(0x80041003)
/battest/betest3.asp, line 21

this is where I make a call to the winmgmts object.

I am just trying to run this locally at the moment, and have given Everyone full control on my pc just to see if I can get it working.

can someone please help me. I am desperate to get this resolved, hence the points.

here is the code I am using:

<%
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep, WScript
strComputer = "localhost"
intSleep = 15000

Set WScript = server.CreateObject("wscript.shell")
Response.Write " Click OK, then wait " & intSleep & " milliseconds"

'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name ="_
& strService & " ")

For Each objService in colListOfServices
    objService.StopService()
    WSCript.Sleep intSleep
    objService.StartService()
Next
Response.Write "Your "& strService & " service has Started"
WScript.Quit
' End of Example WMI script to Start / Stop services
      
%>


thanks in advance for any help
0
Comment
Question by:telliot79
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
joeposter649 earned 500 total points
Comment Utility
0
 

Author Comment

by:telliot79
Comment Utility
nope,

amazingly enough, I had searched the rest of the web, but not MSDN, or atleast not to this page on the MSDN site.

Thanks joeposter649, I will post my final code when I have pieced it all together.
0
 

Author Comment

by:telliot79
Comment Utility
uncheck anonymous access in IIS for this file, or the folder where this file resides and all should be gtg


<%
Option Explicit
Dim objWMIService, objItem, objService, strServiceList
Dim colListOfServices, strComputer, strService, optionList
Dim colServices, strMessage, colService

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service ")

For Each objService in colListOfServices
      optionList = optionList & "<OPTION value=" & chr(34) & objService.name & chr(34)
      if request("ddService") = objService.name then
            optionList = optionList & " selected"
            objService.StopService()
            objService.StartService()
            strMessage = "The Service '" & objService.name & "' has been stopped and started "
      end if
      optionList = optionList & ">" & objService.name & "</OPTION>"
Next

%>


<html>
<head>
      <title>Service Restart</title>
</head>
<form name="form1" method="post" action="kickstart.asp" ID="Form1">
<body>
<table border="0" cellpadding="0" cellspacing="0">
      <tr>
            <td valign=middle><SELECT  NAME="ddService" class="normaltext" ID="Select1">
                        <option value=""> -- SELECT -- </option>
                        <%=optionList%>
                  </SELECT><br>
            </td>
            <td align="center"><INPUT type="Submit" NAME="btnSubmit" value="Restart" ID="Submit1"></td></tr>
      <tr><td colspan="2"><br><%=strMessage%></td></tr>
</table>
</form>
</body>
</html>

            
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now