Powerhell للمبتدئين

عند العمل مع 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, . .


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.


صورة

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


تهيئة الصفيف:


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

All Articles