# encoding: ascii
# api: powershell
# title: Test-IsAdmin
# description: This Advanced Function will look to see if the current user context running a command/script is an Administrator or not. If not, a menu is presented to the user to either continue or enter alternate credentials. The function will either return a credential object of the alternate credential or a string type stating that the current user context will be used.
# version: 0.1
# type: function
# author: Boe Prox
# license: CC0
# function: Test-IsAdmin
# x-poshcode-id: 2667
# x-archived: 2011-05-12T06:50:45
# x-published: 2011-05-09T19:32:00
#
#
Function Test-IsAdmin
{
<#
.SYNOPSIS
Function used to detect if current user is an Administrator.
.DESCRIPTION
Function used to detect if current user is an Administrator. Presents a menu if not an Administrator
.NOTES
Name: Test-IsAdmin
Author: Boe Prox
DateCreated: 30April2011
.EXAMPLE
Test-IsAdmin
Description
-----------
Command will check the current user to see if an Administrator. If not, a menu is presented to the user to either
continue as the current user context or enter alternate credentials to use. If alternate credentials are used, then
the [System.Management.Automation.PSCredential] object is returned by the function.
#>
[cmdletbinding()]
Param()
Write-Verbose "Checking to see if current user context is Administrator"
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You are not currently running this under an Administrator account! `nThere is potential that this command could fail if not running under an Administrator account."
Write-Verbose "Presenting option for user to pick whether to continue as current user or use alternate credentials"
#Determine Values for Choice
$choice = [System.Management.Automation.Host.ChoiceDescription[]] @("Use &Alternate Credentials","&Continue with current Credentials")
#Determine Default Selection
[int]$default = 0
#Present choice option to user
$userchoice = $host.ui.PromptforChoice("Warning","Please select to use Alternate Credentials or current credentials to run command",$choice,$default)
Write-Debug "Selection: $userchoice"
#Determine action to take
Switch ($Userchoice)
{
0
{
#Prompt for alternate credentials
Write-Verbose "Prompting for Alternate Credentials"
$Credential = Get-Credential
Write-Output $Credential
}
1
{
#Continue using current credentials
Write-Verbose "Using current credentials"
Write-Output "CurrentUser"
}
}
}
Else
{
Write-Verbose "Passed Administrator check"
}
}