powershell 使用_极客学院:在PowerShell中使用集合

时间:2024-03-18 17:40:24
powershell 使用_极客学院:在PowerShell中使用集合

powershell 使用

powershell 使用_极客学院:在PowerShell中使用集合

Since PowerShell is based on the .Net Framework and incorporates various other technologies like WMI and CIM, there is always more than one way to accomplish the same thing. Come join us for this short post where we learn how to choose the best method to accomplish our tasks.

由于PowerShell基于.Net Framework并结合了WMI和CIM等各种其他技术,因此,完成同一件事总是有不止一种方法。 快来加入我们的这篇简短文章,我们将学习如何选择最佳方法来完成任务。

Be sure to read the previous articles in the series:

确保阅读本系列中的先前文章:

And stay tuned for the rest of the series all week.

并继续关注本系列的其余部分。

使用批处理Cmdlet (Using Batch Cmdlets)

Earlier on in the series, when we introduced you to the pipeline, we showed you how you can take the objects that one cmdlet outputs and pass them as input to another cmdlet using something like this:

在本系列的较早版本中,当我们向您介绍管道时,我们向您展示了如何使用一个cmdlet输出的对象,并使用如下方法将它们作为输入传递给另一cmdlet:

Get-Process -Name notepad | Stop-Process

Get-Process-名称记事本| 停止程序

This would kill any process with the name “notepad”. But you are probably wondering how PowerShell is able to kill every instance of notepad with a single command. The answer lies within the help of the Stop-Process cmdlet.

这将杀死名称为“ notepad”的任何进程。 但是您可能想知道PowerShell如何使用一个命令杀死每个记事本实例。 答案在于Stop-Process cmdlet的帮助下。

help Stop-Process –Full

帮助Stop-Process –Full

powershell 使用_极客学院:在PowerShell中使用集合

If you look at the last line of code in the syntax section, you can see that the InputObject parameter accepts an object of type Process[], and whenever you see a type followed by two square brackets like that it means that the parameter accepts one or more of the preceding type. In this case, it accepts one or more process objects. Technically, we would say that the InputObject cmdlet accepts a process array. Whenever you have a cmdlet that supports batch operations in this manner, use it. This is choice number one.

如果您在语法部分的最后一行代码中看到,则可以看到InputObject参数接受类型为Process []的对象,并且每当看到一个类型后跟两个方括号,则表示该参数接受一个或更多上述类型。 在这种情况下,它接受一个或多个过程对象。 从技术上讲,我们可以说InputObject cmdlet接受一个process array 。 只要具有支持这种方式的批处理操作的cmdlet,就可以使用它。 这是第一选择。

使用WMI (Using WMI)

Although WMI isn’t the best piece of technology to ship from the Microsoft HQ, it does come in at second on the list of how to work with collections of objects. We could easily get a list of running processes from the Win32_Process class like so:

尽管WMI并不是Microsoft总部提供的最好的技术,但它确实在如何处理对象集合的列表中排在第二位。 我们可以像这样从Win32_Process类中轻松获取正在运行的进程的列表:

Get-WmiObject –Class Win32_Process

Get-WmiObject – Win32_Process类

powershell 使用_极客学院:在PowerShell中使用集合

Since the WMI query returns its own kind of object, you will need to look for a method that can stop the process, so lets pipe that to Get-Member.

由于WMI查询返回其自身的对象类型,因此您将需要寻找一种可以停止该过程的方法,因此让其通过管道传递到Get-Member。

Get-WmiObject –Class Win32_Process | Get-Member

Get-WmiObject – Win32_Process类| 获得会员

powershell 使用_极客学院:在PowerShell中使用集合

Looks like the closest thing to stop is the terminate method, so that must be the one. To call a method on a WMI Object, you simply pipe it to Invoke-WmiMethod and specify the name of the method.

看起来最要停止的是终止方法,所以必须是那个。 要在WMI对象上调用方法,只需将其通过管道传递到Invoke-WmiMethod并指定方法的名称。

Get-WmiObject -Class Win32_Process -Filter “name=’notepad.exe'” | Invoke-WmiMethod -Name Terminate

Get-WmiObject-类Win32_Process-过滤器“ name ='notepad.exe'” | 调用WmiMethod-名称终止

powershell 使用_极客学院:在PowerShell中使用集合

Great, that did the trick. Whenever you get a ReturnValue of 0 in WMI, just remember that the command executed successfully.

太好了。 只要在WMI中将ReturnValue设置为0,就请记住该命令已成功执行。

枚举 (Enumeration)

Failing the other two methods, if you have to do something to a bunch of objects you can enumerate the entire object and act on each individual object. First you have to find the method you would use to stop a single process.

如果没有其他两种方法,则如果必须对一堆对象执行某些操作,则可以枚举整个对象并对每个对象进行操作。 首先,您必须找到用于停止单个进程的方法。

Get-Process | Get-Member -MemberType Method

获取流程| Get-Member -MemberType方法

powershell 使用_极客学院:在PowerShell中使用集合

Perfect, it looks like we can use the Kill method and then pipe into ForEach-Object to kill them all.

完美,看起来我们可以使用Kill方法,然后通过管道将其插入ForEach-Object中以杀死所有对象。

Get-Process -Name notepad | ForEach-Object -Process {$_.Kill()}

Get-Process-名称记事本| ForEach-Object -Process {$ _。Kill()}

powershell 使用_极客学院:在PowerShell中使用集合

Here we took all the process objects that Get-Process returned and passed them to the ForEach-Object cmdlet. Much like the Where-Object cmdlet, we represented each object in the pipeline with $_ which we were able to call to the Kill() method on. With all that said and done, enumerating a collection is much slower than the above methods and should only be used as a last result.

在这里,我们获取了Get-Process返回的所有流程对象,并将它们传递给ForEach-Object cmdlet。 就像Where-Object cmdlet一样,我们用$ _表示管道中的每个对象,我们可以在它们上调用Kill()方法。 综上所述,枚举集合比上述方法慢得多,并且只能用作最后结果。

摘要 (Summary)

Choice One

选择一

Get-Process -Name notepad | Stop-Process

Get-Process-名称记事本| 停止程序

Choice Two

选择二

Get-WmiObject -Class Win32_Process -Filter “name=’notepad.exe'” | Invoke-WmiMethod -Name Terminate

Get-WmiObject-类Win32_Process-过滤器“ name ='notepad.exe'” | 调用WmiMethod-名称终止

Choice Three

选择三

Get-Process -Name notepad | ForEach-Object -Process {$_.Kill()}

Get-Process-名称记事本| ForEach-Object -Process {$ _。Kill()}

That’s all for this time folks, see you next week for more PowerShell fun.

这就是这些时间的全部,下周再见,以获取更多PowerShell乐趣。

翻译自: https://www.howtogeek.com/138974/geek-school-working-with-collections-in-powershell/

powershell 使用