Cmdlety bez tajemnic, czyli jak działa składnia PowerShella?

Jak działa składnia PowerShella?

Czym jest Cmdlet?

Konwencja nazewnicza Verb-Noun

Czasownik (Verb)

Rzeczownik (Noun)

Parametry, czyli większa kontrola

PS C:\Users\Admin> Get-Process -Name Notepad

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     45    57.92     119.78       3.03    2292   2 Notepad

Składnia w PowerShellu

PS C:\Users\Admin> (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>] 

Parametry obowiązkowe

PS C:\Users\Admin> Get-EventLog -LogName Application

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   44283 Sept 10 18:25 Information Software Protection…   1073758208 Successfully scheduled Software Protection servic…
   44282 Sept 10 18:25 Information Software Protection…   3221241866 Offline downlevel migration succeeded.
   44281 Sept 10 18:23 Information SecurityCenter                 15 Updated Windows Defender status successfully to S…
   44280 Sept 10 18:23 Information SecurityCenter                 15 Updated Windows Defender status successfully to S…
   44279 Sept 10 18:18 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   44278 Sept 10 18:18 Information BrYNSvc                         0 The description for Event ID '0' in Source 'BrYNS…
PS C:\Users\Admin> Get-EventLog Application

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   44283 Sept 10 18:25 Information Software Protection…   1073758208 Successfully scheduled Software Protection servic…
   44282 Sept 10 18:25 Information Software Protection…   3221241866 Offline downlevel migration succeeded.
   44281 Sept 10 18:23 Information SecurityCenter                 15 Updated Windows Defender status successfully to S…
   44280 Sept 10 18:23 Information SecurityCenter                 15 Updated Windows Defender status successfully to S…
   44279 Sept 10 18:18 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   44278 Sept 10 18:18 Information BrYNSvc                         0 The description for Event ID '0' in Source 'BrYNS…
PS C:\Users\Admin> Get-EventLog -LogName
Get-EventLog: Missing an argument for parameter 'LogName'. Specify a parameter of type 'System.Object' and try again.
PS C:\Users\Admin> Get-EventLog

cmdlet Get-EventLog at command pipeline position 1
Supply values for the following parameters:
LogName:
Get-EventLog: Cannot bind argument to parameter 'LogName' because it is an empty string.

Inne parametry

PS C:\Users\Admin> Get-EventLog -LogName Application -EntryType Error

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   45661 Sept 18 11:19 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
   45659 Sept 18 11:17 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
   45658 Sept 18 11:17 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
   45392 Sept 15 21:28 Error       Microsoft-Windows-R…        10006 Application or service Microsoft Office SDX Help…
   45071 Sept 14 21:15 Error       Microsoft-Windows-R…        10006 Application or service Microsoft Office SDX Help…
   45029 Sept 13 05:45 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
   45028 Sept 13 05:43 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
   45027 Sept 13 05:40 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
   45026 Sept 13 05:32 Error       .NET Runtime                 1022 .NET Runtime version 4.0.30319.0 - Wystąpił błąd …
PS C:\Users\Admin> Get-EventLog -LogName Application -EntryType Message
Get-EventLog: Cannot validate argument on parameter 'EntryType'. The argument "Message" does not belong to the set "Error,Information,FailureAudit,SuccessAudit,Warning" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.
Get-EventLog -LogName Application -EntryType Error

Kolejność

PS C:\Users\Admin> Get-EventLog Application 0,1

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   45678 Sept 18 11:42 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   45677 Sept 18 11:42 Information BrYNSvc                         0 The description for Event ID '0' in Source BrYNS…
   45633 Sept 18 10:17 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   45632 Sept 18 10:17 Information BrYNSvc                         0 The description for Event ID '0' in Source BrYNS…
   45631 Sept 18 10:17 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   45630 Sept 18 10:17 Information BrYNSvc                         0 The description for Event ID '0' in Source 'BrYNS…
   45596 Sept 18 09:37 Information edgeupdate                      0 Service stopped.
PS C:\Users\Admin> Get-EventLog 0,1 Application
Get-EventLog: Cannot bind parameter 'InstanceId'. Cannot convert value "Application" to type "System.Int64". Error: "Input string was not in a correct format."
PS C:\Users\Admin> Get-EventLog -InstanceId 0,1 Application

   Index Time          EntryType   Source                 InstanceID Message
   ----- ----          ---------   ------                 ---------- -------
   45678 Sept 18 11:42 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   45677 Sept 18 11:42 Information BrYNSvc                         0 The description for Event ID '0' in Source 'BrYNS…
   45633 Sept 18 10:17 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   45632 Sept 18 10:17 Information BrYNSvc                         0 The description for Event ID '0' in Source 'BrYNS…
   45631 Sept 18 10:17 Information igcc                            0 Obsługa elementu PowerEvent przez usługę została …
   45630 Sept 18 10:17 Information BrYNSvc                         0 The description for Event ID '0' in Source 'BrYNS…
   45596 Sept 18 09:37 Information edgeupdate                      0 Service stopped.

Parametry wspólne (Common Parameters)

PS C:\Users\Admin> Clear-RecycleBin -WhatIf
What if: Performing the operation "Clear-RecycleBin" on target "All of the contents of the Recycle Bin".

Jak uczyć się cmdletów

PS C:\Users\Admin> Get-Command

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           Add-AdlAnalyticsDataSource                         1.0.3      Az.DataLakeAnalytics
Alias           Add-AdlAnalyticsFirewallRule                       1.0.3      Az.DataLakeAnalytics
Alias           Add-AdlStoreFirewallRule                           1.3.0      Az.DataLakeStore
Alias           Add-AdlStoreItemContent                            1.3.0      Az.DataLakeStore
Alias           Add-AdlStoreTrustedIdProvider                      1.3.0      Az.DataLakeStore
Alias           Add-AdlStoreVirtualNetworkRule                     1.3.0      Az.DataLakeStore
Alias           Add-AppPackage                                     2.0.1.0    Appx
PS C:\Users\Admin> Get-Command -Verb Uninstall

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Uninstall-Dtc                                      1.0.0.0    MsDtc
Function        Uninstall-Module                                   2.2.5      PowerShellGet
Function        Uninstall-Module                                   2.2.5      PowerShellGet
Function        Uninstall-Module                                   1.0.0.1    PowerShellGet
Function        Uninstall-Script                                   2.2.5      PowerShellGet
Function        Uninstall-Script                                   2.2.5      PowerShellGet
Function        Uninstall-Script                                   1.0.0.1    PowerShellGet
Cmdlet          Uninstall-ADServiceAccount                         1.0.1.0    ActiveDirectory
Cmdlet          Uninstall-AzureRm                                  2.13.0     Az.Accounts
Cmdlet          Uninstall-Language                                 1.0        LanguagePackManagement
Cmdlet          Uninstall-Package                                  1.4.8.1    PackageManagement
Cmdlet          Uninstall-ProvisioningPackage                      3.0        Provisioning
Cmdlet          Uninstall-PSResource                               1.1.1      Microsoft.PowerShell.PSResourceGet
Cmdlet          Uninstall-TrustedProvisioningCertificate           3.0        Provisioning
Cmdlet          Uninstall-WindowsFeature                           2.0.0.0    ServerManager
PS C:\Users\Admin> Get-Command -Noun Service

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-Service                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          New-Service                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Remove-Service                                     7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Restart-Service                                    7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Resume-Service                                     7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Set-Service                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Start-Service                                      7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Stop-Service                                       7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Suspend-Service                                    7.0.0.0    Microsoft.PowerShell.Management
PS C:\Users\Admin> Get-Command -Module Microsoft.PowerShell.Management

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Add-Content                                        7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Clear-Content                                      7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Clear-Item                                         7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Clear-ItemProperty                                 7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Clear-RecycleBin                                   7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Convert-Path                                       7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Copy-Item                                          7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Copy-ItemProperty                                  7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Debug-Process                                      7.0.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-ChildItem                                      7.0.0.0    Microsoft.PowerShell.Management

Find-Command

PS C:\Users\Admin> Find-Command -Repository PSGallery | Where-Object Name -like '*Azure*'

Name                                Version    ModuleName                          Repository
----                                -------    ----------                          ----------
New-AzActionGroupAzureAppPushRecei… 6.0.3      Az.Monitor                          PSGallery
New-AzActionGroupAzureFunctionRece… 6.0.3      Az.Monitor                          PSGallery
New-AzEventGridAzureFunctionEventS… 2.2.0      Az.EventGrid                        PSGallery
New-AzWebAppAzureStoragePath        3.4.1      Az.Websites                         PSGallery
Get-AzSecurityConnectorAzureDevOps… 1.8.0      Az.Security                         PSGallery
Get-AzSecurityConnectorAzureDevOps… 1.8.0      Az.Security                         PSGallery
Get-AzSecurityConnectorAzureDevOps… 1.8.0      Az.Security                         PSGallery
Get-AzSecurityConnectorAzureDevOps… 1.8.0      Az.Security                         PSGallery
New-AzSecurityAzureDevOpsScopeEnvi… 1.8.0      Az.Security                         PSGallery

Get-Help

PS C:\Users\Admin> Get-Help Get-Process

NAME
    Get-Process

SYNTAX
    Get-Process [[-Name] <string[]>] [-Module] [-FileVersionInfo] [<CommonParameters>]

    Get-Process [[-Name] <string[]>] -IncludeUserName [<CommonParameters>]

    Get-Process -Id <int[]> [-Module] [-FileVersionInfo] [<CommonParameters>]

    Get-Process -Id <int[]> -IncludeUserName [<CommonParameters>]

    Get-Process -InputObject <Process[]> [-Module] [-FileVersionInfo] [<CommonParameters>]

    Get-Process -InputObject <Process[]> -IncludeUserName [<CommonParameters>]


ALIASES
    gps
    ps


REMARKS
    Get-Help cannot find the Help files for this cmdlet on this computer. It is displaying only partial help.
        -- To download and install Help files for the module that includes this cmdlet, use Update-Help.
        -- To view the Help topic for this cmdlet online, type: "Get-Help Get-Process -Online" or
           go to https://go.microsoft.com/fwlink/?LinkID=2096814.

Aktualizacja plików pomocy

Update-Help

Aliasy, przydatne, ale używaj z rozwagą

Potok (Pipeline) — współpraca cmdletów

PS C:\Users\Admin> Get-Process | Where-Object {$_.CPU -gt 100}

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
     17     3.57       3.58     258.52    3160   1 Apoint
   1258   568.34     587.61  26,858.06    6864   1 chrome
     77   604.91     376.09  18,077.42    8572   1 chrome
     58   279.18     241.08     120.78   10760   1 chrome
     84   435.97     386.44     970.12   10988   1 chrome

Podsumowanie


To wszystko na dziś!

Jeśli masz ciekawe spostrzeżenia lub doświadczenia w tym temacie – koniecznie podziel się nimi w komentarzach.
A jeśli moje materiały są dla Ciebie pomocne, możesz postawić mi wirtualną kawę.

Dzięki za wsparcie!

Postaw mi kawę na buycoffee.to

Adam Pietrzak

Trener IT | Autor szkoleń | Entuzjasta PowerShella

Administrator systemów i sieci wsparcia działań wojskowych z ponad 10-letnim doświadczeniem. Praktyk w dziedzinie bezpieczeństwa systemu Windows, automatyzacji zadań (PowerShell) oraz rozwiązań chmurowych. Trener i twórca materiałów edukacyjnych (szkolenia, warsztaty, artykuły, podręczniki). Pasjonat dzielenia się wiedzą i wspierania początkujących administratorów IT. Prywatnie – amator aktywnego wypoczynku i rodzinnych podróży.


PowerShell - 10 sposobów na optymalizację codziennej pracy
Podziel się

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *