我如何将其扩展到远程计算机(获取 Windows 服务)?
我有以下代码来获取某台计算机上运行的所有 Windows 服务。如果这台机器是我正在运行的机器,它工作正常,但如果它是远程机器,我会收到“访问被拒绝”异常,因为我没有传递用户名/密码。
我的问题是,如何/在哪里传递用户名/密码?
static void Main(string[] args)
{
bool filter;
string machineName = "mymachine";
string objPath = String.Format("\\\\{0}\\root\\cimv2:Win32_Service",machineName);
ManagementClass servicesMC = new ManagementClass(new ManagementPath(objPath));
Dictionary<string, string> ServicesDict = new Dictionary<string, string>();
List<string> Aggregate = new List<string>();
foreach (ManagementObject service in servicesMC.GetInstances())
{
//Console.WriteLine(service.GetPropertyValue("Name"));
}
I have the following code that fetches all the Windows Services running on a certain machine. If the machine is the one I am running, it works fine, but if its a remote machine, I get a 'Access Denied' exception because I am not passing in the username/password.
My question is, how/where can I pass in the username/password?
static void Main(string[] args)
{
bool filter;
string machineName = "mymachine";
string objPath = String.Format("\\\\{0}\\root\\cimv2:Win32_Service",machineName);
ManagementClass servicesMC = new ManagementClass(new ManagementPath(objPath));
Dictionary<string, string> ServicesDict = new Dictionary<string, string>();
List<string> Aggregate = new List<string>();
foreach (ManagementObject service in servicesMC.GetInstances())
{
//Console.WriteLine(service.GetPropertyValue("Name"));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有运行这个,因为我现在不在网络上,但这应该可以工作:
I haven't run this because I am not on a network right now but this should work: