Answered by:
Try / Catch for Permission Denied

Question
-
Hello - I'm writing a script that includes the following cmdlet:
Get-ChildItem 'C:\Users\JoeShmoe\My Documents' -Recurse
I would like to write in a try/catch to verify the tech using this script is using elevated privileges prior to running the .ps1
How would I accomplish this?
Thank you.
JH
Thursday, October 26, 2017 2:01 PM
Answers
-
Hi. You could use something like this:
$elevated = $False $WindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $WindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($WindowsIdentity) $admin = [System.Security.Principal.WindowsBuiltInRole]::Administrator $elevated = $WindowsPrincipal.IsInRole($admin) Write-Host "Check if running elevated... " if ($elevated -ne $True) { $errorcode = 1 Write-Host "Not running as Administrator" Read-Host "Press enter to exit" exit 1 }
It is not try/Catch but it will do the trick
Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.
- Marked as answer by jasonh17 Thursday, October 26, 2017 2:59 PM
Thursday, October 26, 2017 2:04 PM -
Hi. You could use something like this:
$elevated = $False $WindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $WindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($WindowsIdentity) $admin = [System.Security.Principal.WindowsBuiltInRole]::Administrator $elevated = $WindowsPrincipal.IsInRole($admin) Write-Host "Check if running elevated... " if ($elevated -ne $True) { $errorcode = 1 Write-Host "Not running as Administrator" Read-Host "Press enter to exit" exit 1 }
It is not try/Catch but it will do the trick
Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.
I found my mistake... variable name misspelled $Elevated and $Elevation
How does $elevated know to switch to True if the $Elevation = True?
$admin is stating "Administrator"
$elevation is stating "True"
However, $elevated = False
and I'm being returned "Not running as Administrator"
I'm running the script from an elevated shell.
THANK YOU!! @Vlad817263
Thursday, October 26, 2017 2:54 PM
All replies
-
Hi. You could use something like this:
$elevated = $False $WindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $WindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($WindowsIdentity) $admin = [System.Security.Principal.WindowsBuiltInRole]::Administrator $elevated = $WindowsPrincipal.IsInRole($admin) Write-Host "Check if running elevated... " if ($elevated -ne $True) { $errorcode = 1 Write-Host "Not running as Administrator" Read-Host "Press enter to exit" exit 1 }
It is not try/Catch but it will do the trick
Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.
- Marked as answer by jasonh17 Thursday, October 26, 2017 2:59 PM
Thursday, October 26, 2017 2:04 PM -
Use a Requires statement at the first line of the script. Check the help
#Requires -RunAsAdministrator Get-Help about_requires
Thursday, October 26, 2017 2:13 PM -
I tried this, but it isn't working.
1. #Requires -RunAsAdministrator
It is the first line in my .ps1 - the script still runs.
Thursday, October 26, 2017 2:27 PM -
Hi. You could use something like this:
$elevated = $False $WindowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $WindowsPrincipal = new-object System.Security.Principal.WindowsPrincipal($WindowsIdentity) $admin = [System.Security.Principal.WindowsBuiltInRole]::Administrator $elevated = $WindowsPrincipal.IsInRole($admin) Write-Host "Check if running elevated... " if ($elevated -ne $True) { $errorcode = 1 Write-Host "Not running as Administrator" Read-Host "Press enter to exit" exit 1 }
It is not try/Catch but it will do the trick
Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.
I found my mistake... variable name misspelled $Elevated and $Elevation
How does $elevated know to switch to True if the $Elevation = True?
$admin is stating "Administrator"
$elevation is stating "True"
However, $elevated = False
and I'm being returned "Not running as Administrator"
I'm running the script from an elevated shell.
THANK YOU!! @Vlad817263
Thursday, October 26, 2017 2:54 PM