Access Sql server Agent Windows service on remote machine

  • I am working on Monitering tool which schedules jobs. That I am doing sucessfulling using SMO but Now I first want whether Sql Server agent is running or not.

    While doing that I want to start Sql server Agent Windows service.

    I am using below code to stop or start the windows service. Which is I am doing sucessfully.

    ManagedComputer mc = default(ManagedComputer);

    mc = new ManagedComputer();

    Service svc = default(Service);

    svc = mc.Services["SQLSERVERAGENT"];

    if (svc.ServiceState == ServiceState.Running)

    {

    svc.Stop();

    Response.Write (computerName + "Service Stoped");

    }

    else

    {

    svc.Start();

    Response.Write (computerName + "Service Started");

    }

    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."

    Source="Microsoft.SqlServer.Smo"

    StackTrace:

    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))"

    Source="mscorlib"

    StackTrace:

    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()

    InnerException:

    Edit / Delete

  • which user have you logged on as on the other machine. Access denied is the key message here

    Your code really wants to be expanded as you are only checking for stopped or running, there are other states that could be set which would cause the code to fail. Check out the C# ServiceController class for more info

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • I am able to create object of 'ManagedComputer' on local as no need to 'user name' and 'passward'.

    Using sql server VS 2005. I can connect to database on different machine.

    but When I try to create service object

    i.e.

    ManagedComputer mc = default(ManagedComputer);

    mc = new ManagedComputer("RemoteMAchineName", "Login","Passward");

    Service svc = default(Service);

    svc = mc.Services["SQLSERVERAGENT"]; <--- Error

    I am not user which user name and password it takes or is there any other way.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply