PowerShell ืœืžืชื—ื™ืœื™ื

ื‘ืขื‘ื•ื“ื” ืขื PowerShell, ื”ื“ื‘ืจ ื”ืจืืฉื•ืŸ ืฉืื ื• ื ืชืงืœื™ื ื‘ื• ื”ื•ื ืคืงื•ื“ื•ืช (Cmdlets).
ืงืจื™ืืช ื”ืคืงื•ื“ื” ื ืจืื™ืช ื›ืš:

Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]

ืขึถื–ืจึธื”

ื ื™ืชืŸ ืœื’ืฉืช ืœืขื–ืจื” ื‘-PowerShell ื‘ืืžืฆืขื•ืช ื”ืคืงื•ื“ื” Get-Help. ื ื™ืชืŸ ืœืฆื™ื™ืŸ ืื—ื“ ืžื”ืคืจืžื˜ืจื™ื: ื“ื•ื’ืžื”, ืžืคื•ืจื˜, ืžืœื, ืžืงื•ื•ืŸ, showWindow.

Get-Help Get-Service -full ื™ื—ื–ื™ืจ ืืช ื”ืชื™ืื•ืจ ื”ืžืœื ืฉืœ ืคืขื•ืœืช ื”ืคืงื•ื“ื” Get-Service
Get-Help Get-S* ื™ืฆื™ื’ ืืช ื›ืœ ื”ืคืงื•ื“ื•ืช ื•ื”ืคื•ื ืงืฆื™ื•ืช ื”ื–ืžื™ื ื•ืช ื”ื—ืœ ืž-Get-S*

ื™ืฉ ื’ื ืชื™ืขื•ื“ ืžืคื•ืจื˜ ื‘ืืชืจ ื”ืจืฉืžื™ ืฉืœ ืžื™ืงืจื•ืกื•ืคื˜.

ื”ื ื” ื“ื•ื’ืžื” ืœืขื–ืจื” ืœืคืงื•ื“ืช Get-Evenlog

PowerShell ืœืžืชื—ื™ืœื™ื

ืื ืคืจืžื˜ืจื™ื ืžื•ืงืคื™ื ื‘ืกื•ื’ืจื™ื™ื ืžืจื•ื‘ืขื™ื [], ืื– ื”ื ืื•ืคืฆื™ื•ื ืœื™ื™ื.
ื›ืœื•ืžืจ, ื‘ื“ื•ื’ืžื” ื–ื• ื ื“ืจืฉ ืฉื ื”ื™ื•ืžืŸ ืขืฆืžื•, ื•ืฉื ื”ืคืจืžื˜ืจ ืœื. ืื ืกื•ื’ ื”ืคืจืžื˜ืจ ื•ืฉืžื• ืžื•ืงืคื™ื ื‘ืกื•ื’ืจื™ื™ื ื™ื—ื“, ืื– ืคืจืžื˜ืจ ื–ื” ื”ื•ื ืื•ืคืฆื™ื•ื ืœื™.

ืื ืืชื” ืžืกืชื›ืœ ืขืœ ื”ืคืจืžื˜ืจ EntryType, ืืชื” ื™ื›ื•ืœ ืœืจืื•ืช ืืช ื”ืขืจื›ื™ื ื”ืžื•ืงืคื™ื ื‘ืกื•ื’ืจื™ื™ื ืžืกื•ืœืกืœื™ื. ืขื‘ื•ืจ ืคืจืžื˜ืจ ื–ื”, ืื ื• ื™ื›ื•ืœื™ื ืœื”ืฉืชืžืฉ ืจืง ื‘ืขืจื›ื™ื ืžื•ื’ื“ืจื™ื ืžืจืืฉ ื‘ืกื•ื’ืจื™ื ืžืกื•ืœืกืœื™ื.

ืžื™ื“ืข ืœื’ื‘ื™ ื”ืื ื”ืคืจืžื˜ืจ ื ื“ืจืฉ ื ื™ืชืŸ ืœืจืื•ืช ื‘ืชื™ืื•ืจ ืฉืœื”ืœืŸ ื‘ืฉื“ื” ื—ื•ื‘ื”. ื‘ื“ื•ื’ืžื” ืฉืœืžืขืœื”, ื”ืชื›ื•ื ื” After ื”ื™ื ืื•ืคืฆื™ื•ื ืœื™ืช ืžื›ื™ื•ื•ืŸ ืฉ-Required ืžื•ื’ื“ืจ ื›-false. ืœืื—ืจ ืžื›ืŸ, ืื ื• ืจื•ืื™ื ืืช ื”ืฉื“ื” Position ืžืžื•ืœ ืฉืื•ืžืจ ื‘ืฉื. ื–ื” ืื•ืžืจ ืฉืืชื” ื™ื›ื•ืœ ืœื”ืชื™ื™ื—ืก ืœืคืจืžื˜ืจ ืจืง ืœืคื™ ื”ืฉื, ื›ืœื•ืžืจ:

Get-EventLog -LogName Application -After 2020.04.26

ืžื›ื™ื•ื•ืŸ ืฉืœืคืจืžื˜ืจ LogName ื”ื™ื” ื”ืžืกืคืจ 0 ื‘ืžืงื•ื Named, ื–ื” ืื•ืžืจ ืฉืื ื—ื ื• ื™ื›ื•ืœื™ื ืœื”ืชื™ื™ื—ืก ืœืคืจืžื˜ืจ ืœืœื ืฉื, ืืœื ืขืœ ื™ื“ื™ ืฆื™ื•ืŸ ืื•ืชื• ื‘ืจืฆืฃ ื”ืจืฆื•ื™:

Get-EventLog Application -After 2020.04.26

ื ื ื™ื— ืืช ื”ืกื“ืจ ื”ื–ื”:

Get-EventLog -Newest 5 Application

ื›ื™ื ื•ื™

ื›ื“ื™ ืฉื ื•ื›ืœ ืœื”ืฉืชืžืฉ ื‘ืคืงื•ื“ื•ืช ื”ืจื’ื™ืœื•ืช ืžื”ืžืกื•ืฃ ื‘-PowerShell, ื™ืฉื ื ื›ื™ื ื•ื™ื™ื (Alias).

ื›ื™ื ื•ื™ ื“ื•ื’ืžื” ืœืคืงื•ื“ืช Set-Location ื”ื•ื cd.

ื›ืœื•ืžืจ, ื‘ืžืงื•ื ืœืงืจื•ื ืœืคืงื•ื“ื”

Set-Location โ€œD:โ€

ืื ื—ื ื• ื™ื›ื•ืœื™ื ืœื”ืฉืชืžืฉ

cd โ€œD:โ€

ื”ึดื™ืกื˜ื•ึนืจึดื™ึธื”

ื›ื“ื™ ืœืจืื•ืช ืืช ื”ื”ื™ืกื˜ื•ืจื™ื” ืฉืœ ืฉื™ื—ื•ืช ืคืงื•ื“ื”, ืืชื” ื™ื›ื•ืœ ืœื”ืฉืชืžืฉ ื‘-Get-History

ื‘ืฆืข ืคืงื•ื“ื” ืžื”ื”ื™ืกื˜ื•ืจื™ื” Invoke-History 1; ืงืจื ื”ื™ืกื˜ื•ืจื™ื” 2

ื ืงื” ื”ื™ืกื˜ื•ืจื™ื”

ืฆื™ื ื•ืจ

ืฆื™ื ื•ืจ ื‘-powershell ื”ื•ื ื›ืืฉืจ ื”ืชื•ืฆืื” ืฉืœ ื”ืคื•ื ืงืฆื™ื” ื”ืจืืฉื•ื ื” ืžื•ืขื‘ืจืช ืœืฉื ื™ื™ื”. ื”ื ื” ื“ื•ื’ืžื” ื‘ืืžืฆืขื•ืช ื”ืฆื™ื ื•ืจ:

Get-Verb | Measure-Object

ืื‘ืœ ื›ื“ื™ ืœื”ื‘ื™ืŸ ื˜ื•ื‘ ื™ื•ืชืจ ืืช ื”ืฆื™ื ื•ืจ, ื ื™ืงื— ื“ื•ื’ืžื” ืคืฉื•ื˜ื” ื™ื•ืชืจ. ื™ืฉ ืฆื•ื•ืช

Get-Verb "get"

ืื ืชืงืจื ืœืขื–ืจื” Get-Help Get-Verb -Full, ืื– ื ืจืื” ืฉื”ืคืจืžื˜ืจ Verb ืžืงื‘ืœ ืงืœื˜ pipline ื•-ByValue ื›ืชื•ื‘ ื‘ืกื•ื’ืจื™ื™ื.

PowerShell ืœืžืชื—ื™ืœื™ื

ื–ื” ืื•ืžืจ ืฉืื ื—ื ื• ื™ื›ื•ืœื™ื ืœืฉื›ืชื‘ ืืช Get-Verb "ืœืงื‘ืœ" ืœ"ืงื‘ืœ" | ืงื‘ืœ ืคื•ืขืœ.
ื›ืœื•ืžืจ, ื”ืชื•ืฆืื” ืฉืœ ื”ื‘ื™ื˜ื•ื™ ื”ืจืืฉื•ืŸ ื”ื™ื ืžื—ืจื•ื–ืช ื•ื”ื™ื ืžื•ืขื‘ืจืช ืœืคืจืžื˜ืจ Verb ืฉืœ ื”ืคืงื•ื“ื” Get-Verb ื‘ืืžืฆืขื•ืช ืงืœื˜ ืœืคื™ ืขืจืš.
ื’ื ืงืœื˜ ืฆื™ื ื•ืจ ื™ื›ื•ืœ ืœื”ื™ื•ืช ByPropertyName. ื‘ืžืงืจื” ื–ื”, ื ืขื‘ื™ืจ ืื•ื‘ื™ื™ืงื˜ ื‘ืขืœ ืžืืคื™ื™ืŸ ื‘ืขืœ ืฉื ื“ื•ืžื” Verb.

ืžืฉืชื ื™ื

ืžืฉืชื ื™ื ืื™ื ื ืžื•ืงืœื“ื™ื ื”ื™ื˜ื‘ ื•ื”ื ืžืฆื•ื™ื ื™ื ืขื $ ืžืœืคื ื™ื

$example = 4

ื”ืกืžืœ > ืคื™ืจื•ืฉื• ืœื”ื›ื ื™ืก ืืช ื”ื ืชื•ื ื™ื
ืœื“ื•ื’ืžื”, $example > File.txt
ืขื ื”ื‘ื™ื˜ื•ื™ ื”ื–ื”, ื ื›ื ื™ืก ืืช ื”ื ืชื•ื ื™ื ืžื”ืžืฉืชื ื” $example ืœืงื•ื‘ืฅ
ื–ื”ื” ืœ-Set-Content -Value $example -Path File.txt

ืžืขืจื›ื™ื

ืืชื—ื•ืœ ืžืขืจืš:

$ArrayExample = @(โ€œFirstโ€, โ€œSecondโ€)

ืืชื—ื•ืœ ืžืขืจืš ืจื™ืง:

$ArrayExample = @()

ืงื‘ืœืช ืขืจืš ืœืคื™ ืื™ื ื“ืงืก:

$ArrayExample[0]

ืงื‘ืœ ืืช ื›ืœ ื”ืžืขืจืš:

$ArrayExample

ื”ื•ืกืคืช ืืœืžื ื˜:

$ArrayExample += โ€œThirdโ€

$ArrayExample += @(โ€œFourthโ€, โ€œFifthโ€)

ืžื™ื™ืŸ ืœืคื™:

$ArrayExample | Sort

$ArrayExample | Sort -Descending

ืื‘ืœ ื”ืžืขืจืš ืขืฆืžื• ื ืฉืืจ ืœืœื ืฉื™ื ื•ื™ ืขื ื”ืžื™ื•ืŸ ื”ื–ื”. ื•ืื ืื ื—ื ื• ืจื•ืฆื™ื ืฉืœืžืขืจืš ื™ื”ื™ื• ื ืชื•ื ื™ื ืžืžื•ื™ื ื™ื, ืื– ืื ื—ื ื• ืฆืจื™ื›ื™ื ืœื”ืงืฆื•ืช ืืช ื”ืขืจื›ื™ื ื”ืžืžื•ื™ื ื™ื:

$ArrayExample = $ArrayExample | Sort

ืื™ืŸ ื“ืจืš ืœื”ืกื™ืจ ืืœืžื ื˜ ืžืžืขืจืš ื‘- PowerShell, ืื‘ืœ ืืชื” ื™ื›ื•ืœ ืœืขืฉื•ืช ื–ืืช ื›ืš:

$ArrayExample = $ArrayExample | where { $_ -ne โ€œFirstโ€ }

$ArrayExample = $ArrayExample | where { $_ -ne $ArrayExample[0] }

ื”ืกืจืช ืžืขืจืš:

$ArrayExample = $null

ืœื•ืœืื•ืช

ืชื—ื‘ื™ืจ ืœื•ืœืื”:

for($i = 0; $i -lt 5; $i++){}

$i = 0
while($i -lt 5){}

$i = 0
do{} while($i -lt 5)

$i = 0
do{} until($i -lt 5)

ForEach($item in $items){}

ืฆื ืžืœื•ืœืืช ื”ืคืกืงื”.

ื“ืœื’ ืขืœ ืจื›ื™ื‘ ื”ื”ืžืฉืš.

ื”ืฆื”ืจื•ืช ืขืœ ืชื ืื™

if () {} elseif () {} else

switch($someIntValue){
  1 { โ€œOption 1โ€ }
  2 { โ€œOption 2โ€ }
  default { โ€œNot setโ€ }
}

ืคื•ื ืงืฆื™ื”

ื”ื’ื“ืจืช ืคื•ื ืงืฆื™ื”:

function Example () {
  echo &args
}

ื”ืฉืงืช ืคื•ื ืงืฆื™ื”:

Example โ€œFirst argumentโ€ โ€œSecond argumentโ€

ื”ื’ื“ืจืช ืืจื’ื•ืžื ื˜ื™ื ื‘ืคื•ื ืงืฆื™ื”:

function Example () {
  param($first, $second)
}

function Example ($first, $second) {}

ื”ืฉืงืช ืคื•ื ืงืฆื™ื”:

Example -first โ€œFirst argumentโ€ -second โ€œSecond argumentโ€

ื—ืจื™ื’ื”

try{
} catch [System.Net.WebException],[System.IO.IOException]{
} catch {
} finally{
}

ืžืงื•ืจ: www.habr.com

ื”ื•ืกืคืช ืชื’ื•ื‘ื”