Public/New-specTwiceDailyScheduledTask.ps1
Function New-specTwiceDailyScheduledTask { <# .SYNOPSIS Creates a scheduled task that runs twice daily at 10am and 3pm .DESCRIPTION Creates a scheduled task for a device script that runs twice daily at 10am and 3pm .PARAMETER specsaversFolderPath The path to the folder where the SPECLauncher device script script is located .PARAMETER SPECLauncherScriptName The name of the SPECLauncher device script .PARAMETER TaskName The name of the scheduled task .EXAMPLE New-specTwiceDailyScheduledTask -specsaversFolderPath "C:\ProgramData\Specsavers" -SPECLauncherScriptName "SPECLauncher" -TaskName "SPECLauncher" .NOTES File : New-specTwiceDailyScheduledTask.ps1 Author : owen.heaume Version : 1.0 1.1 - use Remove-specFileExtension function to remove file extension from SPECLauncherScriptName with .ps1 #> [cmdletbinding()] param( [Parameter(Mandatory = $false)] [string]$specsaversFolderPath = "$env:ProgramData\Specsavers", [Parameter(Mandatory = $true)] [string]$SPECLauncherScriptName, [Parameter(Mandatory = $true)] [string]$TaskName ) Begin { # Ensure that the file name does not have a file extension $SPECLauncherScriptName = Remove-specFileExtension -FileName $SPECLauncherScriptName -Extension ".ps1" } process { $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoLogo -NonInteractive -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$specsaversFolderPath\$SPECLauncherScriptName.ps1`"" $trigger1 = New-ScheduledTaskTrigger -Daily -At 10:00AM $trigger2 = New-ScheduledTaskTrigger -Daily -At 3:00PM $principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -RunLevel Highest $settings = New-ScheduledTaskSettingsSet $settings.Compatibility = 'Win8' $settings.WakeToRun = $true $settings.DisallowStartIfOnBatteries = $false $settings.StopIfGoingOnBatteries = $false $settings.RunOnlyIfNetworkAvailable = $true $settings.Hidden = $false $settings.Priority = 7 try { $null = Register-ScheduledTask -Settings $settings -Action $action -Trigger $trigger1, $trigger2 -Principal $principal -TaskName $TaskName -Description "Runs device script twice daily" -TaskPath "Specsavers" -ErrorAction stop # The twice daily scheduled task was created OK return 0 } catch { write-error "Failed to create twice daily scheduled task The error was $_" return 1 } } } |