Skip to content

Instantly share code, notes, and snippets.

@PCfromDCSnippets
Created April 4, 2017 01:42
Show Gist options
  • Save PCfromDCSnippets/2d064c0e1f06a5cc63007e30ff004f23 to your computer and use it in GitHub Desktop.
Save PCfromDCSnippets/2d064c0e1f06a5cc63007e30ff004f23 to your computer and use it in GitHub Desktop.
Create Log In Task
#region Create Log In Task
$user = whoami
$sj = Get-ScheduledJob -Name $jobName -ErrorAction SilentlyContinue
if ($sj.count -gt 0) {
Write-Host "$jobName scheduled job already exists..."
}
else {
Write-Host "Creating $jobName scheduled Job..."
$sj = Register-ScheduledJob –Name $jobName `
-ScriptBlock $sb `
-ScheduledJobOption (New-ScheduledJobOption -ContinueIfGoingOnBattery:$true -StartIfOnBattery:$true) `
-Trigger (New-JobTrigger -AtLogOn -User $user)
}
Write-Host "Updating $jobName scheduled task..."
$path = (Get-ScheduledTask $jobName).TaskPath
$principal = New-ScheduledTaskPrincipal -UserId $user
$st = Set-ScheduledTask -TaskName $jobName -Principal $principal -TaskPath $path
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment