Check if script runs as admin
Check session $CurrentUserfor =admin [Security.Principal.WindowsIdentity]::GetCurrent()privileges
If you have to run your commands in your custom script as admin, it is essential for error handling to check if the current session is admin.
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $CurrentUser).IsInRole([Security.Principal.SecurityIdentifier] "S-1-5-32-544")
if($isAdmin){
write-outputWrite-Output "PowerShell session is admin"
}
if(!$isAdmin){
Write-Output $($CurrentUser)"PowerShell ($($isAdmin))session is not admin"
}
Authenticate Use case
This script can be used to combine with a Remote PowerShell Session to get local Configurations from other Windows Servers or Clients, which only can be accessed via local admin privileges or domain admin privileges.
More about this use case in docs entry "