จะตรวจสอบได้อย่างไรว่าเรียกใช้สคริปต์ PowerShell ในฐานะผู้ดูแลระบบ
หากคุณจำเป็นต้องเรียกใช้สคริปต์ PowerShell ด้วยสิทธิ์ของผู้ดูแลระบบ คุณสามารถตรวจสอบว่ากระบวนการ powershell.exe ปัจจุบันมีสิทธิ์ขั้นสูงในรหัส PS ของคุณหรือไม่
สามารถใช้รหัส PowerShell ต่อไปนี้เพื่อตรวจสอบว่าสคริปต์ปัจจุบันกำลังทำงานในโหมด "เรียกใช้ในฐานะผู้ดูแลระบบ" หรือไม่:
Write-Host "Checking for elevated permissions..."
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "Insufficient permissions to run this script. Open the PowerShell console as an administrator and run this script again."
Break
}
else {
Write-Host "Code is running as administrator — go on executing the script..." -ForegroundColor Green
}บันทึกรหัส PowerShell ลงในไฟล์ check_process_elevation.ps1 และเรียกใช้ในคอนโซลโดยไม่ต้องใช้สิทธิ์ของผู้ดูแลระบบ:
C:\PS\check_process_elevation.ps1อย่างที่คุณเห็น ข้อความปรากฏว่าคุณไม่มีสิทธิ์ของผู้ดูแลระบบ ดังนั้นสคริปต์ PowerShell จึงหยุดทำงาน
ตอนนี้เรียกใช้สคริปต์ในเซสชัน PowerShell ที่ยกระดับ อย่างที่คุณเห็น สคริปต์ตรวจพบว่าเซสชัน PowerShell นี้ทำงานในฐานะผู้ดูแลระบบ
นอกจากนี้คุณยังสามารถร้องขอการยกระดับได้จากสคริปต์ PowerShell โดยทำแทนสตริง:
Write-Warning "Insufficient permissions…”ใช้รหัสต่อไปนี้:
Start-Process Powershell -ArgumentList $PSCommandPath -Verb RunAsเมื่อเรียกใช้สคริปต์โดยไม่มีสิทธิ์ของผู้ดูแลระบบ สคริปต์จะเรียกใช้อีกครั้งในเซสชัน PowerShell ที่ยกระดับใหม่ และคุณจะเห็นข้อความแจ้งการยกระดับ UAC หากคุณยอมรับ สคริปต์ PS1 ของคุณจะถูกเรียกใช้ในฐานะผู้ดูแลระบบ (เส้นทางไปยังไฟล์ปัจจุบันของสคริปต์ PowerShell จะถูกถ่ายโอนโดยใช้ไฟล์
$PSCommandPathตัวแปรสภาพแวดล้อม)
ใน PowerShell 4.0 หรือใหม่กว่า การตรวจสอบว่าสคริปต์ของคุณทำงานด้วยสิทธิ์ของผู้ดูแลระบบทำได้ง่ายยิ่งขึ้นหรือไม่ ในการดำเนินการนี้ ให้ใช้คำสั่ง -RunAsAdministrator
#requires -version 4.0
#requires -RunAsAdministrator
Write-Host "PowerShell is run as administrator" -ForegroundColor Greenหากสคริปต์ไม่ได้ทำงานภายใต้ผู้ดูแลระบบ ข้อผิดพลาดต่อไปนี้จะปรากฏขึ้น:
The script ‘check_process_elevation.ps1’ cannot be run because it contains a “#requires” statement for running as Administrator. The current Windows PowerShell session is not running as Administrator. Start Windows PowerShell by using the Run as Administrator option, and then try running the script again.
At line:1 char:1
+ C:\PS\check_process_elevation.ps1
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (check_process_elevation.ps1:String) [], ScriptRequiresException
+ FullyQualifiedErrorId : ScriptRequiresElevation
หากคุณเรียกใช้สคริปต์บนคอมพิวเตอร์ที่ใช้ PowerShell v2 ข้อความแสดงข้อผิดพลาดต่อไปนี้จะปรากฏขึ้น:
Cannot process the “#requires” statement at line 2 because it is not in the correct format.
The “#requires” statement must be in one of the following formats:
“#requires -shellid <shellID>”
“#requires -version <major.minor>”
“#requires -pssnapin <psSnapInName> [-version <major.minor>]”ในการจัดการ Active Directory คุณอาจต้องทำงานอื่น: เพื่อตรวจสอบว่าผู้ใช้ปัจจุบันมีสิทธิ์ของผู้ดูแลระบบโดเมนจากสคริปต์ PowerShell หรือไม่ ใช้รหัสต่อไปนี้:
If(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole("Domain Admins"))
{
#a user running the script has the Domain Admins rights
}
Else
{
#no Domain Admins rights
}



