Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justinloring/25e9c60894707c033729eddaabfefab5 to your computer and use it in GitHub Desktop.
Save justinloring/25e9c60894707c033729eddaabfefab5 to your computer and use it in GitHub Desktop.
Create LogOnAsBatch Scheduled Task w/Hidden Password
$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