Powerhell untuk pemula

Saat bekerja dengan PowerShell, hal pertama yang kami temui adalah perintah (Cmdlet).
Panggilan perintah terlihat seperti ini:


Verb-Noun -Parameter1 ValueType1 -Parameter2 ValueType2[]

Tolong


Bantuan dipanggil di PowerShell menggunakan perintah Dapatkan-Bantuan. Anda dapat menentukan salah satu parameter: contoh, terperinci, lengkap, online, showWindow.

Get-Help Get-Service -full akan mengembalikan deskripsi lengkap tentang perintah Get-Service Get-
Help Get-S * akan menampilkan semua perintah dan fungsi yang tersedia dimulai dengan Get-S *


Ada juga dokumentasi terperinci di situs web resmi Microsoft.


Berikut adalah contoh bantuan untuk perintah Get-Evenlog


gambar

[], .
, . , .


EntryType, . .


Required. After , Required false. Position Named. , :


Get-EventLog -LogName Application -After 2020.04.26

LogName Named 0 , :


Get-EventLog Application -After 2020.04.26

:


Get-EventLog -Newest 5 Application

Alias


PowerShell (Alias).


Set-Location cd.



Set-Location “D:\”


cd “D:\”

History


Get-History


Invoke-History 1; Invoke-History 2


Clear-History


Pipeline


powershell . :


Get-Verb | Measure-Object

.


Get-Verb "get"

Get-Help Get-Verb -Full, Verb pipline input ByValue.


gambar

Get-Verb «get» «get» | Get-Verb.
Verb Get-Verb pipline input .
pipline input ByPropertyName. Verb.


Variables


$


$example = 4

>
, $example > File.txt
$example
Set-Content -Value $example -Path File.txt


Arrays


Inisialisasi array:


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

Menginisialisasi array kosong:


$ArrayExample = @()

Mendapatkan nilai berdasarkan indeks:


$ArrayExample[0]

Dapatkan seluruh susunan:


$ArrayExample

Menambahkan item:


$ArrayExample += “Third”

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

Penyortiran:


$ArrayExample | Sort

$ArrayExample | Sort -Descending

Tetapi array itu sendiri dengan penyortiran ini tetap tidak berubah. Dan jika kita ingin mengurutkan data dalam array, maka kita perlu menetapkan nilai yang diurutkan:


$ArrayExample = $ArrayExample | Sort

Bahkan, tidak ada elemen yang dihapus dari array di PowerShell, tetapi Anda bisa melakukannya dengan cara ini:


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

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

Hapus array:


$ArrayExample = $null

Loop


Sintaks loop:


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

Keluar dari loop istirahat.


Lewati terus.


Pernyataan bersyarat


if () {} elseif () {} else

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

Fungsi


Definisi fungsi:


function Example () {
  echo &args
}

Peluncuran fungsi:


Example “First argument” “Second argument”

Mendefinisikan argumen dalam suatu fungsi:


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

function Example ($first, $second) {}

Peluncuran fungsi:


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

Pengecualian


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

All Articles