powershell 入門_使用PowerShell入門的5個Cmdlet

powershell 入門

powershell 入門

image

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所具有的每個命令。

image

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 *

image

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 *

image

得到幫助 (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

獲取幫助獲取過程

image

獲得會員(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。

image

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

獲取流程| 獲得會員

image

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”}

image

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 入門

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279193.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279193.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279193.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Part 3: Services

介紹 在第3部分中,我們將擴展應用程序并啟用負載平衡。為此,我們必須在分布式應用程序的層次結構中提升一個級別:服務。 StackServices (你在這里)Container (涵蓋在第2部分中)關于服務 在分布式應用程序中,應用程序的不同部分被稱為“服務”…

mysql ldf文件太大_Linux_數據庫清除日志文件(LDF文件過大),清除日志: 復制代碼 代碼如 - phpStudy...

數據庫清除日志文件(LDF文件過大)清除日志:復制代碼 代碼如下:DECLARE LogicalFileName sysname,MaxMinutes INT,NewSize INTUSE szwzcheck -- 要操作的數據庫名SELECT LogicalFileName szwzcheck_Log, -- 日志文件名MaxMinutes 10, -- Limit on time allowed to …

emwin之錯誤使用控件函數導致死機現象

2018-10-15 導致死機的代碼示例如下 1 /**2 * brief widget ID define3 * {4 */5 6 #define ID_WINDOW_0 (GUI_ID_USER 0x00)7 #define ID_TEXT_0 (GUI_ID_USER 0x01)8 #define ID_TEXT_1 (GUI_ID_USER …

diy感應usb攝像頭拍照_DIY無線感應充電器

diy感應usb攝像頭拍照Courtesy of Instructables user Inducktion shares a very detailed tutorial on how to build a wireless power charger. He explains the impetus behind the project: 由Instructables用戶提供Inducktion分享了有關如何構建無線電源充電器的非常詳細…

ubuntu7.10安裝到3D開啟

累了好幾天,重裝了十幾遍終于把ubuntu7.10搞定到了我自認為完美的狀態了。現在總結一下安裝過程(按操作順序記錄):1.在xp下不管用pqmajac還是其他硬盤分區工具分出10G的空余分區來(實驗階段10G嘗試下)&…

初學者對python的認識_Python初學者列表,python,初識

1.認識列表列表可以放入所有我們目前學習過的數據類型,甚至包括列表2.有關列表的方法、內置函數(設列表的名稱為list)向列表中添加元素:append():list.append(要添加的元素),注意每次只能添加一個元素,被添加的元素自動…

常用模塊之 time,datetime,random,os,sys

time與datetime模塊 先認識幾個python中關于時間的名詞: 時間戳(timestamp):通常來說,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。我們運行“type(time.time())”,返回的是float類型。1970年之前的日期無法以此表…

使用aSpotCat控制您的Android應用權限

Viewing the permissions of each installed Android app requires digging through the Manage Applications screen and examining each app one by one — or does it? aSpotCat takes an inventory of the apps on your system and the permissions they require. 要查看每…

xtrabackup備份mysql“ib_logfile0 is of different”錯誤分析

今天用xtrabackup工具完整備份mysql數據庫的時候出現“./ib_logfile0 is of different”錯誤,具體的日志信息如下: 我第一時間查詢了百度和谷歌都沒有找見相對應的答案。決定從錯誤日志入手,上面的日志提示說:mysql數據庫inondb的日志文件的大…

java socket 報文解析_java socket解析和發送二進制報文工具(附java和C++轉化問題)

解析:首先是讀取字節:/*** 讀取輸入流中指定字節的長度* * 輸入流**paramlength 指定長度*return指定長度的字節數組*/public static byte[] readBytesFromTo(byte[] buffer, int from, intlength) {byte[] sub new byte[length];int cur 0;for (int i from; i < length …

Ubuntu防火墻:ufw

原始linux的防火墻是iptables&#xff0c;以為過于繁瑣&#xff0c;各個發行版幾乎都有自己的方案; ubuntu下的防火墻是ufw[ubuntu fireward的縮寫]&#xff0c;centos的防火墻是fireward ubuntu下&#xff1a; 啟用或者關閉防火墻 sudo ufw enable|disable sudo ufw default d…

如何使自己的不和諧機器人

Discord has an excellent API for writing custom bots, and a very active bot community. Today we’ll take a look at how to get started making your own. Discord具有出色的用于編寫自定義機器人的API&#xff0c;以及非常活躍的機器人社區。 今天&#xff0c;我們將探…

?css3屬性選擇器總結

css3屬性選擇器總結 &#xff08;1&#xff09;E[attr]只使用屬性名&#xff0c;但沒有確定任何屬性值 <p miaov"a1">111111</p> <p miaov"a2">111111</p> p[miaov]{background: red;} /*所有屬性為miaov的元素都會被背景變紅&a…

java復合賦值運算符_Java 之復合賦值運算符

1.引入問題切入正題&#xff0c;看下面代碼&#xff0c;結果應該是怎么樣的public class App{public static void main( String[] args ){byte a1 ;int b 10;a ab;System.out.println(a);ab;System.out.println(a);}}這段代碼的執行結果是什么&#xff1f;&#xff1f;2. 執行…

程序代碼初學者_初學者:如何使用熱鍵在Windows中啟動任何程序

程序代碼初學者Assigning shortcut keys to launch programs in Windows is probably one of the oldest geek tricks in the book, but in true geek fashion we are going to show you how to do it in Windows 8. 分配快捷鍵以在Windows中啟動程序可能是本書中最古老的怪胎技…

stevedore——啟用方式

2019獨角獸企業重金招聘Python工程師標準>>> setuptools維護的入口點注冊表列出了可用的插件&#xff0c;但是并沒有為最終用戶提供使用或啟用的方法。 下面將描述用于管理要使用的擴展集的公共模式。 通過安裝方式啟用 對于許多應用程序&#xff0c;僅僅安裝一個擴…

java 重置定時器_可重置Java定時器

我想有一個java.utils.Timer與一個可重置時間在java.I需要設置一次off事件發生在X秒。如果在創建定時器的時間和X秒之間沒有發生任何事情&#xff0c;則事件會正常發生。然而&#xff0c;如果在X秒之前&#xff0c;我決定該事件應該發生在Y秒后&#xff0c;然后我想要能夠告訴定…

C# -- 文件的壓縮與解壓(GZipStream)

文件的壓縮與解壓 需引入 System.IO.Compression; 1.C#代碼&#xff08;入門案例&#xff09; 1 Console.WriteLine("壓縮文件...............");2 using (FileStream fr File.OpenRead("d:\\test.txt"))3 {4 …

win7屏保文件.scr_如何將屏保添加到Ubuntu 12.04

win7屏保文件.scrUbuntu 12.04 doesn’t ship with any screen savers, just a black screen that appears when your system is idle. If you’d rather have screensavers, you can swap gnome-screensaver for XScreenSaver. Ubuntu 12.04沒有附帶任何屏幕保護程序&#xff…

簡單讀寫XML文件

IPAddress.xml 文件如下&#xff1a; <?xml version"1.0" encoding"utf-8"?><IP><IPAddress>192.168.0.120</IPAddress></IP> 在 Form 窗體(讀取XML配置.Designer.cs)中有如下控件&#xff1a; 代碼 privateSystem.Wind…