Created
May 11, 2016 13:39
-
-
Save alastairtree/d1da3c888cf25899cc30d3be72c60a0e to your computer and use it in GitHub Desktop.
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
$DemoUser = 'Domain\username' | |
$DemoPass = 'complexpassword' | |
$Command = "ECHO 'Hello world'; WhoAmi;" | |
$StdOutLogFile = "$env:windir\temp\PowerShellElevatedStdOut.log" | |
$SecurePassword = ConvertTo-SecureString $DemoPass -AsPlainText -Force | |
$Credential = New-Object System.Management.Automation.PSCredential $DemoUser, $securePassword | |
"Running '$Command' as current user" | |
Start-Process Powershell.exe -NoNewWindow -ArgumentList "-Command $Command" -RedirectStandardOutput $StdOutLogFile -Wait | |
Get-Content -Path $StdOutLogFile #Retrieve the output from the external process | |
"Running '$Command' as $DemoUser" | |
Start-Process Powershell.exe -NoNewWindow -ArgumentList "-Command $Command" -RedirectStandardOutput $StdOutLogFile -Wait -Credential $Credential -WorkingDirectory 'C:\Windows\System32' | |
Get-Content -Path $StdOutLogFile #Retrieve the output from the external process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment