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

ตัวแปรส่วนกลางของ PowerShell | จะกำหนดตัวแปรโกลบอลได้อย่างไร?


ข้อมูลเบื้องต้นเกี่ยวกับตัวแปรส่วนกลางของ PowerShell

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

ไวยากรณ์ของตัวแปร PowerShell Global

วิธีสร้างตัวแปรโกลบอลมีดังนี้

$global:test="This is a test"

หรือ

$global:test = $null

ตัวอย่าง:

Write-Host "Example of global variable in PS" -ForegroundColor Green
$global:test=""
Function test ($tes)
{
$global:test+=$tes
}
test -tes "this "
$global:test+="is "
$global:test+="test "
Write-Host $global:test -ForegroundColor Green

เอาต์พุต:

ตัวอย่าง:

การตั้งค่าตัวแปรร่วมผ่านฟังก์ชัน

Write-Host "Demo of calling global variable from function" -ForegroundColor Green
$global:fln = $null
function getfn ($fn, $ln)
{
$fn +"  "+ $ln
}
$global:fln = getfn vignesh krishnakumar
Write-Host "the function value is set to global variable" -ForegroundColor Green
write-Host "the value is "$global:fln
$global:fln1 = $null
function getfn1 ($fn1, $ln1, $chn1)
{
$fn1 +"  "+ $ln1 +"  "+ $chn1
}
Write-Host "Demo of global variable with function as three parameters" -ForegroundColor Green
$global:fln1 = getfn1 vignesh krishnakumar Chennai
Write-Host " the value is " $global:fln1

เอาต์พุต:

ตัวอย่าง:

อินพุต:

Write-Host "Demo of passing global variable as a parameter to a function"
Write-Host "global variable needs to be declared as reference"
$global:finalsum =""
function sum ($no1, $no2, [REF]$resr)
{
$resr.Value = $no1 + $no2
}
#You can then call it like this:

sum 51 61 ([REF]$global:finalsum) Write-Host " the value of the global variable is" $global:finalsum $global:finalsum1 ="" function sum1 ($no11, $no22, $no33,$no44,[REF]$resr1) { $resr1.Value = $no11 + $no22 +$no33 + $no44 } #You can then call it like this:

sum1 51 61 71 81 ([REF]$global:finalsum1) Write-Host " the value of the global variable is" $global:finalsum1

ข้อดีของตัวแปรโกลบอล

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

ลำดับชั้นขอบเขต

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

ตัวแก้ไขขอบเขตประเภทต่างๆ:

ทั่วโลก: สิ่งนี้แสดงถึงขอบเขตทั่วโลก

ท้องถิ่น: หมายถึงขอบเขตปัจจุบันเท่านั้น

ส่วนตัว: สิ่งนี้แสดงว่ารายการนั้นเป็นส่วนตัวและพร้อมใช้งานภายในขอบเขตปัจจุบัน

สคริปต์: หมายถึงว่ารายการสามารถเข้าถึงได้ทั่วทั้งสคริปต์

การใช้: ใช้เพื่อเข้าถึงรายการจากสคริปต์อื่น

ขั้นตอนการทำงาน: หมายถึงรายการที่ใช้ในขั้นตอนการทำงาน

การใช้ตัวแก้ไข:

ใช้เพื่อทำงานกับรายการใน cmdlet ระยะไกล เมื่อใดก็ตามที่จำเป็นต้องเข้าถึงสิ่งใดนอกเซสชันปัจจุบัน ควรใช้ตัวแก้ไขการใช้ นี่เป็นระดับที่สูงกว่าตัวแปรร่วมหนึ่งขั้น ตัวอย่าง cmdlet บางส่วน ได้แก่ Start-Job และ Inrigg-Command

ตัวอย่าง:

อินพุต:

Write-Host "Demo of setting global variable value" -ForegroundColor Green
Set-Variable -Name "TD" -Value (Get-Date) -Scope global
Get-Variable -Name "TD"
Set-Variable -Name "NAME1" -Value "vignesh" -Scope global
Set-Variable -Name "age1" -Value "28" -Scope global
Set-Variable -Name "city" -Value "chennai" -Option Private -Scope global
Set-Variable -Name "status" -Value "married" -Scope global
Set-Variable -Name "gender" -Value "male" -Scope global
Get-Variable -Name "NAME1"
Get-Variable -Name "age1"
Get-Variable -Name "city"
Get-Variable -Name "status"
Get-Variable -Name "gender"

เอาต์พุต:

ตัวอย่าง:

อินพุต:

Write-Host "Example of global variable in PS" -ForegroundColor Green
$global:eg=""
Function test ($par)
{
$global:eg+=$par
}
test -par "welcome"
$global:eg += "user "
$global:eg+="mark "
Write-Host $global:eg -ForegroundColor Green
$global:abc = $null
function geabcdtfn ($fn, $ln)
{
$fn +"  "+ $ln
}
$global:abc = geabcdtfn vijaya sethupathi
write-Host "the value is "$global:abc
$global:fln1 = $null
function getfn1 ($fn1, $ln1, $chn1)
{
$fn1 +"  "+ $ln1 +"  "+ $chn1
}
Write-Host "Demo of global variable with function as three parameters" -ForegroundColor Green
$global:fln1 = getfn1 vignesh krishnakumar Chennai
Write-Host " the value is " $global:fln1
Set-Variable -Name "tdate" -Value (Get-Date) -Scope global
Set-Variable -Name "mark1" -Value "100" -Scope global
Set-Variable -Name "mark2" -Value "90" -Scope global
Set-Variable -Name "mark3" -Value "100" -Option Private -Scope global
Set-Variable -Name "mark4" -Value "100" -Scope global
Set-Variable -Name "total" -Value "390" -Scope global
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "mark1"
Get-Variable -Name "total"
Get-Variable -Name "tdate"

เอาต์พุต:

บทสรุป

ดังนั้นบทความนี้จึงอธิบายรายละเอียดเกี่ยวกับตัวแปรส่วนกลางใน PowerShell โดยจะอธิบายรายละเอียดวิธีกำหนดตัวแปรส่วนกลาง ตั้งค่าให้กับตัวแปรส่วนกลาง ส่งต่อตัวแปรส่วนกลางไปยังฟังก์ชัน และตั้งค่าที่ฟังก์ชันส่งคืนไปยังตัวแปรส่วนกลาง แม้ว่าตัวแปรร่วมจะพร้อมใช้งานสำหรับทั้งเซสชัน แต่ไม่แนะนำให้สร้างตัวแปรทั้งหมดเป็นตัวแปรร่วม ซึ่งนำไปสู่แนวทางปฏิบัติที่ไม่ดี หากต้องการเรียนรู้รายละเอียดเพิ่มเติม ขอแนะนำให้เขียนสคริปต์ตัวอย่างและฝึกฝน

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