PowerShell๋ก ์์
ํ ๋ ๊ฐ์ฅ ๋จผ์ ์ ํ๋ ๊ฒ์ ๋ช
๋ น(Cmdlet)์
๋๋ค.
๋ช
๋ น ํธ์ถ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]
๋์๋ง
PowerShell์ ๋์๋ง์ Get-Help ๋ช ๋ น์ ์ฌ์ฉํ์ฌ ์ก์ธ์คํฉ๋๋ค. ๋ค์ ๋งค๊ฐ๋ณ์ ์ค ํ๋๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค: ์, ์์ธ, ์ ์ฒด, ์จ๋ผ์ธ, showWindow.
Get-Help Get-Service -full์ Get-Service ๋ช
๋ น ์์
์ ๋ํ ์ ์ฒด ์ค๋ช
์ ๋ฐํํฉ๋๋ค.
Get-Help Get-S*๋ Get-S*๋ก ์์ํ๋ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ ๋ช
๋ น๊ณผ ๊ธฐ๋ฅ์ ํ์ํฉ๋๋ค.
Microsoft ๊ณต์ ์น ์ฌ์ดํธ์๋ ์์ธํ ์ค๋ช ์๊ฐ ์์ต๋๋ค.
๋ค์์ Get-Evenlog ๋ช ๋ น์ ๋ํ ์์ ๋์๋ง์ ๋๋ค.
๋งค๊ฐ๋ณ์๊ฐ ๋๊ดํธ[]๋ก ๋ฌถ์ฌ ์์ผ๋ฉด ์ ํ ์ฌํญ์
๋๋ค.
์ฆ, ์ด ์์์๋ ๋ก๊ทธ ์์ฒด์ ์ด๋ฆ์ด ํ์ํ๊ณ ๋งค๊ฐ๋ณ์์ ์ด๋ฆ์ด
EntryType ๋งค๊ฐ ๋ณ์๋ฅผ ๋ณด๋ฉด ์ค๊ดํธ๋ก ๋ฌถ์ธ ๊ฐ์ ๋ณผ ์ ์์ต๋๋ค. ์ด ๋งค๊ฐ๋ณ์์ ๊ฒฝ์ฐ ์ค๊ดํธ ์์ ๋ฏธ๋ฆฌ ์ ์๋ ๊ฐ๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋งค๊ฐ๋ณ์๊ฐ ํ์์ธ์ง ์ฌ๋ถ์ ๋ํ ์ ๋ณด๋ ํ์ ํ๋์ ์๋ ์ค๋ช ์์ ํ์ธํ ์ ์์ต๋๋ค. ์์ ์์์ ํ์๊ฐ false๋ก ์ค์ ๋์ด ์๊ธฐ ๋๋ฌธ์ After ํน์ฑ์ ์ ํ ์ฌํญ์ ๋๋ค. ๋ค์์ผ๋ก Named๋ผ๊ณ ์ ํ Position ํ๋๊ฐ ๋ณด์ ๋๋ค. ์ด๋ ๋ค์๊ณผ ๊ฐ์ด ์ด๋ฆ์ผ๋ก๋ง ๋งค๊ฐ๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์์์ ์๋ฏธํฉ๋๋ค.
Get-EventLog -LogName Application -After 2020.04.26
LogName ๋งค๊ฐ๋ณ์์๋ Named ๋์ ์ซ์ 0์ด ์์ผ๋ฏ๋ก ์ด๋ฆ ์์ด ๋งค๊ฐ๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์์ง๋ง ์ํ๋ ์์๋ก ์ง์ ํ๋ฉด ๋ฉ๋๋ค.
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๊ฐ ๊ดํธ ์์ ์ฐ์ฌ ์์์ ์ ์ ์์ต๋๋ค.
์ด๋ Get-Verb "get"์ "get"์ผ๋ก ๋ค์ ์ธ ์ ์์์ ์๋ฏธํฉ๋๋ค | GetVerb.
์ฆ, ์ฒซ ๋ฒ์งธ ์์ ๊ฒฐ๊ณผ๋ ๋ฌธ์์ด์ด๋ฉฐ ๊ฐ์ผ๋ก pipline ์
๋ ฅ์ ํตํด Get-Verb ๋ช
๋ น์ 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{
}
์ถ์ฒ : habr.com