共用方式為


第 2 章 - 協助系統

在一項旨在評估 PowerShell 熟練程度的實驗中,兩組不同的 IT 專業人員——初學者和專家——首先進行了一次無法使用計算機的筆試。 令人驚訝的是,測試分數顯示兩組有相似的能力。 接著進行後續的測試,與第一次測試相同,但有一個主要差異:參與者可以存取離線並配備 PowerShell 的電腦。 結果顯示,這次兩組之間的技能差距很大。

哪些因素促成了兩項評估之間觀察到的結果?

專家並不總是知道答案,但他們知道如何找出答案。

兩項測試結果中觀察到的結果是因為專家不會記住數千個 PowerShell 命令。 相反地,他們擅長在PowerShell中使用說明系統,讓他們能夠探索並瞭解如何在必要時使用命令。

熟悉說明系統是PowerShell成功的關鍵。

我聽到 PowerShell 的建立者 Jeffrey Snover 多次分享類似的故事。

可探索性

PowerShell 中的編譯命令稱為 Cmdlet,發音為 “command-let”,而不是 “CMD-let”。 Cmdlet 的命名慣例會遵循單一 Verb-Noun 格式,使其易於探索。 例如,Get-Process 是判斷哪些進程正在執行的 Cmdlet,而 Get-Service 是擷取服務清單的 Cmdlet。 函式也稱為腳本 Cmdlet 和別名,是本文稍後討論的其他類型的 PowerShell 命令。 “PowerShell 命令” 一詞 描述 PowerShell 中的任何命令,不論它是 Cmdlet、函式或別名。

您也可以從 PowerShell 執行作系統原生命令,例如傳統命令列程式,例如 ping.exeipconfig.exe

PowerShell 中的三個核心 cmdlet

  • Get-Help
  • Get-Command
  • Get-Member(第3章涵蓋)

我經常被問及:「如何找出 PowerShell 中的命令?」Get-HelpGet-Command 都是在PowerShell中探索和瞭解命令的寶貴資源。

獲取幫助

您需要瞭解 PowerShell 中說明系統的第一件事,就是如何使用 Get-Help Cmdlet。

Get-Help 是多用途命令,可協助您瞭解如何在找到命令之後使用命令。 您也可以使用 Get-Help 來尋找命令,但相較於 Get-Command,則以不同且更間接的方式尋找命令。

使用 Get-Help 來定位命令時,它會根據您的輸入,先執行一次命令名稱的通配符搜尋。 如果找不到任何相符項目,它會針對您系統上的所有 PowerShell 說明文章進行完整的全文搜尋。 如果這也找不到任何結果,則會傳回錯誤。

以下說明如何使用 Get-Help 來檢視 Get-Help Cmdlet 的說明內容。

Get-Help -Name Get-Help

從 PowerShell 3.0 版開始,說明內容不會隨作系統預安裝。 當您第一次執行 Get-Help 時,訊息會詢問您是否要將PowerShell 說明檔案下載到您的電腦。

回答 並按 Y 會執行 Update-Help Cmdlet,進而下載說明內容。

Do you want to run Update-Help?
The Update-Help cmdlet downloads the most current Help files for Windows
PowerShell modules, and installs them on your computer. For more information
about the Update-Help cmdlet, see
https:/go.microsoft.com/fwlink/?LinkId=210614.
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

如果您沒有收到此訊息,請從以系統管理員身分執行的已提升許可權的 PowerShell 會話執行 Update-Help

更新完成後,會顯示說明文章。

請花點時間在計算機上執行範例、檢閱輸出,並觀察說明系統如何組織資訊。

  • 名字
  • 概要
  • 語法
  • 描述
  • 相關連結
  • 備註

當您檢閱輸出時,請記住,說明文章通常包含大量的資訊,而且您預設看到的不是整個說明文章。

參數

當您在 PowerShell 中執行命令時,您可能需要提供命令的其他資訊或輸入。 參數可讓您指定變更命令行為的選項和自變數。 每個說明文章的 SYNTAX 區段概述命令的可用參數。

Get-Help 有數個參數,您可以指定這些參數以返回整個或部分的說明文件,用於某個命令。 若要檢視 Get-Help的所有可用參數,請參閱其說明文章的 SYNTAX 一節,如下列範例所示。

...
SYNTAX
    Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
    | General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
    Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
    Class | Configuration}] [-Component <System.String[]>] [-Full]
    [-Functionality <System.String[]>] [-Path <System.String>] [-Role
    <System.String[]>] [<CommonParameters>]

    Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
    | General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
    Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
    Class | Configuration}] [-Component <System.String[]>] -Detailed
    [-Functionality <System.String[]>] [-Path <System.String>] [-Role
    <System.String[]>] [<CommonParameters>]

    Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
    | General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
    Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
    Class | Configuration}] [-Component <System.String[]>] -Examples
    [-Functionality <System.String[]>] [-Path <System.String>] [-Role
    <System.String[]>] [<CommonParameters>]

    Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
    | General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
    Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
    Class | Configuration}] [-Component <System.String[]>] [-Functionality
    <System.String[]>] -Online [-Path <System.String>] [-Role
    <System.String[]>] [<CommonParameters>]

    Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
    | General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
    Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
    Class | Configuration}] [-Component <System.String[]>] [-Functionality
    <System.String[]>] -Parameter <System.String> [-Path <System.String>]
    [-Role <System.String[]>] [<CommonParameters>]

    Get-Help [[-Name] <System.String>] [-Category {Alias | Cmdlet | Provider
    | General | FAQ | Glossary | HelpFile | ScriptCommand | Function |
    Filter | ExternalScript | All | DefaultHelp | Workflow | DscResource |
    Class | Configuration}] [-Component <System.String[]>] [-Functionality
    <System.String[]>] [-Path <System.String>] [-Role <System.String[]>]
    -ShowWindow [<CommonParameters>]
...

參數集

當您檢閱 Get-Help 區段時,請注意資訊似乎重複了六次。 每個區塊都是個別的參數集,表示 Get-Help Cmdlet 具有六組不同的參數。 仔細查看會顯示每個參數集至少包含一個唯一參數,使其與其他參數不同。

參數集互斥。 一旦指定唯一的參數只存在於一個參數集中,PowerShell 就會限制您使用該參數集內所包含的參數。 例如,您無法使用 完整 參數,因為它們屬於不同的參數集。

下列每個參數都屬於 Get-Help Cmdlet 的不同參數集。

  • 完整
  • 詳細
  • 範例
  • 線上
  • 參數
  • 顯示視窗

命令語法

如果您不熟悉 PowerShell,理解在 SYNTAX 區段中以方括弧和角括弧為特徵的神秘信息,可能會讓人感到困惑。 不過,了解這些語法元素對於熟悉PowerShell至關重要。 使用 PowerShell 說明系統的頻率愈高,就更容易記住所有細微差別。

檢視 Get-EventLog Cmdlet 的語法。

Get-Help Get-EventLog

下列輸出顯示幫助文章的相關內容。

...
SYNTAX
    Get-EventLog [-LogName] <System.String> [[-InstanceId]
    <System.Int64[]>] [-After <System.DateTime>] [-AsBaseObject] [-Before
    <System.DateTime>] [-ComputerName <System.String[]>] [-EntryType {Error
    | Information | FailureAudit | SuccessAudit | Warning}] [-Index
    <System.Int32[]>] [-Message <System.String>] [-Newest <System.Int32>]
    [-Source <System.String[]>] [-UserName <System.String[]>]
    [<CommonParameters>]

    Get-EventLog [-AsString] [-ComputerName <System.String[]>] [-List]
    [<CommonParameters>]
...

語法資訊包括成對的方括號([])。 根據它們的使用方式,這些方括弧有兩個不同的用途。

  • 以方括弧括住的項目是選擇性的。
  • 數據類型之後的空白方括號集合,例如 <string[]>,表示參數可以接受傳遞為陣列或集合物件的多個值。

位置參數

某些 Cmdlet 的設計目的是要接受位置參數。 位置參數可讓您提供值,而不指定參數的名稱。 在位置上使用參數時,您必須在命令行的正確位置中指定其值。 您可以在命令說明文章的 PARAMETERS 區段中找到參數的位置資訊。 當您明確指定參數名稱時,可以使用任何順序的參數。

對於 Get-EventLog Cmdlet,第一個參數集的第一個參數是 LogNameLogName 會以方括弧括住,表示它是位置參數。

Get-EventLog [-LogName] <System.String>

由於 LogName 是位置參數,因此您可以透過名稱或位置來指定它。 根據參數名稱後面的角括弧,LogName 的值必須是單一字串。 不含括住參數名稱和數據類型的方括弧,表示 LogName 是這個特定參數集內的必要參數。

該參數集中第二個參數為 InstanceId。 參數名稱和數據類型全都以方括弧括住,表示 InstanceId 是選擇性參數。

[[-InstanceId] <System.Int64[]>]

此外,InstanceId 有自己的一對方括弧,表示這是位置參數,類似於 LogName 參數。 在數據類型之後,一組空的方括弧表示 InstanceId 可以接受多個值。

切換參數

不需要值的參數稱為 switch 參數。 您可以輕鬆地識別開關參數,因為參數名稱之後沒有資料類型。 當您指定 switch 參數時,其值會 true。 當您未指定 switch 參數時,其值會 false

第二個參數集包含 List 參數,這是 switch 參數。 當您指定 List 參數時,它會傳回本機電腦上的事件記錄清單。

[-List]

語法的簡化方法

除了純英文以外,還有更方便使用的方法可取得與某些命令的神秘命令語法相同的資訊。 使用 Get-Help 搭配 Full 參數時,PowerShell 會傳回完整的說明文章,讓您更容易瞭解命令的使用方式。

Get-Help -Name Get-Help -Full

請花點時間在計算機上執行範例、檢閱輸出,並觀察說明系統如何組織資訊。

  • 名字
  • 概要
  • 語法
  • 描述
  • 參數
  • 輸入
  • 輸出
  • 筆記
  • 例子
  • 相關連結

藉由使用 Cmdlet 指定 Get-Help 參數,輸出會包含數個額外的區段。 在這些章節中,PARAMETERS 通常會為每個參數提供詳細的說明。 不過,這項資訊的範圍會根據您調查的特定命令而有所不同。

...
    -Detailed <System.Management.Automation.SwitchParameter>
        Adds parameter descriptions and examples to the basic help display.
        This parameter is effective only when the help files are installed
        on the computer. It has no effect on displays of conceptual ( About_
        ) help.

        Required?                    true
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false

    -Examples <System.Management.Automation.SwitchParameter>
        Displays only the name, synopsis, and examples. This parameter is
        effective only when the help files are installed on the computer. It
        has no effect on displays of conceptual ( About_ ) help.

        Required?                    true
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false

    -Full <System.Management.Automation.SwitchParameter>
        Displays the entire help article for a cmdlet. Full includes
        parameter descriptions and attributes, examples, input and output
        object types, and additional notes.

        This parameter is effective only when the help files are installed
        on the computer. It has no effect on displays of conceptual ( About_
        ) help.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       False
        Accept wildcard characters?  false
...

當您執行上一個命令以顯示 Get-Help 命令的說明時,您可能會注意到輸出滾動得太快,讓人難以閱讀。

如果您使用PowerShell控制台、Windows終端機或 VS Code,而且需要檢視說明文章,則 help 函式很有用。 它會使用管道將 Get-Help 的輸出傳送至 more.com,顯示說明內容每次一頁。 我建議使用 help 函式,而不是 Get-Help Cmdlet,因為它能提供更好的使用者體驗,而且輸入起來較省力。

備註

ISE 不支援使用 more.com,因此執行 help 的運作方式與 Get-Help相同。

在電腦上的PowerShell中執行下列每個命令。

Get-Help -Name Get-Help -Full
help -Name Get-Help -Full
help Get-Help -Full

當您執行先前的命令時,是否觀察到輸出中的任何變化?

在上一個範例中,第一行使用 Get-Help Cmdlet,第二行會使用 help 函式,而第三行則會在使用 函式時省略 help 參數。 由於 Name 是位置參數,因此第三個範例會利用其位置,而不是明確指出參數的名稱。

差異在於最後兩個命令會逐頁顯示其輸出。 使用 help 函式時,請按 空格鍵 以顯示下一頁的內容或 Q 結束。 如果您需要終止在 PowerShell 中以互動方式執行的任何命令,請按 Ctrl+C

若要快速尋找特定參數的相關信息,請使用 Parameter 參數。 此方法只會傳回包含參數特定信息的內容,而不是整個說明文章。 這是尋找特定參數相關信息的最簡單方式。

下列範例使用 help 函式與 參數 參數,從 Get-Help 名稱的說明文章中傳回資訊。

help Get-Help -Parameter Name

說明信息顯示 Name 參數是位置參數,且在使用時,必須在第一個位置(位置零)指定。

-Name <System.String>
    Gets help about the specified command or concept. Enter the name of a
    cmdlet, function, provider, script, or workflow, such as `Get-Member`,
    a conceptual article name, such as `about_Objects`, or an alias, such
    as `ls`. Wildcard characters are permitted in cmdlet and provider
    names, but you can't use wildcard characters to find the names of
    function help and script help articles.

    To get help for a script that isn't located in a path that's listed in
    the `$env:Path` environment variable, type the script's path and file
    name.

    If you enter the exact name of a help article, `Get-Help` displays the
    article contents.

    If you enter a word or word pattern that appears in several help
    article titles, `Get-Help` displays a list of the matching titles.

    If you enter any text that doesn't match any help article titles,
    `Get-Help` displays a list of articles that include that text in their
    contents.

    The names of conceptual articles, such as `about_Objects`, must be
    entered in English, even in non-English versions of PowerShell.

    Required?                    false
    Position?                    0
    Default value                None
    Accept pipeline input?       True (ByPropertyName)
    Accept wildcard characters?  true

Name 參數預期一個由參數名稱旁邊的 <String> 數據類型所識別的字串值。

您可以使用 Get-Help 來指定數個其他參數,以傳回說明文章的子集。 若要查看其運作方式,請在您的電腦上執行下列命令。

Get-Help -Name Get-Command -Full
Get-Help -Name Get-Command -Detailed
Get-Help -Name Get-Command -Examples
Get-Help -Name Get-Command -Online
Get-Help -Name Get-Command -Parameter Noun
Get-Help -Name Get-Command -ShowWindow

我通常會使用 help <command name> 搭配 FullOnline 參數。 如果您只對範例感興趣,請使用 Examples 參數。 如果您只對特定參數感興趣,請使用 Parameter 參數。

當您使用 ShowWindow 參數時,它會在個別的可搜尋視窗中顯示說明內容。 如果您有多個監視器,您可以將該視窗移至不同的監視器。 不過,ShowWindow 參數有一個已知錯誤,可能會導致它無法顯示整個說明文章。 ShowWindow 參數也需要具有圖形使用者介面 (GUI) 的作系統。 當您嘗試在 Windows Server Core 上使用時,它會傳回錯誤。

如果您有因特網存取權,您可以改用 Online 參數。 Online 參數會在預設網頁瀏覽器中開啟說明文章。 線上內容是最 up-to當前內容。 瀏覽器可讓您搜尋說明內容,並檢視其他相關的說明文章。

備註

About 文章不支援 Online 參數。

help Get-Command -Online

使用 Get-Help 尋找命令

若要尋找具有 Get-Help的命令,請為 * 參數的值指定以星號 () 通配符括住的搜尋字詞。 下列範例會以位置方式使用 Name 參數。

help *process*
Name                              Category  Module                    Synops
----                              --------  ------                    ------
Enter-PSHostProcess               Cmdlet    Microsoft.PowerShell.Core Con...
Exit-PSHostProcess                Cmdlet    Microsoft.PowerShell.Core Clo...
Get-PSHostProcessInfo             Cmdlet    Microsoft.PowerShell.Core Get...
Debug-Process                     Cmdlet    Microsoft.PowerShell.M... Deb...
Get-Process                       Cmdlet    Microsoft.PowerShell.M... Get...
Start-Process                     Cmdlet    Microsoft.PowerShell.M... Sta...
Stop-Process                      Cmdlet    Microsoft.PowerShell.M... Sto...
Wait-Process                      Cmdlet    Microsoft.PowerShell.M... Wai...
Invoke-LapsPolicyProcessing       Cmdlet    LAPS                      Inv...
ConvertTo-ProcessMitigationPolicy Cmdlet    ProcessMitigations        Con...
Get-ProcessMitigation             Cmdlet    ProcessMitigations        Get...
Set-ProcessMitigation             Cmdlet    ProcessMitigations        Set...

在此案例中,您不需要新增 * 通配符。 如果 Get-Help 找不到符合您提供值的命令,它會對該值執行全文搜索。 下列範例會產生與在 *的每個結尾指定 process 通配符相同的結果。

help process

當您在值內指定通配符時,Get-Help 只會搜尋符合您所提供模式的命令。 它不會執行全文搜索。 下列命令不會傳回任何結果。

help pr*cess

如果您指定以破折號開頭的值,而不用引弧括住它,因為它會將它解譯為參數名稱,則 PowerShell 會產生錯誤。 Get-Help Cmdlet 沒有這類參數名稱。

help -process

如果您嘗試搜尋以 -process結尾的命令,則必須將 * 新增至值的開頭。

help *-process

當您使用 Get-Help搜尋 PowerShell 命令時,最好是模糊而非過於具體。

當您稍早搜尋 process 時,結果只會傳回名稱中包含 process 的命令。 但是,如果您搜尋 processes,它就不會找到任何與之相符的指令名稱。 如先前所述,當說明無法找到任何相符項目時,它會在系統上的每篇說明文章中執行完整的全文檢索,並傳回這些結果。 這種類型的搜尋通常會產生比預期更多的結果,包括與您無關的資訊。

help processes
Name                              Category  Module                    Synops
----                              --------  ------                    ------
Disconnect-PSSession              Cmdlet    Microsoft.PowerShell.Core Dis...
Enter-PSHostProcess               Cmdlet    Microsoft.PowerShell.Core Con...
ForEach-Object                    Cmdlet    Microsoft.PowerShell.Core Per...
Get-PSHostProcessInfo             Cmdlet    Microsoft.PowerShell.Core Get...
Get-PSSessionConfiguration        Cmdlet    Microsoft.PowerShell.Core Get...
New-PSSessionOption               Cmdlet    Microsoft.PowerShell.Core Cre...
New-PSTransportOption             Cmdlet    Microsoft.PowerShell.Core Cre...
Out-Host                          Cmdlet    Microsoft.PowerShell.Core Sen...
Start-Job                         Cmdlet    Microsoft.PowerShell.Core Sta...
Where-Object                      Cmdlet    Microsoft.PowerShell.Core Sel...
Debug-Process                     Cmdlet    Microsoft.PowerShell.M... Deb...
Get-Process                       Cmdlet    Microsoft.PowerShell.M... Get...
Get-WmiObject                     Cmdlet    Microsoft.PowerShell.M... Get...
Start-Process                     Cmdlet    Microsoft.PowerShell.M... Sta...
Stop-Process                      Cmdlet    Microsoft.PowerShell.M... Sto...
Wait-Process                      Cmdlet    Microsoft.PowerShell.M... Wai...
Clear-Variable                    Cmdlet    Microsoft.PowerShell.U... Del...
Convert-String                    Cmdlet    Microsoft.PowerShell.U... For...
ConvertFrom-Csv                   Cmdlet    Microsoft.PowerShell.U... Con...
ConvertFrom-Json                  Cmdlet    Microsoft.PowerShell.U... Con...
ConvertTo-Html                    Cmdlet    Microsoft.PowerShell.U... Con...
ConvertTo-Xml                     Cmdlet    Microsoft.PowerShell.U... Cre...
Debug-Runspace                    Cmdlet    Microsoft.PowerShell.U... Sta...
Export-Csv                        Cmdlet    Microsoft.PowerShell.U... Con...
Export-FormatData                 Cmdlet    Microsoft.PowerShell.U... Sav...
Format-List                       Cmdlet    Microsoft.PowerShell.U... For...
Format-Table                      Cmdlet    Microsoft.PowerShell.U... For...
Get-Unique                        Cmdlet    Microsoft.PowerShell.U... Ret...
Group-Object                      Cmdlet    Microsoft.PowerShell.U... Gro...
Import-Clixml                     Cmdlet    Microsoft.PowerShell.U... Imp...
Import-Csv                        Cmdlet    Microsoft.PowerShell.U... Cre...
Measure-Object                    Cmdlet    Microsoft.PowerShell.U... Cal...
Out-File                          Cmdlet    Microsoft.PowerShell.U... Sen...
Out-GridView                      Cmdlet    Microsoft.PowerShell.U... Sen...
Select-Object                     Cmdlet    Microsoft.PowerShell.U... Sel...
Set-Variable                      Cmdlet    Microsoft.PowerShell.U... Set...
Sort-Object                       Cmdlet    Microsoft.PowerShell.U... Sor...
Tee-Object                        Cmdlet    Microsoft.PowerShell.U... Sav...
Trace-Command                     Cmdlet    Microsoft.PowerShell.U... Con...
Write-Information                 Cmdlet    Microsoft.PowerShell.U... Spe...
Export-BinaryMiLog                Cmdlet    CimCmdlets                Cre...
Get-CimAssociatedInstance         Cmdlet    CimCmdlets                Ret...
Get-CimInstance                   Cmdlet    CimCmdlets                Get...
Import-BinaryMiLog                Cmdlet    CimCmdlets                Use...
Invoke-CimMethod                  Cmdlet    CimCmdlets                Inv...
New-CimInstance                   Cmdlet    CimCmdlets                Cre...
Remove-CimInstance                Cmdlet    CimCmdlets                Rem...
Set-CimInstance                   Cmdlet    CimCmdlets                Mod...
Compress-Archive                  Function  Microsoft.PowerShell.A... Cre...
Get-Counter                       Cmdlet    Microsoft.PowerShell.D... Get...
Invoke-WSManAction                Cmdlet    Microsoft.WSMan.Manage... Inv...
Remove-WSManInstance              Cmdlet    Microsoft.WSMan.Manage... Del...
Get-WSManInstance                 Cmdlet    Microsoft.WSMan.Manage... Dis...
New-WSManInstance                 Cmdlet    Microsoft.WSMan.Manage... Cre...
Set-WSManInstance                 Cmdlet    Microsoft.WSMan.Manage... Mod...
about_Arithmetic_Operators        HelpFile
about_Arrays                      HelpFile
about_Environment_Variables       HelpFile
about_Execution_Policies          HelpFile
about_Functions                   HelpFile
about_Jobs                        HelpFile
about_Logging                     HelpFile
about_Methods                     HelpFile
about_Objects                     HelpFile
about_Pipelines                   HelpFile
about_Preference_Variables        HelpFile
about_Remote                      HelpFile
about_Remote_Jobs                 HelpFile
about_Session_Configuration_Files HelpFile
about_Simplified_Syntax           HelpFile
about_Switch                      HelpFile
about_Variables                   HelpFile
about_Variable_Provider           HelpFile
about_Windows_PowerShell_5.1      HelpFile
about_WQL                         HelpFile
about_WS-Management_Cmdlets       HelpFile
about_Foreach-Parallel            HelpFile
about_Parallel                    HelpFile
about_Sequence                    HelpFile

當您搜尋 process時,它會傳回 12 個結果。 不過,搜尋 processes時,會產生 78 個結果。 如果您的搜尋只找到一個相符專案,Get-Help 顯示幫助內容而非列出搜尋結果。

help *hotfix*
NAME
    Get-HotFix

SYNOPSIS
    Gets the hotfixes that are installed on local or remote computers.


SYNTAX
    Get-HotFix [-ComputerName <System.String[]>] [-Credential
    <System.Management.Automation.PSCredential>] [-Description
    <System.String[]>] [<CommonParameters>]

    Get-HotFix [[-Id] <System.String[]>] [-ComputerName <System.String[]>]
    [-Credential <System.Management.Automation.PSCredential>]
    [<CommonParameters>]


DESCRIPTION
    > This cmdlet is only available on the Windows platform. The
    `Get-HotFix` cmdlet uses the Win32_QuickFixEngineering WMI class to
    list hotfixes that are installed on the local computer or specified
    remote computers.


RELATED LINKS
    Online Version: https://learn.microsoft.com/powershell/module/microsoft.
    powershell.management/get-hotfix?view=powershell-5.1&WT.mc_id=ps-gethelp
    about_Arrays
    Add-Content
    Get-ComputerRestorePoint
    Get-Credential
    Win32_QuickFixEngineering class

REMARKS
    To see the examples, type: "Get-Help Get-HotFix -Examples".
    For more information, type: "Get-Help Get-HotFix -Detailed".
    For technical information, type: "Get-Help Get-HotFix -Full".
    For online help, type: "Get-Help Get-HotFix -Online"

您也可以找到缺少說明文章的 Get-Help命令,雖然這項功能並不是廣為人知的。 more 功能是其中一個沒有說明文章的命令。 若要確認您可以找到不包含說明文章的命令,請使用 Get-Help 函式來尋找 help中的 more

help *more*

搜尋只找到一個匹配項目,因此它會顯示命令沒有說明文件時顯示的基本語法信息。

NAME
    more

SYNTAX
    more [[-paths] <string[]>]

ALIASES
    None

REMARKS
    None

PowerShell 說明系統也包含與概念相關的 關於 說明文章。 您必須更新系統上的說明內容,以取得 關於 文章。 如需詳細資訊,請參閱本章的 更新說明 一節。

使用下列命令傳回系統上所有 About 相關說明文章的清單。

help About_*

當您將結果限製為一 About 說明文章時,Get-Help 會顯示該文章的內容。

help about_Updatable_Help

更新協助

本章稍早,您第一次執行 Get-Help Cmdlet 時,更新了計算機上的 PowerShell 說明文章。 您應該定期在計算機上執行 Update-Help Cmdlet,以取得說明內容的任何更新。

這很重要

在 Windows PowerShell 5.1 中,您必須在提升許可權的 PowerShell 會話中以系統管理員身分執行 Update-Help

在下列範例中,Update-Help 下載計算機上安裝之所有模組的PowerShell說明內容。 您應該使用 Force 參數,以確保您下載最新版本的說明內容。

Update-Help -Force

如下列結果所示,模組傳回錯誤。 錯誤並不常見,而且通常會在模組的作者未正確設定可更新的說明時發生。

Update-Help : Failed to update Help for the module(s) 'BitsTransfer' with UI
culture(s) {en-US} : Unable to retrieve the HelpInfo XML file for UI culture
en-US. Make sure the HelpInfoUri property in the module manifest is valid or
check your network connection and then try the command again.
At line:1 char:1
+ Update-Help
+ ~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (:) [Update-Help], Except
   ion
    + FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShe
   ll.Commands.UpdateHelpCommand

Update-Help 需要因特網存取才能下載說明內容。 如果您的電腦沒有因特網存取權,請使用具有因特網存取權的計算機上 Save-Help Cmdlet 來下載並儲存更新的說明內容。 然後,使用 Update-Help 參數來指定已儲存更新說明內容的位置。

Get-Command(取得指令)

Get-Command 是另一個多用途命令,可協助您尋找命令。 當您在沒有任何參數的情況下執行 Get-Command 時,它會傳回系統上所有 PowerShell 命令的清單。 您也可以使用 Get-Command 來取得類似 Get-Help的命令語法。

如何判斷 Get-Command的語法? 您可以使用 Get-Help 來顯示 Get-Command的說明文章,如本章的 Get-Help 一節所示。 您也可以使用 Get-Command 搭配 Syntax 參數來檢視任何命令的語法。 此快捷方式可協助您快速判斷如何使用命令,而不流覽其說明內容。

Get-Command -Name Get-Command -Syntax

使用 Get-Command 搭配 Syntax 參數提供更簡潔的語法展示,以顯示參數及其值的類型,而不列出像 Get-Help 所顯示的特定可允許值。

Get-Command [[-ArgumentList] <Object[]>] [-Verb <string[]>]
[-Noun <string[]>] [-Module <string[]>]
[-FullyQualifiedModule <ModuleSpecification[]>] [-TotalCount <int>]
[-Syntax] [-ShowCommandInfo] [-All] [-ListImported]
[-ParameterName <string[]>] [-ParameterType <PSTypeName[]>]
[<CommonParameters>]

Get-Command [[-Name] <string[]>] [[-ArgumentList] <Object[]>]
[-Module <string[]>] [-FullyQualifiedModule <ModuleSpecification[]>]
[-CommandType <CommandTypes>] [-TotalCount <int>] [-Syntax]
[-ShowCommandInfo] [-All] [-ListImported] [-ParameterName <string[]>]
[-ParameterType <PSTypeName[]>] [<CommonParameters>]

如果您需要有關如何使用命令的詳細資訊,請使用 Get-Help

help Get-Command -Full

Get-Help 區段藉由擴充參數的列舉值,提供更方便使用的顯示。 它會顯示您可以使用的實際值,讓您更容易瞭解可用的選項。

...
    Get-Command [[-Name] <System.String[]>] [[-ArgumentList]
    <System.Object[]>] [-All] [-CommandType {Alias | Function | Filter |
    Cmdlet | ExternalScript | Application | Script | Workflow |
    Configuration | All}] [-FullyQualifiedModule
    <Microsoft.PowerShell.Commands.ModuleSpecification[]>] [-ListImported]
    [-Module <System.String[]>] [-ParameterName <System.String[]>]
    [-ParameterType <System.Management.Automation.PSTypeName[]>]
    [-ShowCommandInfo] [-Syntax] [-TotalCount <System.Int32>]
    [<CommonParameters>]

    Get-Command [[-ArgumentList] <System.Object[]>] [-All]
    [-FullyQualifiedModule
    <Microsoft.PowerShell.Commands.ModuleSpecification[]>] [-ListImported]
    [-Module <System.String[]>] [-Noun <System.String[]>] [-ParameterName
    <System.String[]>] [-ParameterType
    <System.Management.Automation.PSTypeName[]>] [-ShowCommandInfo]
    [-Syntax] [-TotalCount <System.Int32>] [-Verb <System.String[]>]
    [<CommonParameters>]
...

說明的 Get-Command 區段顯示,NameNoun,以及 Verb 參數允許使用通配符。

...
    -Name <System.String[]>
        Specifies an array of names. This cmdlet gets only commands that
        have the specified name. Enter a name or name pattern. Wildcard
        characters are permitted.

        To get commands that have the same name, use the All parameter. When
        two commands have the same name, by default, `Get-Command` gets the
        command that runs when you type the command name.

        Required?                    false
        Position?                    0
        Default value                None
        Accept pipeline input?       True (ByPropertyName, ByValue)
        Accept wildcard characters?  true

    -Noun <System.String[]>
        Specifies an array of command nouns. This cmdlet gets commands,
        which include cmdlets, functions, and aliases, that have names that
        include the specified noun. Enter one or more nouns or noun
        patterns. Wildcard characters are permitted.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?       True (ByPropertyName)
        Accept wildcard characters?  true
    -Verb <System.String[]>
        Specifies an array of command verbs. This cmdlet gets commands,
        which include cmdlets, functions, and aliases, that have names that
        include the specified verb. Enter one or more verbs or verb
        patterns. Wildcard characters are permitted.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?       True (ByPropertyName)
        Accept wildcard characters?  true
...

下列範例使用 * 通配符,搭配用於 Get-Command 參數的值。

Get-Command -Name *service*

當您使用通配符搭配 Get-Command 參數時,它會傳回 PowerShell 指令和內建命令,如下列結果所示。


CommandType     Name                                               Version
-----------     ----                                               -------
Function        Get-NetFirewallServiceFilter                       2.0.0.0
Function        Set-NetFirewallServiceFilter                       2.0.0.0
Cmdlet          Get-Service                                        3.1.0.0
Cmdlet          New-Service                                        3.1.0.0
Cmdlet          New-WebServiceProxy                                3.1.0.0
Cmdlet          Restart-Service                                    3.1.0.0
Cmdlet          Resume-Service                                     3.1.0.0
Cmdlet          Set-Service                                        3.1.0.0
Cmdlet          Start-Service                                      3.1.0.0
Cmdlet          Stop-Service                                       3.1.0.0
Cmdlet          Suspend-Service                                    3.1.0.0
Application     SecurityHealthService.exe                          10.0.2...
Application     SensorDataService.exe                              10.0.2...
Application     services.exe                                       10.0.2...
Application     services.msc                                       0.0.0.0
Application     TieringEngineService.exe                           10.0.2...
Application     Windows.WARP.JITService.exe                        10.0.2...

您可以使用 Get-Command 參數,將 的結果限製為 PowerShell 命令。

Get-Command -Name *service* -CommandType Cmdlet, Function, Alias, Script

另一個選項可能是使用 動詞名詞 參數,或兩者都是因為只有 PowerShell 命令有動詞和名詞。

下列範例會使用 Get-Command,來尋找在您的電腦上與處理程序相關的命令。 使用 名詞 參數,並將 Process 指定為其值。

Get-Command -Noun Process
CommandType     Name                                               Version
-----------     ----                                               -------
Cmdlet          Debug-Process                                      3.1.0.0
Cmdlet          Get-Process                                        3.1.0.0
Cmdlet          Start-Process                                      3.1.0.0
Cmdlet          Stop-Process                                       3.1.0.0
Cmdlet          Wait-Process                                       3.1.0.0

總結

在本章中,您已瞭解如何使用 Get-HelpGet-Command尋找命令。 您也學會如何使用說明系統來理解發現命令後的使用方式。 此外,您已瞭解如何在有新的說明內容可用時更新電腦上的說明系統。

回顧

  1. DisplayName 參數在 Get-Service 位置嗎?
  2. Get-Process Cmdlet 有多少個參數集?
  3. 使用事件記錄檔時有哪些 PowerShell 命令?
  4. 傳回電腦上執行的PowerShell進程清單的PowerShell命令為何?
  5. 如何更新儲存在電腦上的PowerShell說明內容?

參考資料

若要深入瞭解本章所涵蓋的概念,請閱讀下列 PowerShell 說明文章。

後續步驟

在下一章中,您將了解對象、屬性、方法和 Get-Member Cmdlet。