本課的視頻教程: 請看置頂留言
Powershell 企業(yè)應(yīng)用 - 第02章 Powershell的幫助系統(tǒng)_嗶哩嗶哩_bilibili
本章將為大家介紹在使用Powershell的時(shí)候,如何更有效率的得到幫助信息。
在使用Powershell的過程中,我們可以從以下四個(gè)來源獲得在線幫助。
- Powershell的Get-Help命令。
- 搜索引擎。
- Microsoft Docs (https://docs.microsoft.com/zh-cn/powershell/)
- .NET Framework 類庫
接下來,我們將對(duì)這四個(gè)幫助信息的來源進(jìn)行逐一的介紹。
Powershell的Get-Help命令
默認(rèn)安裝的Powershell并沒有包含幫助文件,雖然我們也可以通過Get-Help獲得幫助信息,但這個(gè)幫助信息是系統(tǒng)自動(dòng)生成的、只有語法的簡要信息(圖一)。對(duì)于我們理解命令的幫助不大。

不包含體積龐大的幫助文檔,這樣做的好處是減少了Powershell的體積,更好的適應(yīng)各類的硬件平臺(tái),尤其是物聯(lián)網(wǎng)設(shè)備的硬件往往是受限的。
所以,我們首先要做的是使用Update-Help來進(jìn)行幫助文件的更新。
Update-Help更新本地幫助文檔的步驟
-
使用管理員運(yùn)行Powershell,否則會(huì)有讀寫權(quán)限不足的報(bào)錯(cuò)信息。
開始-> 輸入Powershell -> 選擇使用管理員(圖二)。
圖二 -
Update-Help更新幫助。
輸入U(xiǎn)pdate-Help命令,開始更新本地幫助。
在Powershell的模塊,是幫助的最小單元。不帶參數(shù)的Update-Help會(huì)更新所有的模塊,如果我們想查詢的只是某個(gè)命令的幫助信息,可以使用-module來更新命令所在的模塊的幫助文件,操作如下:
-
首先使用Get-Command來查看命令所屬的模塊(圖三)。
圖三 -
使用Update-Help -Module(圖四)。
圖四
Get-Help所顯示的幫助信息說明
當(dāng)我們使用Get-Help來查詢命令的幫助文檔時(shí),可能會(huì)得到以下信息(Get-Help返回的內(nèi)容根據(jù)傳入的參數(shù)的不同而不同)。
下面是對(duì)于幫助文檔的所有字段的解釋。
名稱
Get-Service
解釋: 名稱部分顯示的是幫助文件所對(duì)應(yīng)的命令。
摘要
Gets the services on a local or remote computer.
解釋:用一句話對(duì)命令進(jìn)行簡要說明。
語法 >?#命令的使用方法和參數(shù)
???#[-參數(shù) ] 中的參數(shù)不是必選項(xiàng)。
???#<System.String> 尖括號(hào)中的內(nèi)容表示了某個(gè)數(shù)據(jù)類型的對(duì)象。
???#<System.String[]> 這里的中括號(hào)代表數(shù)組,也就是一個(gè)或多個(gè)字符串。
Get-Service [-ComputerName <System.String[]>] [-DependentServices] -DisplayName <System.String[]> [-Exclude <System.String[]>] [-Include <System.String[]>] [-RequiredServices] [<CommonParameters>]
Get-Service [-ComputerName <System.String[]>] [-DependentServices] [-Exclude <System.String[]>] [-Include <System.String[]>] [-InputObject <System.ServiceProcess.ServiceController[]>] [-RequiredServices] [<CommonParameters>]
Get-Service [[-Name] <System.String[]>] [-ComputerName <System.String[]>] [-DependentServices] [-Exclude <System.String[]>] [-Include <System.String[]>] [-RequiredServices] [<CommonParameters>]
解釋:
?[-ComputerName <System.String[]>]
? ? 1. ComputerName是參數(shù)名。
? ? 2. <System.String[]>是參數(shù)值的類型是字符串類型,System.String[]的中括號(hào),代表可以是字; 符串?dāng)?shù)組,也就是用逗號(hào)分隔的多個(gè)字符串作為參數(shù)的值,例如: -Computername DC1.Contoso.com, DC2.Contoso.com。
? ? 3. [-ComputerName <System.String[]>]最外層的大括號(hào)代表此參數(shù)是可選的,可以不輸入這個(gè)參數(shù)。在Get-Help命令中,如果指定Computername,將會(huì)查詢給出的計(jì)算機(jī)所運(yùn)行的服務(wù),而不指定Computername,那么默認(rèn)查詢本地計(jì)算機(jī)所運(yùn)行的服務(wù)。?[-DependentServices]
? ? 只有參數(shù)名,而沒有參數(shù)值的參數(shù)是開關(guān)參數(shù)。當(dāng)輸入開關(guān)參數(shù)后,Powershell會(huì)有一個(gè)邏輯來運(yùn)行開關(guān)參數(shù)為True的分支代碼。如果不輸入開關(guān)參數(shù),Powershell運(yùn)行開關(guān)參數(shù)是False的分支代碼。?一個(gè)命令可能會(huì)有多個(gè)語法。這是因?yàn)樵赑owershell中,并不是所有的參數(shù)都可以放在一起執(zhí)行,不能放在一起執(zhí)行的參數(shù)也叫做互斥參數(shù)。例如Get-Service命令中,-DisplayName, -Name和-InputObject都是互斥參數(shù),所以需要用三個(gè)語法來表示Get-Service這個(gè)命令的全部用法。
說明
TheGet-Servicecmdlet gets objects that represent the services on a local computer or on a remote computer, including running and stopped services.
You can direct this cmdlet to get only particular services by specifying the service name or the display name of the services, or you can pipe service objects to this cmdlet.
解釋: 對(duì)于Get-Service命令的詳細(xì)描述。
參數(shù)
-ComputerName <System.String[]> Gets the services running on the specified computers. The default is the local computer. Type the NetBIOS name, an IP address, or a fully qualified domain name (FQDN) of a remote computer. To specify the local computer, type the computer name, a dot (.), or localhost. This parameter does not rely on Windows PowerShell remoting. You can use the ComputerName parameter of
Get-Service even if your computer is not configured to run remote commands.
?是否必需? False
?位置? named
?默認(rèn)值 None
?是否接受管道輸入? True (ByPropertyName)
?是否接受通配符? False解釋:參數(shù)模塊會(huì)顯示出命令所接受的所有參數(shù),首先是對(duì)于參數(shù)的說明。接下來時(shí)關(guān)于參數(shù)的屬性進(jìn)行介紹。
? ? 1. 是否必須: 這個(gè)參數(shù)是否是可選參數(shù)。
? ? 2. 位置: Named是命名參數(shù),調(diào)用參數(shù)的時(shí)候必須輸入?yún)?shù)名稱。位置參數(shù)使用從0開始的數(shù)字表示,不用輸入?yún)?shù)名稱,只要放置在指定的位置即可。
? ? 3. 默認(rèn)值:參數(shù)是否有默認(rèn)值,對(duì)于有默認(rèn)值的參數(shù),如果不使用這個(gè)參數(shù)并對(duì)其賦值,那么命令會(huì)用默認(rèn)值為參數(shù)賦值。
? ? 4. 是否接受管道輸入: 可否用管道符傳入值。可以根據(jù)屬性名傳值ByPropertyName或者根據(jù)值類型傳值ByValue。這部分將會(huì)在之后介紹腳本參數(shù)的章節(jié)進(jìn)行詳細(xì)介紹。
? ? 5. 是否接受通配符:指明參數(shù)是否可以使用 * 這樣的通配符。
輸入?#命令所接受的參數(shù)的類型
System.ServiceProcess.ServiceController, System.String
You can pipe a service object or a service name to this cmdlet.輸出 ?#命令所輸出的參數(shù)的類型
System.ServiceProcess.ServiceController
This cmdlet returns objects that represent the services on the computer.解釋:
輸入代表此命令所接受的參數(shù)的所有類型。
輸出代表此命令返回值的類型。
范例
--------- Example 1: Get all services on the computer ---------
Get-Service
This command gets all of the services on the computer. It behaves as though you typedGet-Service *. The default
display shows the status, service name, and display name of each service.--- Example 2: Get services that begin with a search string ---
Get-Service "wmi*"
This command retrieves services with service names that begin with WMI (the acronym for Windows Management Instrumentation).解釋: 范例部分是開發(fā)者給用戶的范例代碼??梢詭椭脩舾玫睦斫饷?,以及提供一些命令的使用場景。
注釋
You can also refer to Get-Service by its built-in alias, "gsv". For more information, see about_Aliases (../Microsoft.PowerShell.Core/About/about_Aliases.md).
This cmdlet can display services only when the current user has permission to see them. If this cmdlet does not display services, you might not have permission to see them. To find the service name and display name of each service on your system, typeGet-Service. The service names appear in the Name column, and the display names appear in the DisplayName column. When you sort in ascending order by status value, "Stopped" services appear before "Running" services. The Status property of a service is an enumerated value in which the names of the statuses represent integer values. The sort is based on the integer value, not the name. "Running" appears before "Stopped" because "Stopped" has a value of "1", and "Running" has a value of "4".解釋: 開發(fā)者給出的命令的一些備注信息。
相關(guān)鏈接 >?#與命令相關(guān)的網(wǎng)站,Get-Help -Online參數(shù)用到。
Online Version: https://docs.microsoft.com/powershell/module/microsoft.powershell.management/get-service?view=power
New-Service
Restart-Service
Resume-Service
Set-Service
Start-Service
Stop-Service
Suspend-Service
解釋: 命令的在線幫助信息和相關(guān)命令。放在第一行的是在線幫助的網(wǎng)站鏈接,可以復(fù)制到瀏覽器進(jìn)行查看。也是Get-Help Get-Service -Online所轉(zhuǎn)向的網(wǎng)址。
備注
若要查看示例,請鍵入: "get-help Get-Service -examples".
有關(guān)詳細(xì)信息,請鍵入: "get-help Get-Service -detailed".
若要獲取技術(shù)信息,請鍵入: "get-help Get-Service -full".
有關(guān)在線幫助,請鍵入: "get-help Get-Service -online"
若要查看參數(shù),請鍵入: "get-help Get-Service -parameter".解釋:額外的信息,介紹Get-Help的參數(shù)信息。
搜索引擎
Get-Help提供的是關(guān)于命令的使用方法,如果我們想知道針對(duì)問題的解決方案,可能就需要使用搜索引擎進(jìn)行查找。
中文社區(qū)的一個(gè)問題是同樣的內(nèi)容被很多網(wǎng)站引用,造成的結(jié)果是搜索出來的結(jié)果雖然多,但是內(nèi)容幾乎相同。
對(duì)于搜索引擎,我的建議是使用Bing來進(jìn)行搜索,尤其是國內(nèi)如果找不到的話,不妨試試在必應(yīng)的國際版,使用英文嘗試搜索一下,可能會(huì)有驚喜。
Microsoft Docs
(https://docs.microsoft.com/zh-cn/powershell/)
Microsoft文檔中心提供了Powershell的文檔庫,推薦閱讀以下模塊:
Powershell 101, 向?qū)降膶W(xué)習(xí)指引。
https://docs.microsoft.com/zh-cn/powershell/scripting/learn/ps101/00-introduction?view=powershell-7.2About關(guān)于主題,介紹Powershell的基本概念。
https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about?view=powershell-7.2
.NET Framework 類庫
(https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframework-4.8)
在這里可以查詢到與Dotnet相關(guān)的類的幫助文檔。
因?yàn)镻owershell沒有提供原生的數(shù)學(xué)計(jì)算方式,所以我們需要使用.NET提供的System.Math類的靜態(tài)方法進(jìn)行四舍五入等計(jì)算。
關(guān)于System.Math類,可以在.Net Framework類庫中進(jìn)行查詢。




