我正在嘗試編寫一個程序,該程序將連接到遠程服務器並顯示服務的某種狀態。該服務也由我們(我的公司)編寫。無法在計算機上打開服務控制管理器
爲此我寫了一個控制檯應用程序和代碼是
static void Main(string[] args)
{
ConnectionOptions options = new ConnectionOptions();
options.Password = "mypassword";
options.Username = "Administrator";
options.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
ManagementScope scope =
new ManagementScope(
"\\\\ip_of_the_server\\root\\cimv2", options);
scope.Connect();
ServiceController svc = new ServiceController("My_Service_Name", "ip_of_the_server");
var status = svc.Status.ToString();
Console.WriteLine(svc.DisplayName + " : " status);
}
但我不能使它發揮作用。我得到的錯誤是:
Cannot open Service Control Manager on computer 'ip_of_the_server'. This operation might require other privileges.
內部異常:「訪問被拒絕」。
堆棧跟蹤:
at System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String machineName, Int32 serviceControlManaqerAccess)
at System.ServiceProcess.ServiceController.GetDataBaseHandleWithConnectAccess()
at System.ServiceProcess.ServiceController.GenerateNames()
at System.ServiceProcess.ServiceController.get_ServiceName()
at System.ServiceProcess.ServiceController.GenerateStatus()
at System.ServiceProcess.ServiceController.get_Status()
at ServiceConsole.Program.Main(String[] args) in c:\Users\Kandroid\Documents\Visual Studio 2013\Projects\ServiceConsole\ServiceConsole\Program.cs:line 33
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
任何想法如何解決呢?
使用具有正確權限的帳戶? – rene 2014-08-27 11:29:47
:)是offcourse。我正在使用'管理員'。以及我需要什麼樣的權限? – kandroid 2014-08-27 11:31:09
您設置了一個管理範圍,但是我沒有看到那個人去了您的ServiceController中的任何地方,對吧? – rene 2014-08-27 11:32:12