469,352 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Access Sql server Agent Windows service on remote machine

I want to stop Sql server Agent Windows service.
I am using below code to stop or start the windows service. Which is I am doing sucessfully.
Expand|Select|Wrap|Line Numbers
  1.  ManagedComputer mc = default(ManagedComputer);
  2.       mc = new ManagedComputer();
  3.       Service svc = default(Service);
  4.       svc = mc.Services["SQLSERVERAGENT"];
  6.       if (svc.ServiceState == ServiceState.Running)
  7.       {
  8.           svc.Stop();
  9.           Response.Write (computerName + "Service Stoped");
  10.       }
  11.       else
  12.       {
  13.           svc.Start();
  14.           Response.Write (computerName + "Service Started");
  15.       }
But I want to stop that service from another machine in my network.
When I try to do that I am getting following error so please help me to remove that error.

Microsoft.SqlServer.Management.Smo.SmoException was unhandled by user code
HelpLink="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00. 1399.00&LinkId=20476"
Message="An exception occurred in SMO while trying to manage a service."
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComp uter.TryConnect()
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.get_Proxy()
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.get_Proxy()
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.ImplInitialize(String[] fields, OrderBy[] orderby)
at Microsoft.SqlServer.Management.Smo.Wmi.WmiSmoObjec t.Initialize()
at Microsoft.SqlServer.Management.Smo.Wmi.ServiceColl ection.GetObjectByName(String name)
at Microsoft.SqlServer.Management.Smo.Wmi.ServiceColl ection.get_Item(String name)
at TestService.tempFunc() in e:\Working Folder\BI_Server_Monitor.root\BI_Server_Monitor\BI _Server_Monitor\TestService.aspx.cs:line 37
at TestService.Page_Load(Object sender, EventArgs e) in e:\Working Folder\BI_Server_Monitor.root\BI_Server_Monitor\BI _Server_Monitor\TestService.aspx.cs:line 21
at System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException: System.UnauthorizedAccessException
Message="Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
at System.Runtime.InteropServices.Marshal.ThrowExcept ionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementScope.InitializeGuts(O bject o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementScope.Connect()
at Microsoft.SqlServer.Management.Smo.Wmi.ManagedComp uter.TryConnect()
Dec 18 '08 #1
7 7968
750 Expert 512MB
You need to specify login info and computer name
Secondly you require permissions for WMI calls to work on remote machines
HRESULT: 0x80070005 ="Errors originating in the core operating system. WMI may return this type of error because of an external failure, for example, DCOM security failure."
Look at the links given in this thread
Also look at ServiceController class
Dec 18 '08 #2
There is absolutely no problem with my code.
Problem is in the machine Which I am accessing.

I sure that there are some setting which are blocking my request for service object.
Please help me whether it is firewall or something else.

Dec 19 '08 #3
750 Expert 512MB
You need permission and admin account that is validated on the remote pc for WMI to work on remote PC.. Your local login wont work... You need the remote PC login credentials.. and that too of a admin ...
check this
Dec 19 '08 #4
I created a user with administrator rights.
Turned off the firewall.

Still no success...now I am getting furstrated...
Please help me..
I am using different code which is as follows:
Expand|Select|Wrap|Line Numbers
  1.  ConnectionOptions connection = new ConnectionOptions();
  2.                 connection.Username = "Test";
  3.                 connection.Password = "Test";
  4.                 connection.Authority = "Kerberos:administrator";
  6.                 ManagementScope scope = new ManagementScope("\\\\MachineName\\root\\CIMV2"
  7.                     , connection);
  9.                 //"\\\\ADJ-IND-01-0010\\root\\CIMV2"
  10.                 scope.Connect();
  12.                 ObjectQuery query = new ObjectQuery(
  13.                     "SELECT * FROM Win32_Service");
  15.                 ManagementObjectSearcher searcher =
  16.                     new ManagementObjectSearcher(scope, query);
  18.                 foreach (ManagementObject queryObj in searcher.Get())
  19.                 {
  20.                     Console.WriteLine("-----------------------------------");
  21.                     Console.WriteLine("Win32_Service instance");
  22.                     Console.WriteLine("-----------------------------------");
  23.                     Console.WriteLine("Caption: {0}", queryObj["Caption"]);
  24.                     Console.WriteLine("Description: {0}", queryObj["Description"]);
  25.                     Console.WriteLine("Name: {0}", queryObj["Name"]);
  26.                     Console.WriteLine("PathName: {0}", queryObj["PathName"]);
  27.                     Console.WriteLine("State: {0}", queryObj["State"]);
  28.                     Console.WriteLine("Status: {0}", queryObj["Status"]);
  29.                 }
  30.                 //Close();
  31.             }
  32.             catch (ManagementException err)
  33.             {
  34.                 //MessageBox.Show("An error occured while querying for WMI data: "
  35.                 //   + err.Message);
  36.             }
Giving me error

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
Dec 19 '08 #5
750 Expert 512MB
On the remote computer see to it that Remote Procedure Call (RPC)
and Remote Procedure Call (RPC) Locator services are enabled and running...
and secondly
Grant DCOM Remote Launch permissions

1. Click Start, click Run, type DCOMCNFG, and then click OK.
2. In the Component Services dialog box, expand Component Services, expand Computers, and then expand My Computer.
3. On the toolbar, click the Configure My Computer button.

The My Computer dialog box appears.
4. In the My Computer dialog box, click the COM Security tab.
5. Under Launch and Activate Permissions, click Edit Limits.
6. In the Launch Permission dialog box, follow these steps if your name or your group does not appear in the Groups or user names list:
1. In the Launch Permission dialog box, click Add.
2. In the Select Users, Computers, or Groups dialog box, add your name and the group in the Enter the object names to select box, and then click OK.
7. In the Launch Permission dialog box, select your user and group in the Group or user names box. In the Allow column under Permissions for User, select Remote Launch, and then click OK.
More Info
If all is working fine... you should be able to atleast read/loop through the service names....
Also you can go in for a client server application where local client executes starting and stopping services/processes... Doing it remotely you will compromise the remote system...
Dec 19 '08 #6
Thanx for the reply.
Dec 23 '08 #7
750 Expert 512MB
Welcome, do continue to post your queries on Bytes
Dec 23 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ravi | last post: by
3 posts views Thread by Lyle Fairfield | last post: by
21 posts views Thread by Kevin Swanson | last post: by
1 post views Thread by Jody Gelowitz | last post: by
reply views Thread by Suresh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.