Created
July 17, 2019 13:45
-
-
Save justinloring/25e9c60894707c033729eddaabfefab5 to your computer and use it in GitHub Desktop.
Create LogOnAsBatch Scheduled Task w/Hidden Password
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
$encrypted = Get-Content '\\srilcredev01\c$\RSICode\StoredCreds\LogOnAsBatch.txt' | ConvertTo-SecureString -Key (Get-Content '\\srilcredev01\c$\RSICode\StoredCreds\aes.key') | |
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($encrypted) | |
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | |
$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-ExecutionPolicy Bypass -File "C:\RSItemp\RunMe.ps1"' | |
$Trigger = New-ScheduledTaskTrigger -At "07/17/2019 08:47" -Once | |
$Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable | |
$Principal = New-ScheduledTaskPrincipal -UserID "repsrv\svc-sccmtask" -LogonType ServiceAccount -RunLevel Highest | |
$Password = "$UnsecurePassword" | |
$UserName = "repsrv\svc-sccmtask" | |
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Principal | |
$Task | Register-ScheduledTask -TaskName 'LogOnAsBatchTest' -User $UserName -Password $Password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment