Created
March 15, 2021 17:54
-
-
Save fmartins-andre/bd03e128cdec84826cefaa4f860190db to your computer and use it in GitHub Desktop.
A Powershell script to run executables in elevated mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory = $true, Position = 0)][string]$exeFileName, | |
[Parameter(Mandatory = $true, Position = 1)][string]$credentialName | |
) | |
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop | |
function CheckIfFileExists { | |
if (-Not $(Test-Path -Path "$($PSScriptRoot)\$($exeFileName)" -PathType Leaf)) { | |
$wshell.Popup( | |
'O arquivo informado não pôde ser localizado!', | |
5, | |
'Usuário não é Administrador' | |
) | |
Exit | |
} | |
} | |
function CheckIfUserIsAdmin { | |
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
$user = New-Object System.Security.Principal.WindowsPrincipal($id) | |
if ( -Not $($user.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))) { | |
$wshell.Popup( | |
'O usuáro atual não tem permissão para instalar o módulo ' + | |
'necessário! Execute novamente como administrador!', | |
5, | |
'Usuário não é Administrador' | |
) | |
Exit | |
} | |
} | |
function CheckIfCredentialExists { | |
$_credential = $(Get-StoredCredential -Target $credentialName) | |
if ( -Not $($_credential)) { | |
$wshell.Popup( | |
'Credencial de acesso não encontrada! ' + | |
'Configure uma nova credencial para o identificador "' + | |
$credentialName + '".', | |
5, | |
'Instalação Requerida' | |
) | |
Exit | |
} | |
return $_credential | |
} | |
function CheckIfModuleIsInstalled([bool] $_secondRun = $false) { | |
if ( -Not $(Get-Module -ListAvailable -Name 'CredentialManager')) { | |
if ($_secondRun) { | |
$wshell.Popup( | |
'Parece ter havido algum problema com a instalação. ' + | |
'Verifique manualmente ou solicite apoio técnico!', | |
5, | |
'Erro na instalação' | |
) | |
Exit | |
} | |
CheckIfUserIsAdmin | |
$wshell.Popup( | |
'O módulo gerenciador de credenciais não está instalado. ' + | |
'Clique "OK" para tentar instalar agora.', | |
5, | |
'Instalação Requerida' | |
) | |
Install-Module -Name "CredentialManager" -SkipPublisherCheck -Force | |
CheckIfModuleIsInstalled($true) | |
} | |
} | |
function RunElevated([System.Management.Automation.PSCredential] $_credential) { | |
Start-Process powershell ` | |
-Credential $_credential ` | |
-ArgumentList "Start-Process -FilePath '$($PSScriptRoot)\$($exeFileName)' -Verb runas" ` | |
-WorkingDirectory $PSScriptRoot ` | |
-WindowStyle hidden | |
} | |
function main { | |
CheckIfModuleIsInstalled | |
CheckIfFileExists | |
$_credentials = CheckIfCredentialExists | |
RunElevated($_credentials) | |
} | |
main | |
# Paste this script in the same folder of the app you wanna elevate. | |
# Then create a shortcut with the following command: | |
# powershell.exe -WindowStyle hidden -ExecutionPolicy Bypass -File "full/path/to/this/script" "fileNameToElevate" "credentialIdentifier" | |
# Example: | |
# powershell.exe -WindowStyle hidden -ExecutionPolicy Bypass -File "C:\Program Files (x86)\Intelbras\MesaVirtual20\run_elevated.ps1" "MesaVirtual30.exe" "ps_suporte_auth" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this script to make possible common users to run apps as with high privileges.
It makes use of "CredentialManager" module to make possible to get windows stored credentials to run it. The Sysadmin just needs to save the credential on Windows Credential Manager and pass the credential address to the script aside the executable file name and extension.
Paste this script into your app folder and create a custom shortcut to run it elevated.
Tested on Windows 10 Pro.