无法从我的网络应用程序远程启动/停止Windows服务

时间:2017-01-26 04:46:16

标签: c# .net service wmi

我尝试从我的网络应用程序启动/停止/重新启动Windows服务。以下代码是Start函数。如果使用名称&提供密码,即取消注释行:

connectoptions.Username = DomainUsername;
connectoptions.Password = Password;

如果我在其他远程服务器上运行它,它不起作用。它没有开始/停止服务。

#region StartService
public int StartService(string ServiceName, string IPAddress)
{
    int serviceflag = 0;
    try
    {
        //Assign the user name and password of the account to ConnectionOptions object
        //which have administrative privilege on the remote machine.
        ConnectionOptions connectoptions = new ConnectionOptions();
        connectoptions.Impersonation = ImpersonationLevel.Impersonate;
        //connectoptions.Username = DomainUsername;
        //connectoptions.Password = Password;

        //IP Address of the remote machine

        ManagementScope scope = new ManagementScope(@"\\" + IPAddress + @"\root\cimv2", connectoptions);
        //ManagementScope scope = new ManagementScope();
        scope.Connect();

        //Define the WMI query to be executed on the remote machine
        SelectQuery query0 = new SelectQuery("select * from Win32_Service  where name = '" + ServiceName + "'");

        object[] methodArgs = { ServiceName,  null, null, 0 };
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query0))
        {
            foreach (ManagementObject service in searcher.Get())
            {
                if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
                {
                    serviceflag = 1;
                }
                else
                {
                    service.InvokeMethod("StartService", methodArgs);
                }
            }
        }

        return serviceflag;
    }
    catch (Exception ex)
    {
        serviceflag = 2;
        return serviceflag;
    }
}
#endregion

还有其他功能,例如 IsServiceInstalled (显示服务未安装在应用中)和 IsServiceRunning (显示服务是否正在运行)。这两个功能都运行正常。

#region IsServiceRunning
public bool IsServiceRunning(string ServiceName0, string IPAddress)
{
    try
    {
        int serviceexistsflag1 = 0;
        //Assign the user name and password of the account to ConnectionOptions object
        //which have administrative privilege on the remote machine.
        ConnectionOptions connectoptions = new ConnectionOptions();
        connectoptions.Impersonation = ImpersonationLevel.Impersonate;
        //connectoptions.Username = DomainUsername;
        //connectoptions.Password = Password;

        //IP Address of the remote machine

        ManagementScope scope = new ManagementScope(@"\\" + IPAddress + @"\root\cimv2", connectoptions);
        //ManagementScope scope = new ManagementScope();
        scope.Connect();
        //Define the WMI query to be executed on the remote machine
        SelectQuery query0 = new SelectQuery("select * from Win32_Service  where name = '" + ServiceName0 + "'");

        object[] methodArgs = { ServiceName0, null, null, 0 };
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query0))
        {
            foreach (ManagementObject service in searcher.Get())
            {
                if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
                {
                    return true;
                }
                else
                {
                    //service.InvokeMethod("StartService", methodArgs);
                }
            }
        }
        return false;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}
#endregion

那么Start函数会出现什么问题?

0 个答案:

没有答案