powershell 入门
PowerShell is quickly becoming the preferred scripting language and CLI of Power Users as well as IT Pros. It’s well worth learning a few commands to get you started, so we’ve got 5 useful cmdlets for you to learn today.
PowerShellSwift成为高级用户和IT专业人员的首选脚本语言和CLI。 值得学习一些命令以使您入门非常值得,因此,我们今天有5个有用的cmdlet供您学习。
获取命令 (Get-Command)
The Get-Command is one of the most useful cmdlets in the whole of PowerShell, as it will help you getting to grips with PowerShell by letting you search for certain cmdlets. Using Get-Command on its own is admittedly not very useful as its just going to spit out every command that PowerShell has.
Get-Command是整个PowerShell中最有用的cmdlet之一,因为它可以通过搜索某些cmdlet来帮助您掌握PowerShell。 单独使用Get-Command并不是很有用,因为它只会吐出PowerShell所具有的每个命令。
But from this we can see that that objects that PowerShell outputs have both a Name and a ModuleName property. Using this information we can fine grain our search, by searching for cmdlets that contain certain terms. For example if I wanted to find all cmdlets that contain the word “IP”, I could do this:
但是从中我们可以看到PowerShell输出的对象同时具有Name和ModuleName属性。 使用此信息,我们可以通过搜索包含某些术语的cmdlet来细化搜索范围。 例如,如果我想查找所有包含单词“ IP”的cmdlet,则可以这样做:
Get-Command –Name *IP*
Get-Command –名称* IP *
As you can see we still get quite a few results, our next option is to search within a specific module. In our case i will choose the NetTCPIP module.
如您所见,我们仍然可以获得很多结果,我们的下一个选择是在特定模块内搜索。 在我们的情况下,我将选择NetTCPIP模块。
Get-Command –Module NetTCPIP –Name *IP*
获取命令–模块NetTCPIP –名称* IP *
得到帮助 (Get-Help)
Once you have found the cmdlet you are looking for using Get-Command, you are going to want to know the syntax and how you can use that specific cmdlet. This is where Get-Help comes in, if you have ever used the command line in Windows you probably did something like this:
找到要使用Get-Command查找的cmdlet后,您将想要了解语法以及如何使用该特定cmdlet。 这是获取帮助的地方,如果您曾经在Windows中使用过命令行,则可能会执行以下操作:
ipconfig /?
ipconfig /?
Well that doesn’t work in PowerShell, this is because in PowerShell a space is used to separate a command from its parameters. So instead we use Get-Help and pass a cmdlets name to Get-Help as a parameter.
嗯,这在PowerShell中不起作用,这是因为在PowerShell中,空格用于将命令与其参数分开。 因此,我们改为使用Get-Help并将cmdlet名称作为参数传递给Get-Help。
Get-Help Get-Process
获取帮助获取过程
获得会员(Get-Member)
Get-Member allows us to get information about the objects that a cmdlets returns. The catch with get-member, is that it relies on PowerShell’s pipeline feature, to demonstrate this, we will can use the Get-Process cmdlet.
Get-Member允许我们获取有关cmdlet返回的对象的信息。 get-member的不足之处在于,它依赖于PowerShell的管道功能,对此进行演示,我们将可以使用Get-Process cmdlet。
As you can see PowerShell’s output shows us some of the properties, which you can see at the top of each column. The first problem is that, while those are the properties you might be looking for most of the time, there are still more of them. The second problem is that it doesn’t show any methods that we are able to call on the object. To see the methods and properties we can pipe our output to Get-Member, like so:
如您所见,PowerShell的输出向我们显示了一些属性,您可以在每一列的顶部看到这些属性。 第一个问题是,尽管大多数时候这些属性是您可能一直在寻找的,但仍有更多的属性。 第二个问题是它没有显示我们可以在对象上调用的任何方法。 要查看方法和属性,我们可以将输出通过管道传递到Get-Member,如下所示:
Get-Process | Get-Member
获取流程| 获得会员
While it may mean nothing to you right now, you will sooner or later need to use Get-Member, and the sooner you learn to use it the better. As an example, using the information from the output we could do something like:
尽管这可能对您现在没有任何意义,但是您迟早需要使用Get-Member,并且越早学会使用它就越好。 例如,使用输出中的信息,我们可以执行以下操作:
Start-Process notepad.exe $NotepadProc = Get-Process -Name notepad $NotepadProc.WaitForExit() Start-Process calc.exe
Start-Process notepad.exe $ NotepadProc =获取进程-Name记事本$ NotepadProc.WaitForExit()Start-process calc.exe
That script will launch notepad, it then assigns output of “Get-Process –Name notepad” to the $NotepadProc variable, then we call the WaitForExit method on $NotepadProc which causes the script to pause until you close notepad, once you have closed notepad then the calculator will launch.
该脚本将启动记事本,然后将“ Get-Process –Name notepad”的输出分配给$ NotepadProc变量,然后在$ NotepadProc上调用WaitForExit方法,这将导致脚本暂停,直到您关闭记事本为止。然后计算器将启动。
$ _(当前管道对象) ($_(Current Pipeline Object))
While not exactly a cmdlet, it is one of the most used special variables in PowerShell. The official name for $_ is “the current pipeline object” . It is used in script blocks, filters, the process clause of functions, where-object, foreach-object and switches. However it is easier to explain with an example, which brings us to our next and final cmdlet, Where-Object.
虽然不完全是cmdlet,但它是PowerShell中最常用的特殊变量之一。 $ _的正式名称是“当前管道对象”。 它用于脚本块,过滤器,函数的处理子句,where对象,foreach对象和开关。 但是,通过示例更容易解释,该示例将我们带入下一个也是最后一个cmdlet Where-Object。
对象在哪里 (Where-Object)
Where-Object does exactly what it sounds like, it selects an object based on whether it meets a certain criteria. This will bring together $_, and the properties we can see using Get-Member. To demonstrate this, we will pipe the output of Get-Process into the Where-Object cmdlet.
Where-Object完全按照听起来的样子运行,它根据是否满足特定条件来选择对象。 这将集合$ _和我们可以使用Get-Member看到的属性。 为了说明这一点,我们将把Get-Process的输出传递到Where-Object cmdlet中。
Get-Process | Where-Object {$_.Name –eq “iexplore”}
获取流程| 哪里对象{$ _。Name –eq“ iexplore”}
So what’s going on here you ask? Well the first thing we are doing is getting a list of processes on our computer and passing the output (using the | character) to our Where-Object cmdlet, which takes a script block as a parameter. The script block (defined by the curly braces) instructs the Where-Object cmdlets to only select objects where their name parameter is equal to “iexplore”, and so we only get a list of the IE instances that are running. That’s all there is to it, have fun!
那你问这是怎么回事? 好吧,我们要做的第一件事是获取计算机上的进程列表,并将输出(使用|字符)传递给Where-Object cmdlet,该cmdlet将脚本块作为参数。 脚本块(由花括号定义)指示Where-Object cmdlet仅选择其名称参数等于“ iexplore”的对象,因此,我们仅获得正在运行的IE实例的列表。 这就是全部,玩得开心!
翻译自: https://www.howtogeek.com/114344/5-cmdlets-to-get-you-started-with-powershell/
powershell 入门