jueves, 5 de mayo de 2016

Run script As user (Useful when running scripts with encrypted credentials) | Ejecutar script como otro usuario (útil cuando el script tiene credenciales encriptadas)


Sometimes you need to store username and password on your scripts and if you are sharing them with other members of your team you should use a common account to encrypt the values, in my case we run scripts manually or by using PDQ Deploy (Admin Arsenal) running on a service account, encrypting credentials mean that if someone has physical access to the scripts they will not have the credentials to execute it unless they get access to the credentials used to encrypt them.

$Credentials = (Get-Credential)
$ScriptToExecute = {return (
Get-Item Env:AppData)}
$AppData =
Invoke-Command -ComputerName localhost -Credential $Credentials -ScriptBlock $ScriptToExecute
$AppData # Now contains the Environment var named "AppData"

Thank you Mathias for sharing this quick and easy way to do this: