Last active
August 23, 2017 14:45
-
-
Save tahir-hassan/906f2deea0494560d2756c3c44303d81 to your computer and use it in GitHub Desktop.
PowerShell wrapper for runas.exe (Start-UserProcess)
This file contains 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
# this does not support all the features of runas.exe, but is a first stab | |
Function Start-UserProcess { | |
param([Parameter(Mandatory=$true)][string]$Username, [switch]$SaveCredentials, [Parameter(Mandatory=$true)][string]$Program, [string[]]$ArgumentList) | |
$program = & { | |
if ($ArgumentList) { | |
((& { $Program; $ArgumentList }) | foreach { "\`"$_\`"" }) -join " " | |
} else { | |
"`"$Program`""; | |
} | |
} | |
$saveCred = if ($SaveCredentials) { "/savecred" } else { "" } | |
runas.exe "/user:$Username" $saveCred $program | |
} | |
# examples: | |
# Start-UserProcess -Username "MyDomain\MyUsername" -SaveCredentials -Program "notepad" -ArgumentList "C:\Temp\myfile.txt" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment