PowerShell rau beginners

Thaum ua haujlwm nrog PowerShell, thawj qhov uas peb ntsib yog cov lus txib (Cmdlets).
Cov lus txib hu zoo li no:

Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]

pab

Pab hauv PowerShell yog nkag tau siv Get-Help hais kom ua. Ib qho ntawm cov kev txwv tuaj yeem teev tseg: piv txwv, nthuav dav, tag nrho, online, showWindow.

Get-Help Get-Service -full yuav rov qab tag nrho cov lus piav qhia ntawm kev ua haujlwm ntawm Get-Service hais kom ua
Get-Help Get-S * yuav qhia tag nrho cov lus txib muaj thiab ua haujlwm pib nrog Get-S *

Kuj tseem muaj cov ntaub ntawv ntxaws ntxaws ntawm Microsoft lub vev xaib official.

Nov yog ib qho piv txwv pab rau Get-Evenlog hais kom ua

PowerShell rau beginners

Yog hais tias tsis muaj nyob rau hauv square brackets [], ces lawv yog xaiv tau.
Ntawd yog, hauv qhov piv txwv no, lub npe ntawm lub cav nws tus kheej yuav tsum tau, thiab lub npe ntawm qhov ntsuas Tsis muaj. Yog hais tias tus parameter hom thiab nws lub npe yog nyob rau hauv lub brackets ua ke, ces qhov no parameter yog xaiv tau.

Yog tias koj saib ntawm EntryType parameter, koj tuaj yeem pom cov txiaj ntsig uas tau muab kaw hauv curly brackets. Rau qhov kev ntsuas no, peb tsuas tuaj yeem siv qhov tseem ceeb ua ntej hauv curly braces.

Cov ntaub ntawv hais txog seb qhov parameter puas yuav tsum tau pom nyob rau hauv cov lus piav qhia hauv qab no hauv Daim Ntawv Thov. Hauv qhov piv txwv saum toj no, Tom qab tus cwj pwm yog xaiv tau vim tias Yuav Tsum tau teeb tsa rau qhov tsis tseeb. Tom ntej no, peb pom Txoj Haujlwm teb rov qab uas hais tias Npe. Qhov no txhais tau tias koj tuaj yeem xa mus rau qhov parameter nkaus xwb los ntawm lub npe, uas yog:

Get-EventLog -LogName Application -After 2020.04.26

Txij li thaum LogName parameter muaj tus lej 0 tsis yog Named, qhov no txhais tau hais tias peb tuaj yeem xa mus rau qhov tsis muaj npe, tab sis los ntawm kev qhia nws hauv qhov xav tau ua ntu zus:

Get-EventLog Application -After 2020.04.26

Cia peb xav tias qhov kev txiav txim no:

Get-EventLog -Newest 5 Application

cai

Yog li ntawd peb tuaj yeem siv cov lus txib ib txwm los ntawm lub console hauv PowerShell, muaj npe (Alias).

Ib qho piv txwv alias rau Set-Location hais kom ua yog cd.

Qhov ntawd yog, es tsis txhob hu cov lus txib

Set-Location “D:”

peb siv tau

cd “D:”

Keeb kwm

Txhawm rau saib keeb kwm ntawm kev hu xov tooj, koj tuaj yeem siv Get-History

Ua cov lus txib los ntawm keeb kwm Invoke-History 1; Hu Keeb Kwm 2

Clear-History

Pipeline

Ib lub raj xa dej hauv powershell yog thaum qhov tshwm sim ntawm thawj qhov ua haujlwm dhau mus rau qhov thib ob. Ntawm no yog ib qho piv txwv uas siv lub raj xa dej:

Get-Verb | Measure-Object

Tab sis kom nkag siab zoo dua cov kav dej, cia peb ua piv txwv yooj yim dua. Tau ib pab neeg

Get-Verb "get"

Yog tias koj hu rau Get-Help Get-Verb -Full help, ces peb yuav pom tias Verb parameter yuav siv cov tswv yim pipline thiab ByValue yog sau rau hauv cov kab lus.

PowerShell rau beginners

Qhov no txhais tau hais tias peb tuaj yeem sau dua Get-Verb "tau" rau "tau" | GetVerb.
Ntawd yog, qhov tshwm sim ntawm thawj qhov kev qhia yog ib txoj hlua thiab nws raug xa mus rau Verb parameter ntawm Get-Verb hais kom ua ntawm pipline input los ntawm tus nqi.
Kuj pipline input tuaj yeem yog ByPropertyName. Hauv qhov no, peb yuav dhau ib yam khoom uas muaj cov cuab yeej nrog lub npe zoo sib xws Verb.

ntau yam

Cov kev hloov pauv tsis yog ntaus ntawv zoo heev thiab tau teev nrog tus $ pem hauv ntej

$example = 4

Lub cim > txhais tau tias muab cov ntaub ntawv tso rau hauv
Piv txwv li, $example > File.txt
Nrog rau qhov kev qhia no, peb yuav muab cov ntaub ntawv los ntawm $ piv txwv hloov pauv mus rau hauv cov ntaub ntawv
Zoo ib yam li Teeb-Cov ntsiab lus -Value $example -Path File.txt

Arrays

Array pib:

$ArrayExample = @(“First”, “Second”)

Empty array pib:

$ArrayExample = @()

Tau txais tus nqi los ntawm index:

$ArrayExample[0]

Tau tag nrho array:

$ArrayExample

Ntxiv ib lub ntsiab:

$ArrayExample += “Third”

$ArrayExample += @(“Fourth”, “Fifth”)

Qhov:

$ArrayExample | Sort

$ArrayExample | Sort -Descending

Tab sis cov array nws tus kheej tseem tsis hloov nrog qhov kev txheeb xyuas no. Thiab yog tias peb xav kom cov array muaj cov ntaub ntawv txheeb xyuas, ces peb yuav tsum tau muab cov nqi txheeb xyuas:

$ArrayExample = $ArrayExample | Sort

Tsis muaj txoj hauv kev los tshem tawm cov khoom los ntawm ib qho array hauv PowerShell, tab sis koj tuaj yeem ua nws zoo li no:

$ArrayExample = $ArrayExample | where { $_ -ne “First” }

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

Tshem tawm ib qho array:

$ArrayExample = $null

Loops

Loop syntax:

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){}

Tawm ntawm lub voj voog tawg.

Hla qhov txuas ntxiv.

Cov Lus Ceeb Toom

if () {} elseif () {} else

switch($someIntValue){
  1 { “Option 1” }
  2 { “Option 2” }
  default { “Not set” }
}

muaj nuj nqi

Function txhais:

function Example () {
  echo &args
}

Function launch:

Example “First argument” “Second argument”

Defining arguments nyob rau hauv ib tug function:

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

function Example ($first, $second) {}

Function launch:

Example -first “First argument” -second “Second argument”

Tshaj tawm

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

Tau qhov twg los: www.hab.com

Ntxiv ib saib