The following PowerShell code can be used to check if the current script is running in “Run as Administrator” mode or not:
function Check-PSAdminPermission (){
if (-NOT ([Security.Principal.WindowsPrincipal]`
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
[Security.Principal.WindowsBuiltInRole] "Administrator")) {
Return $false
}
else {
Return $true
}
}
Write-Host "Checking for administrative privileges …"
if (Check-PSAdminPermission) {
Write-Host "Administrator permission detected" -ForegroundColor Green
No comments:
Post a Comment