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

การเปรียบเทียบตัวแปรใน Windows PowerShell และ PowerShell 7


ตัวแปรอัตโนมัติของ PowerShell จะถูกจัดเก็บและบำรุงรักษาโดย PowerShell สิ่งนี้ใช้กับ Windows PowerShell ที่มาพร้อมกับระบบปฏิบัติการ Windows ทุกระบบ รวมถึง PowerShell ที่ดาวน์โหลดผ่าน GitHub

ในโพสต์บล็อกนี้ ฉันจะแสดงวิธีเปรียบเทียบตัวแปรอัตโนมัติใน Windows PowerShell และ PowerShell 7

ตัวอย่างเช่น ตัวแปร $IsWindows หายไปใน Windows PowerShell นอกจากนี้ การตรวจสอบว่าติดตั้ง Windows ไว้หรือไม่ก็ไม่มีเหตุผลเช่นกัน เนื่องจาก Windows PowerShell ทำงานบน Windows OS เท่านั้น


$IsWindows

แต่มีความแตกต่างเพิ่มเติมที่เราจะค้นพบในตอนนี้

การบันทึก Varialbes ในไฟล์

ขั้นแรกเราจะบันทึกรายการตัวแปร Windows PowerShell ไว้ในไฟล์


Get-Variable | Select-Object -ExpandProperty Name | Out-File C:\Temp\windowspowershellvariables.txt

จากนั้นเราจะบันทึกตัวแปร PowerShell ลงในไฟล์


Get-Variable | Select-Object -ExpandProperty Name | Out-File C:\Temp\powershellvariables.txt

การเปรียบเทียบตัวแปรของ Windows PowerShell และ PowerShell

นี่คือรายการที่แสดงให้เราเห็นความแตกต่าง:


Compare-Object -ReferenceObject (Get-Content C:\Temp\windowspowershellvariables.txt) `
-DifferenceObject (Get-Content C:\Temp\powershellvariables.txt)

หากต้องการดูว่า Windows PowerShell ขาดอะไรไปเมื่อเปรียบเทียบกับ PowerShell ให้รัน


(Compare-Object -ReferenceObject (Get-Content C:\Temp\windowspowershellvariables.txt) `
-DifferenceObject (Get-Content C:\Temp\powershellvariables.txt) |
Where-Object {$_.SideIndicator -eq "=>"} |
Select-Object -ExpandProperty InputObject) -join ", "

หากต้องการดูว่า PowerShell ขาดหายไปเมื่อเปรียบเทียบกับ Windows PowerShell ให้รัน


(Compare-Object -ReferenceObject (Get-Content C:\Temp\windowspowershellvariables.txt) `
-DifferenceObject (Get-Content C:\Temp\powershellvariables.txt) |
Where-Object {$_.SideIndicator -eq "<="} |
Select-Object -ExpandProperty InputObject) -join ", "

เอาล่ะ ภารกิจสำเร็จแล้ว หวังว่าจะเป็นข้อมูล โดยเฉพาะอย่างยิ่งสำหรับผู้อ่านที่มักจะทำงานกับตัวแปรอัตโนมัติ

เจอกันครั้งหน้ากับ PowerShell!

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