-
-
Save MacsInSpace/cbe96cdc3123a05f520db2d5f6933acf to your computer and use it in GitHub Desktop.
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
# from https://blog.ordix.de/execute-powershell-scripts-as-system-eng | |
# request and store encrypted credentials for use when running scheduled tasks as SYSTEM under C:\Windows\system32\config\systemprofile\AppData\Local | |
Set-Content -Path "$env:temp\ExportCredential.ps1" -Value '[CmdletBinding()] | |
Param( | |
[Parameter(Position = 0, Mandatory = $true)][string]$UserName, | |
[Parameter(Position = 1, Mandatory = $true)][string]$Password, | |
[Parameter(Position = 2, Mandatory = $false)][string]$Path = "$env:LOCALAPPDATA\Credential.xml" | |
) | |
$secureString = ConvertTo-SecureString -String $Password -AsPlainText -Force | |
$credential = [PSCredential]::new($UserName, $secureString) | |
$credential | Export-Clixml -Path $Path | |
' | |
$scriptFile = "$env:temp\ExportCredential.ps1" | |
$credential = Get-Credential -Message 'Enter credential to be exported as SYSTEM' | |
$password = $credential.GetNetworkCredential().Password | |
$actionArgument = "-File $scriptFile -UserName $($credential.UserName) -Password $password" | |
$action = New-ScheduledTaskAction -Execute powershell -Argument $actionArgument | |
$principal = New-ScheduledTaskPrincipal -UserId SYSTEM | |
$task = New-ScheduledTask -Action $action -Principal $principal | |
$regTask = $task | Register-ScheduledTask ExportCredential | |
$regTask | Start-ScheduledTask | |
$regTask | Unregister-ScheduledTask -Confirm:$false | |
Remove-Item -Path "$env:temp\ExportCredential.ps1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment