如何使用C#在远程计算机上启动和停止Windows服务 [英] How to start and stop windows Services on remote machine using C#
问题描述
最初我用ServiceController创建了win应用程序来重启远程windows服务,我成功重启远程windows服务。
然后我在windows服务中实现相同的代码来重启远程窗口服务,但它在计算机'机器名'上提供了一些错误消息,例如"无法打开'服务名'。
在serviceProcessInstaller属性中,我给出了类似Account = LocalSystem
注意我也检查我本地计算机上的服务管理器并将其连接到远程计算机,我可以重新启动服务。
然后为什么它不允许从Windows服务重启服务
请建议我....
thnaks
检查这个......
<例如:
使用System;
使用System.Management;
命名空间WMITest
{
class clsMain
{
static void Main( string [] args)
{
GetWMIStats();
Console.ReadLine();
}
static void GetWMIStats()
{
//远程计算机的连接凭证 -
//登录帐户 必须具有远程机器的管理员访问权限$
ConnectionOptions oConn = new ConnectionOptions();
$
//oConn.Username =" username" ;;
//oConn.Password =" password";
$
string remoteMachineName =" localhost";
$
System.Management.ManagementScope oMs = new System.Management.ManagementScope( " \\\\" + remoteMachineName,oConn);
//使用此特定WQL查询传递要查询的服务名称---
System.Management.RelatedObjectQuery oQuery =
new System.Management.RelatedObjectQuery(" Win32_Service.Name ='Alerter'");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
$
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach(管理对象oReturn in oReturnCollection)
{
Console.WriteLine(" Name:" + oReturn [" Name"]。ToString());
$
//对相应的每个属性重复上述过程
//可以读取的对象有关详细信息,请参阅MSDN上的WMI文档。
}
}
}
}
&NBSP;
Initially I created win application with ServiceController to restart remote windows service, I success to restart remote windows service.
then I implement same code in windows service to restart remote windows service, but it gives some error message like "can not open 'service name' on computer 'machine name'.
In serviceProcessInstaller properties i given like Account = LocalSystem
Note i also check the Service Manager on my local computer and connect it to the remote computer, there i able to restart the service.
then why its not allowing to restart the service from windows service
Please suggest me....
thnaks
check this...
example:
using System;
using System.Management;
namespace WMITest
{
class clsMain
{
static void Main(string[] args)
{
GetWMIStats();
Console.ReadLine();
}
static void GetWMIStats()
{
//Connection credentials to the remote computer -
//logged in account must have adminstrator access for a remote machine
ConnectionOptions oConn = new ConnectionOptions();
//oConn.Username = "username";
//oConn.Password = "password";
string remoteMachineName="localhost";
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" +remoteMachineName , oConn);
// pass the name of the service to query with this particular WQL query---
System.Management.RelatedObjectQuery oQuery =
new System.Management.RelatedObjectQuery("Win32_Service.Name='Alerter'");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine("Name : " + oReturn["Name"].ToString());
// Repeat the above process for each property of the respective
// object that can be read. See the WMI documentation at MSDN for details.
}
}
}
}
这篇关于如何使用C#在远程计算机上启动和停止Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!