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.

With that you can use an If statement to check for the alternate credential and use that instead.

Ex.

$admincheck = Test-IsAdmin

If ( -is [System.Management.Automation.PSCredential])
{

Get-WMIObject -computername Server1 Win32_OperatingSystem -Credential $admincheck

}

Else

{

Get-WMIObject -computername Server1 Win32_OperatingSystem

}

 

 

PowerShell
Edit|Remove
. .\Test-IsAdmin.ps1