尝试在调用WMI方法时理解对foreach循环的需求 [英] Trying to make sense of the need for foreach loop when Invoking WMI Method

查看:42
本文介绍了尝试在调用WMI方法时理解对foreach循环的需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始熟悉WMI,对于开始尝试实施重新启动远程计算机的方法的初学者.

I am starting to get myself familiar with WMI and for starters trying to implement a method that reboots a remote computer.

从许多代码示例中我注意到的是,一个foreach循环用于调用单个管理对象的方法/显示值(例如: http://social.msdn.microsoft.com/Forums/vstudio/zh-CN/118ac7b1-c786-4c56-9eb0-24fc96e31548/win32shutdown?forum=csharpgeneral ).现在,我了解到ManagementClass的Get()方法返回对象的集合.但是当我们的管理范围 仅定义了具有单个远程计算机路径的单个对象?根据我的理解,对象集合"意味着我们正在使用多台机器.

What I notice from a lot of code examples is that a foreach loop was used to InvokeMethod/Display value for single Management Object(eg: http://social.msdn.microsoft.com/Forums/vstudio/en-US/118ac7b1-c786-4c56-9eb0-24fc96e31548/win32shutdown?forum=csharpgeneral), . Now I understand that ManagementClass' Get() method returns a collection of objects. but is that necessary when our ManagementScope is only a single object with a single remote computer path defined? From my understanding, a Collection of Objects would imply that we are tackling with multiple machines.

无用的是MSDN的初始代码示例( http://msdn.microsoft.com/zh-CN/library/ms257364(v=vs.80).aspx ),未将foreach循环用于创建"方法,在 我遵循了它,并在尝试调用"Win32Shutdown"时遗忘了自己的头.直到我在这里找到利用foreach循环的这些代码,它才起作用.

What doesn't help is MSDN's initial code example(http://msdn.microsoft.com/en-us/library/ms257364(v=vs.80).aspx), did not use a foreach loop for a "Create" method, in which I followed and leave me scratching my head when trying to invoke "Win32Shutdown" and it doesn't work, until I find these codes that utilize the foreach loop here.

推荐答案

如您所述,ForEach循环仅用于遍历对象集合.

As you mentioned, ForEach loop is only for iterating thru the collection of objects.

在第二个MSDN链接中,他们展示了如何创建Win32_Process对象来直接执行操作系统命令,该操作不需要迭代,而在第一个链接中,他们正在调用 GetInstances()方法将仅返回一个集合,即使只有一个对象要返回,也会将其添加到集合中并返回.因此,您需要一个foreach循环来遍历集合并进行必要的操作 操作.

In the second MSDN link they are showing how to create a Win32_Process object to directly execute a operating system command, which doesn't require a iteration, where as in the first link, they are calling the GetInstances() method which will return only a collection, even if there is only one object going to returned, it will be added to a collection and returned. Hence you need a foreach loop to iterate thru the collection and do the necessary operation.

您的问题的答案在第一个链接的答复中.

The Answer for your question is there in the one of the replies from the first link.

System.Diagnostics.Process.Start("Shutdown",-s -t 10");  

您可以将Shutdown命令与/m \\ computername标志一起使用来关闭远程计算机, 如果您有权这样做.

you can use the Shutdown command with /m \\computername flag for shutting down a remote computer, if u have permissions to do so.

如果您想通过WMI实现这一目标,请尝试这样做.

IF you want to achieve that via WMI, you try like this.

ManagementClass mcWin32 = new ManagementClass("Win32_Process");
    
mcWin32.Get();

ManagementBaseObject Mos = mcWin32.InvokeMethod("Create", new string[] { "shutdown /m \\computername" }) as ManagementBaseObject;

有关如何使用关机"命令的更多信息,请打开命令提示符(cmd)

For more info on how to use the Shutdown command, open the command prompt (cmd) 

,然后输入关掉/? "命令以获取选项列表以及如何使用它.

and type the " shutdown /? " command to get the list of options and how to use it.

希望这会有所帮助.


这篇关于尝试在调用WMI方法时理解对foreach循环的需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆