ค้นหาเว็บไซต์

ตัวดำเนินการ PowerShell [คู่มือฉบับสมบูรณ์]


เมื่อใช้ PowerShell คุณสามารถใช้ตัวดำเนินการได้หลากหลายในสคริปต์ของคุณ สามารถใช้ในคำสั่งหรือนิพจน์ และใช้เพื่อทำการเปรียบเทียบ กำหนดเงื่อนไข หรือกำหนดและจัดการค่า

หากคุณใช้ PowerShell 7 คุณสามารถใช้ตัวดำเนินการ Ternary ใหม่ได้ สิ่งเหล่านี้ช่วยให้คุณสร้างคำสั่ง if-else ออนไลน์ได้ ทำให้โค้ดของคุณอ่านง่ายขึ้น

ในบทความนี้ เราจะมาดูตัวดำเนินการต่างๆ ฉันได้แบ่งพวกมันออกเป็นกลุ่มๆ เพื่อให้คุณสามารถอ้างอิงพวกมันได้อย่างง่ายดาย

ผู้ดำเนินการที่ได้รับมอบหมาย

ตัวดำเนินการที่ใช้กันมากที่สุดคือตัวดำเนินการที่ได้รับมอบหมาย โอเปอเรเตอร์เหล่านี้ใช้เพื่อกำหนด จัดการ หรือผนวกค่าเข้ากับตัวแปร

OperatorExampleDescription
=$a = 5Assigns the value on the right to the left.
+=$a += 3Adds the right operand to the left and assigns it.
-=$a -= 2Subtracts the right operand from the left and assigns it.
*=$a *= 4Multiplies the left operand by the right and assigns it.
/=$a /= 2Divides the left operand by the right and assigns it.

ตัวอย่างเช่น เราสามารถกำหนดค่า 10 ให้กับตัวแปร $count และเพิ่ม 5 ลงไป ทำให้ผลรวมเป็น 15:

$count = 10
$count += 5
Write-Output $count  # Outputs 15

ตัวดำเนินการเปรียบเทียบ

ตัวดำเนินการเปรียบเทียบใน PowerShell สามารถแบ่งออกเป็น 4 กลุ่ม เรามีตัวดำเนินการเพื่อเปรียบเทียบค่า เช่น เพื่อทดสอบว่า $a มากกว่า $b ด้วย ($a -gt $b) ซึ่งเป็นตัวดำเนินการเพื่อค้นหาและแทนที่ด้วยซ้ำ รูปแบบ เพื่อทดสอบว่าค่าปรากฏในชุดและเพื่อตรวจสอบว่าวัตถุเป็นประเภทเฉพาะหรือไม่

ตัวดำเนินการเปรียบเทียบพื้นฐานใน PowerShell คือ:

OperatorExampleDescription
-eq5 -eq $aReturns True if both operands are equal.
-ne5 -ne $aReturns True if operands are not equal.
-gt$a -gt 5Returns True if the left operand is greater.
-lt$a -lt 5Returns True if the left operand is less.
-ge5 -ge $aReturns True if the left operand is greater or equal.
-le5 -le $aReturns True if the left operand is less or equal.

ดังที่คุณเห็นในตารางด้านบน ตัวแปรจะอยู่ทางด้านขวาเมื่อใช้ตัวดำเนินการความเท่าเทียมกัน เหตุผลก็คือเมื่อคุณใช้ตัวดำเนินการ -eq ระหว่างอาร์เรย์และค่าสเกลาร์ (เช่น ตัวเลข) ดังนั้น PowerShell จะไม่ส่งคืน True หรือ เท็จ.

แต่จะเปรียบเทียบค่าสเกลาร์กับแต่ละองค์ประกอบในอาร์เรย์แทน หากค่าสเกลาร์มีอยู่ในอาร์เรย์ ก็จะส่งคืนองค์ประกอบอาร์เรย์แทนค่า True

$a = 0,1,2,3
$a -eq 0 # Returns 0
0 -eq $a # Returns False

นอกจากนี้เรายังมีตัวดำเนินการเปรียบเทียบที่เราสามารถใช้ค้นหาหรือแทนที่รูปแบบในสตริงได้อีกด้วย ตัวดำเนินการ -match และ -replace ใช้ regex ในการเปรียบเทียบ ในขณะที่ตัวดำเนินการ -like ใช้ wildcard

OperatorExampleDescription
-match$string -match “foo”Returns True if the string matches the regex pattern.
-notmatch$string -notmatch “foo”Returns True if the string does not match the regex pattern.
-replace$string -replace “foo”, “bar”Replaces text that matches a regex pattern.
-like$string -like “foo*”Returns True if the string matches the wildcard pattern.
-notlike$string -notlike “foo*”Returns True if the string does not match the wildcard pattern.

ตัวดำเนินการเปรียบเทียบการบรรจุใช้เพื่อตรวจสอบว่ามีค่าอยู่หรือไม่ในชุดอ้างอิง เช่น อาร์เรย์ เป็นต้น ตัวดำเนินการทั้งสอง -contains และ -in สามารถใช้เพื่อตรวจสอบว่ามีรายการอยู่ในคอลเลกชันหรือไม่

ข้อแตกต่างเพียงอย่างเดียวระหว่างทั้งสองคือตำแหน่งของมูลค่าและการสะสม อันไหนที่คุณควรใช้นั้นขึ้นอยู่กับบริบท

OperatorExampleDescription
-contains$array -contains 5Returns True if the array contains the specified value.
-notcontains$array -notcontains 5Returns True if the array does not contain the value.
-in5 -in $arrayReturns True if the left operand is in the array.
-notin5 -notin $arrayReturns True if the left operand is not in the array.
$numbers = 2, 4, 6, 8, 10

if ($numbers -contains 4) {
    Write-Output "The array contains 4."
}

ตัวดำเนินการเปรียบเทียบล่าสุดที่เราสามารถใช้ใน PowerShell คือตัวดำเนินการ -is อันนี้ไม่ได้ใช้มากนัก แต่ให้คุณตรวจสอบว่าวัตถุนั้นเป็นประเภทที่ระบุหรือไม่

-is

$object - คือ [ประเภท]

-isnot

$object -isnot [ประเภท]

ตัวดำเนินการเชิงตรรกะ

ตัวดำเนินการเชิงตรรกะใช้เพื่อเชื่อมต่อคำสั่งแบบมีเงื่อนไขใน PowerShell กับคำสั่งเดียว

OperatorExampleDescription
-and(5 -gt 3) -and (6 -gt 2)Returns True if both conditions are true.
-or(5 -gt 7) -or (8 -gt 2)Returns True if any of the conditions are true.
-not-not (5 -eq 5)Reverses the logic of its operand.
!!(3 -eq 4)Short form of -not.

ตัวอย่างเช่น เราสามารถใช้โอเปอเรเตอร์ -and เพื่อตรวจสอบว่าค่าน้อยกว่า 10 และมากกว่า 2 ด้วยคำสั่งด้านล่าง:

$val = 5
if ($val -lt 10 -and $val -gt 2)  {
    Write-Output "Both conditions are met"
}

ตัวดำเนินการเปลี่ยนเส้นทาง

ตัวดำเนินการเปลี่ยนเส้นทางใน PowerShell แบ่งปันความคล้ายคลึงกันบางอย่างกับตัวดำเนินการจาก Command Prompt แต่ PowerShell ก็มีบางอย่างเป็นของตัวเองเช่นกัน

คุณสามารถใช้ตัวดำเนินการเปลี่ยนเส้นทางเพื่อส่งเอาต์พุตของคำสั่งไปยังไฟล์ข้อความได้ ตัวเลือกต่างๆ ไม่เพียงแต่ช่วยให้คุณสามารถส่งหรือผนวกเอาต์พุตปกติ (มาตรฐาน) เข้ากับไฟล์ได้เท่านั้น นอกจากนี้ยังสามารถเปลี่ยนเส้นทางเฉพาะสตรีมข้อผิดพลาดหรือสตรีมคำเตือนได้อีกด้วย ซึ่งดีมากเมื่อคุณต้องการสร้างบันทึกข้อผิดพลาดสำหรับสคริปต์ของคุณ

OperatorExampleDescription
>Get-Process > processes.txtRedirects standard output to a file.
>>Get-Process >> log.txtAppends standard output to a file.
2>Get-Command xyz 2> errors.txtRedirects error output to a file.
2>>Get-Command xyz 2>> errors.logAppends error output to a file.
2>&1Get-Process 2>&1 alloutput.txtRedirects error stream to the succes stream
*>Get-Process *> alloutput.txtRedirects all streams to a file

ตัวอย่างเช่น หากต้องการเขียนเฉพาะข้อผิดพลาดของคำสั่งลงในไฟล์ เราสามารถใช้ตัวดำเนินการสตรีมการเปลี่ยนเส้นทาง:

# Write only the error to the file:
Get-ProcessCpu -name "explorer2" 2> C:\temp\process-error-log.txt

แยกและเข้าร่วมผู้ประกอบการ

ตัวดำเนินการแยกและรวมใช้ในการแยกหรือรวมสตริง

-split

'หนึ่ง สอง สาม' -แยก ','

-join

('หนึ่ง','สอง') - เข้าร่วม ','

ตัวดำเนินการประเภท

เราได้เห็นตัวดำเนินการประเภท -is เช่นกันในส่วนตัวดำเนินการเปรียบเทียบ โอเปอเรเตอร์ใช้ในการตรวจสอบว่าตัวแปรมีประเภทเฉพาะหรือไม่ แต่นอกเหนือจากตัวดำเนินการ -is แล้ว เรายังสามารถใช้ตัวดำเนินการ -as เพื่อแปลงตัวแปรให้เป็นประเภทเฉพาะได้อีกด้วย

OperatorExampleDescription
-is$var -is [int]Checks if a variable is of a specified type.
-as$var -as [string]Tries to convert a variable to a specified type.
[Type][int]$varCasts a variable to a specified type.

ตัวดำเนินการ -as มีประโยชน์อย่างยิ่งเมื่อคุณต้องการแปลงสตริงให้เป็นจำนวนเต็ม เป็นต้น

$number = "123"
if ($number -is [string]) {
    $converted = $number -as [int]
    Write-Output "Converted: $converted"
}

ตัวดำเนินการ Unary

ตัวดำเนินการ Unary ถูกใช้ค่อนข้างบ่อย โดยเฉพาะในลูป เพื่อติดตามดัชนีหรือสำหรับการนับ น่าสนุกที่ตัวดำเนินการ ++ น่าจะถูกใช้มากที่สุด แต่ส่วนใหญ่ไม่รู้ว่ามันเรียกว่าตัวดำเนินการ Unary

OperatorExampleDescription
–-$varNegates the value of the operand.
++++$varIncrements the operand’s value by 1.
—–$varDecrements the operand’s value by 1.

ผู้ดำเนินการพิเศษ

โอเปอเรเตอร์เหล่านี้ไม่เหมาะกับกลุ่มอื่นๆ เนื่องจากแต่ละกลุ่มมีกรณีการใช้งานพิเศษของตัวเอง

OperatorExampleDescription
&& script.ps1Executes a script or command.
.. script.ps1Executes a script in the current scope.
()($a + $b) * $cEnsure that expressions are evaluated in a specific order
$( )$(Get-Date)Subexpression operator; runs the command in a subexpression.
`“This is a backtick `n”Escape character; used to include special characters in strings.
..$a = 1..10Create an array with a range of integer from 1 to 10.

ตัวดำเนินการจัดกลุ่มหรือที่รู้จักกันดีในชื่อวงเล็บ ถูกนำมาใช้เพื่อให้แน่ใจว่านิพจน์หรือ cmdlet ของการดำเนินการในลำดับเฉพาะ ในตัวอย่างด้านล่าง เราต้องการรับเส้นทางของรายการก่อน จากนั้นตรวจสอบว่าความยาวมากกว่า 100 หรือไม่

$path = "C:\MyScripts\script.ps1"
if ((Get-Item $path).Length -gt 100) {
 Write-Host "Long path"
}

ตารางตัวดำเนินการแบบไตรภาค

ตัวดำเนินการแบบไตรภาคได้รับการเผยแพร่ใน PowerShell 7 ซึ่งช่วยลดความซับซ้อนของคำสั่ง if-else ทำให้อ่านง่ายขึ้น ฉันขอแนะนำให้ใช้ตัวดำเนินการแบบไตรภาคสำหรับคำสั่ง if-else แบบสั้นที่พอดีกับบรรทัดเดียวเท่านั้น

OperatorExampleDescription
? :$result = ($x -gt 10) ? ‘High’ : ‘Low’Returns High if $x is greater than 10,
otherwise returns Low.

ในตัวอย่างด้านล่าง เราจะตรวจสอบว่าตัวแปร $x มากกว่า 10 หรือไม่ หากเป็นเช่นนั้น เราจะตั้งค่าสถานะเป็นสูง หรือต่ำ:

# Define a variable
$x = 15

# Use the ternary operator to assign a value based on a condition
$status = ($x -gt 10) ? 'High' : 'Low'

# Output the result
Write-Output "The status is $status"

ตัวดำเนินการรวมศูนย์

ตัวดำเนินการรวมค่าว่าง ?? ใน PowerShell 7 ช่วยให้คุณสามารถตรวจสอบได้อย่างรวดเร็วว่าตัวแปรที่กำหนดเป็นค่าว่างหรือไม่ และตั้งค่าเริ่มต้นแทน

หากค่าทางด้านซ้ายของตัวดำเนินการเท่ากับ null ค่าทางด้านขวา (ซึ่งอาจเป็นตัวแปรก็ได้) จะถูกส่งกลับ คุณยังสามารถรวมตัวดำเนินการการรวมค่า Null หลายตัวเข้าด้วยกันเพื่อค้นหาค่าแรกที่ไม่ใช่ค่า Null ได้

OperatorExampleDescription
??$result = $value ?? DefaultValueReturns $value if it is not null; otherwise, returns DefaultValue.
??=$value ??= DefaultValueAssigns DefaultValue to $value if $value is null.
?.$result = $object?.PropertyAccesses Property of $object only if $object is not null.
?[]$element = $array?[index]Accesses the element at index in $array only if $array is not null.

สิ่งที่ดีอย่างหนึ่งเกี่ยวกับตัวดำเนินการ Null-coalescing คือมันช่วยให้คุณเข้าถึงได้เฉพาะคุณสมบัติของอ็อบเจ็กต์หากอ็อบเจ็กต์นั้นไม่เป็นโมฆะ:

# Define an object with a property
$person = [PSCustomObject]@{ Name = "Alice" }

# Access the 'Name' property only if $person is not null
$name = $person?.Name

ห่อขึ้น

มีโอเปอเรเตอร์จำนวนมากใน PowerShell ที่เราสามารถใช้ได้ ส่วนใหญ่ค่อนข้างจะธรรมดาและยังใช้ในภาษาการเขียนโปรแกรมอื่นๆ ด้วย แต่ถ้าคุณค่อนข้างใหม่กับการเขียนสคริปต์หรือการเขียนโปรแกรม ตรวจสอบให้แน่ใจว่าคุณให้ความสนใจเป็นพิเศษกับตัวดำเนินการแบบรวมศูนย์และแบบไตรภาคสำหรับ PowerShell 7

หวังว่ารายการนี้มีประโยชน์ ตรวจสอบให้แน่ใจว่าคุณสมัครรับจดหมายข่าวหรือติดตามฉันบน Facebook เพื่อที่คุณจะได้ไม่พลาดบทความล่าสุด

บทความที่เกี่ยวข้อง