(4)Powershell基礎知識(二)

上一節主要介紹Powershell可發現,面向對象,一致性等特性,以及Powershell命令是基於.Net對象等重要概念,以及Powershell命令的命名規範,詳細內容點擊這裏


這一節的Powershell基礎知識主要包含以下知識點

  1. 獲取命令的摘要信息。

  2. 獲取命令的幫助信息。

  3. 總結。


獲取命令的摘要信息

Powershell命令 Get-Command 可檢索當前shell中所有可用的命令名稱。在Powershell提示符輸入 Get-Command ,輸出的內容類似以下內容(以下只寫出輸出的結果的部分內容)。

PS C:\Documents and Settings\Administrator> Get-Command

CommandType     Name                                                Definition
-----------     ----                                                ----------
Alias           %                                                   ForEach-Object
Alias           ?                                                   Where-Object
Function        A:                                                  Set-Location A:
Alias           ac                                                  Add-Content
Cmdlet          Add-Computer                                        Add-Computer [-DomainName] <String> [-Credential...
Cmdlet          Add-Content                                         Add-Content [-Path] <String[]> [-Value] <Object[...
Cmdlet          Add-History                                         Add-History [[-InputObject] <PSObject[]>] [-Pass...
Cmdlet          Add-Member                                          Add-Member [-MemberType] <PSMemberTypes> [-Name]...

在 Get-Command 命令的輸出中,所有定義都以省略號 (...) 結尾,表示 PowerShell 無法在可用空間內顯示所有內容。在顯示輸出時,PowerShell 會將輸出格式設置爲文本,然後對其進行排列,以使數據整齊地顯示在窗口中。在後續的文章中會對命令輸出的格式化做詳細說明。

Get-Command cmdlet 有一個 Syntax 參數,使用該參數,可以僅檢索每個 cmdlet 的語法。輸入 Get-Command -Syntax 命令可以顯示完整的輸出:

PS C:\Documents and Settings\Administrator> Get-Command -Syntax
ForEach-Object
Where-Object
A:

Add-Content
Add-Computer [-DomainName] <String> [-Credential <PSCredential>] [-OUPath <String>] [-PassThru] [-Server <String>] [-UnSecure] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <Stri
ng>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]Add-Computer [-WorkGroupName] <String> [-Credential <PSCredential>] [-PassThru] [-Verbose] [-Debug] [-ErrorAction <Acti
onPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable<String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm]

Add-Content [-Path] <String[]> [-Value] <Object[]> [-PassThru] [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <Act
ionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm] [-UseTransaction] [-Encoding <FileSystemCmdletProviderEncoding>]
Add-Content [-LiteralPath] <String[]> [-Value] <Object[]> [-PassThru] [-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningActi
on <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>] [-WhatIf] [-Confirm] [-UseTransaction] [-Encoding <FileSystemCmdletProviderEncoding>]

需要注意的是,Get-Command 命令僅列出當前 shell 中的 cmdlet,並不是Powershell中的所有可用的命令。別名,函數和腳本也是Powershell命令,外部程序也歸類與命令。通過輸入以下命令,可以返回所有可調用項的列表:

PS> Get-Command *

由於此列表包括搜索路徑中的外部文件,因此它可能包含數千個項目。實際上,僅查看精簡的命令集更爲有用。若要查找其他類型的本機命令,可以使用 Get-Command cmdlet 的 CommandType 參數。儘管我們尚未介紹其他這些命令類型,但如果知道某類命令的 CommandType 名稱,您仍然可以顯示這些命令類型。

注意:命令中的星號(*) 是通配符,表示“匹配一個或多個任意字符”,如輸入Get-Command *ervice* 列出所有包含"ervice"的命令。

    若要顯示特殊命令類別的別名(作標準命令名稱的替代名稱),可輸入以下命令:

PS> Get-Command -CommandType Alias

    若要顯示所有 Windows PowerShell 函數,請輸入以下命令:

PS> Get-Command -CommandType Function

    若要顯示 PowerShell 搜索路徑中的外部腳本,請輸入以下命令:

PS> Get-Command -CommandType ExternalScript

獲取命令的幫助信息

  • 獲取cmdlet幫助信息

    要獲取有關 PowerShell cmdlet 的幫助,請使用 Get-Help cmdlet。例如,要獲取Get-ChildItem的幫助,請輸入:

get-help get-childitem

    或

get-childitem -?

    當然可以獲取Get-Help命令本身的幫助,例如:

get-help get-help

    若要在會話中獲取所有 cmdlet 幫助主題的列表,請鍵入:

get-help -category cmdlet

    若要每次顯示每個幫助主題的一頁,請使用 help 函數或其別名 man。例如,若要顯示 Get-ChildItem cmdlet 的幫助,請鍵入

man get-childitem

    或

help get-childitem

    若要顯示有關 cmdlet、函數或腳本的詳細信息,包括其參數說明和使用示例,請使用 Get-Help cmdlet 的 Detailed 參數。例如,若要獲取有關 Get-ChildItem cmdlet 的詳細信息,請鍵入:

get-help get-childitem -detailed  #顯示get-childitem的詳細信息
get-help get-childitem -full      #顯示get-childitem幫助主題的全部內容
get-help get-childitem -parameter *  #顯示get-childitem參數的詳細幫助
get-help get-childitem -examples     #顯示get0childitem的幫助中的示例

:Powershell中,#用於註釋,類似於Java或C#中的"//"。

  • 獲取概念性幫助。Powershell通過 Get-Help about_* 命令獲取概念性的幫助。

  • 獲取有關提供程序的幫助

    Powershell可以獲取提供程序的幫助信息。如要獲取Registry 提供程序的幫助,請輸入:

get-help registry

    如要在會話中獲取所有提供程序幫助主題的列表,請輸入

get-help -category provider

    Get-Help 的各個參數(如 Detailed、Parameter 和 Examples)對提供程序幫助主題的顯示沒有影響。

  • 獲取連網幫助

    如果計算機已經連接到網絡,查看幫助的最好方式是查看網上幫助主題,在線幫助主題更容易提供最新的內容。

    若要使用 Get-Help cmdlet 的 Online 參數,請使用以下命令格式。

get-help <command-name> -online

    如果該命令提供了幫助主題的聯機版本,則它將在默認瀏覽器中打開。


總結

通過學習本節,應當掌握以下內容。

  1. 可以查看當前shell中所有可用的命令及Powershell中所有可用的命令。

  2. 對於任何一條命令會查看其摘要信息,語法信息及指定類型命令的檢索。

  3. 可以獲取指定命令的幫助信息,包括在線幫助信息。

  4. 知道Powershell中"#"是用於內容的註釋。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章