I have a script that must be run as an elevated user. I'm using the code from the question "Running a PowerShell script as administrator without typing in passwords" but my 2nd script is not being called. The first script gets triggered by a system process (my ticketing system getting an email, then it calls my elevation script with the subject line as a parameter), and simply using a scheduled task is not an option.
The calling script:
param(
[Parameter(Mandatory=$true)]
[String]$MyParam
)
$LogFile = "c:\temp\log.txt"
$encpwd = Get-Content c:\temp\password.bin
$passwd = ConvertTo-SecureString $encpwd
$cred = new-object System.Management.Automation.PSCredential 'domain\LocalAdminAccount',$passwd
Add-Content $LogFile "I am running as $env:userdomain\$env:username"
Add-Content $LogFile "Trying to call the script with the parameter: $MyParam"
try {
Add-Content $LogFile "Calling script"
Start-Process PowerShell -WorkingDirectory 'C:\Windows\System32' -Cred $cred -ArgumentList '-File', "c:\temp\TargetScript.ps1 $MyParam"
Add-Content $LogFile "Script called"
} catch {
$msg = $Error[0].Exception.Message
Add-Content $LogFile "Error caught: $msg"
}
Add-Content $LogFile "Error caught: $msg"
The called script:
param(
[Parameter(Mandatory=$true)]
[String]$PassedParam
)
$LogFile = "c:\temp\log.txt"
Add-Content $LogFile "I am running as $env:userdomain\$env:username"
if ($PassedParam) {
try {
#stuff
if ($?) {
Add-Content $LogFile "$PassedParam worked"
} else {
Add-Content $LogFile "Failed"
}
} catch {
$msg = $Error[0].Exception.Message
Add-Content $LogFile "Error caught: $msg"
}
}
Add-Content $LogFile "Error caught: $msg"
And this is what gets put in the log file:
I am running as DOMAIN\COMPUTER$
Trying to call the script with the parameter: Tim
Calling script
It never seems to actually start the 2nd powershell process, or at least if it does, the 2nd powershell process isn't writing to the log file. I specifically granted the LocalAdminAccount full rights to the log file and password.bin file, and the LocalAdminAccount is in the administrators group on the computer.
And in case it matters, my powershell version is:
PS C:\> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
Update: If I log in to the computer and run the script as myself, Here's is what the log file shows:
I am running as DOMAIN\TIM
Trying to call the script with the parameter: Tim
Calling script
Script called
I am running as DOMAIN\LocalAdminAccount
Tim worked
Error caught:
Update: I did find this article: https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-2/ which shows I need to provide a decryption key for the password since I made the password file under my account but it is being decrypted by NT SYSTEM. That didn't fix my issue though.
I did more testing by simply trying to call notepad. It fails if I try to open it as a different user, but if I just try to open it I can see it in Task Manager running under the SYSTEM user name.
My issue really seems to be that SYSTEM (DOMAIN\COMPUTER$) does not have the ability to run a process as a different user?