03 Nov 2011 @ 1:25 PM 

When I provide PowerShell scripts to a customer, I sometimes get a complaint that the script isn’t working. When I go to the customer I suddenly realize that they just execute the script, and don’t right-click on the script and select “Run as administrator” and therefor will run with elevated permissions.

Because this has happened a few times now, I started to include a few extra lines of code in my scripts:

$CurrentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent( ) )
if ( -not ($currentPrincipal.IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) ) )
{
Write-Error “This script must be executed with elevated permissions!” -ErrorAction Stop
}

This little bit of code will give the customer a message stating that the script will need to run with elevated permissions Smile

Post to Twitter

Posted By: Jeff Wouters
Last Edit: 23 Apr 2012 @ 07:00 AM

EmailPermalink
Tags
Categories: PowerShell, Script



Get Adobe Flash player
 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 164
  • Comments » 150
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

About



    No Child Pages.

Contact



    No Child Pages.