Assuming I have a powershell function to run a command and log the output to file that other scripts are also logging to, is there a generic way to detect that a PowerShell commandlet has not executed to completion because it requires Administrator privileges?
For example, running Initialize-Tpm
as a regular user outputs “Administrator privilege is required to execute this command”, but sets the exit code to 0 and doesn’t add any errors to $Error
. So other than explicitly checking the commandlet output for that string, how can I tell it didn’t work?
I can’t find a way to get such commandlets to indicate an error when they stop, and using Get-Command
on initialize-Tpm
doesn’t indicate RequiresElevation either.
Do I just need to maintain my own list of commandlets that I know need admin?
(I don’t want to explicitly check for Administrator privileges at the start of the encompassing powershell script for other reasons.)