function Confirm-RunningAsAdministrator { <# .SYNOPSIS This function will return a bool, "True" if running as administrator, "False" if not. .DESCRIPTION You can use this script to check is you are currently running as administrator. It is best used when your script needs to verify that this is the case. .EXAMPLE if (Confirm-RunningAsAdministrator) { Write-Host "Running as an administrator." } else { Write-Host "NOT running as an administrator." } ----------------------------------------------------------- If running as an administrator, this would produce: Running as an administrator. .INPUTS None. .OUTPUTS [bool] - "True" if running as administrator, "False" if not. .NOTES Original Function name: Confirm-RunningAsAdministrator.ps1 Original Author: Norman Skinner Original Created on: 12/05/2012 HISTORY: ===========#================#=========#============================ Date | User | Version | Description -----------+----------------+---------+---------------------------- 12/05/2011 | Norman Skinner | 1.0.0.0 | Created script -----------+----------------+---------+---------------------------- | | | -----------+----------------+---------+---------------------------- #> $WinIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() $currentPrincipal = New-Object Security.Principal.WindowsPrincipal($WinIdentity) $AdministratorRole = [Security.Principal.WindowsBuiltInRole]::Administrator return $currentPrincipal.IsInRole($AdministratorRole) }
Wednesday, February 22, 2012
Check if Running As Administrator
Have you ever had a script that requires it be run in “Run As Administrator” mode and needed a function to check that? When I run into this I use the following function:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment