Modules/TaskCreator/taskcreator.psm1
<# .SYNOPSIS Registers a scheduled task definition on a local computer. .DESCRIPTION The ``New-APSTask`` cmdlet registers a scheduled task definition on a local computer. This task executes PowerShell commands at the specified time. .PARAMETER Command Specifies the commands to be executed. .PARAMETER StartTime Defines the execution time of the commands given in the ``-Command`` parameter. .PARAMETER TaskName Specifies the name of the task. By default APS Task (<cmdlet>). .PARAMETER WindowStyle Set the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden. Default Hidden. .PARAMETER Save Specifies whether the task after execution is to be kept in the task schedule. .EXAMPLE New-APSTask -Command '[console]::beep(5000,3500)' -StartTime 6:00:00 -TaskName "Alarm" #> function New-APSTask { [CmdletBinding(SupportsShouldProcess, HelpUri="https://github.com/akotu235/APS/blob/master/Docs/Modules/TaskCreator/New-APSTask.md")] Param( [Parameter(Mandatory=$true)] [System.String]$Command, [System.DateTime]$StartTime = $((Get-Date).AddSeconds(3)), [System.String]$TaskName = "APS Task($($Command.TrimStart('"& {').Split(" ")[0]))", [ValidateSet("Normal","Minimized","Maximized","Hidden")] [System.String]$WindowStyle = "Hidden", [switch]$Save ) $Command = " & { $Command }" $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument " -WindowStyle $WindowStyle -command $Command" $triger = New-ScheduledTaskTrigger -Once -At $StartTime $options = New-ScheduledTaskSettingsSet -Hidden -DontStopIfGoingOnBatteries -AllowStartIfOnBatteries if($Save) { $taskPath = "\APS\" } else { $taskPath = "\APS\Temp\" } $i = 0 $iTaskName = $TaskName while($null -ne (Get-ScheduledTask -TaskName "$iTaskName" -TaskPath $taskPath -ErrorAction Ignore )){ $i++ $iTaskName = "$TaskName($i)" } Register-ScheduledTask -TaskName $iTaskName -Action $action -Settings $options -Trigger $triger -TaskPath $taskPath >> $null #remove temporary tasks $APSTempTasks = Get-ScheduledTask -TaskPath "\APS\Temp\" -ErrorAction Ignore if($APSTempTasks){ foreach($task in $APSTempTasks) { $taskInfo = $task | Get-ScheduledTaskInfo if($taskInfo.NextRunTime -lt (Get-Date) -and $task.TaskName -notlike $iTaskName){ $task | Unregister-ScheduledTask -Confirm:$false } } } } # SIG # Begin signature block # MIIIWAYJKoZIhvcNAQcCoIIISTCCCEUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUBfmsfdoQ53SOeMun/Z6E1rCq # 10SgggT6MIIE9jCCAt6gAwIBAgIQYYPyfUBBC6pE/rAfOslXOzANBgkqhkiG9w0B # AQsFADATMREwDwYDVQQDDAhha290dSBDQTAeFw0yMjA5MjAxOTQ4MDFaFw0zMjA5 # MjAxOTU4MDFaMBMxETAPBgNVBAMMCGFrb3R1IENBMIICIjANBgkqhkiG9w0BAQEF # AAOCAg8AMIICCgKCAgEAvGcae/FCZugTbghxO7Qv9wQKvRvp9/WvJyJci/SIsPr1 # /Mf5wfBTJ3aCvyjFvHfcsDH4NdHZubHO531tc1NHCDh+Ztkr5hbOdl3x46nEXm6u # e4Fiw23SB02dU3dAnFvNSGEE5jhQDOApGX/u7xEW4ZXrvMC5yLCBa3Kva1abPx5b # owvQlHhiSsn039/K2xSNhR+x4QcgEIo9JYdcob0f7ZY3AhXT+f1PNyYe075SY+t2 # y1YMlPlq4THolVUB4yB5MknAOG7IoxFt0U9vXhMSjbb06LZ/I/2RpAJd/qcaC/aX # CBvKYQbbmEqMqKutic/Q23cQU2jcuRxyy+Y5QphALwdkQGIuvOOIQCak/ZKa6k5S # 5U3zcMSbGOFF1BHdLSmcUnicsuvMM4uOT0zF/yzuSv5fSo3t6W5VHa+1Ct8ygt3/ # Byq2dLPskUPn0khR3/PaC8Px0k6TpcL1auKeb/uObvckBH/NVvQebtFuXMFXCayw # ZFQx2dGfqb20Q5ZDNw5u8PtrSAeTaqZ7shrcsHbi59ztASvNjapdnhosQ26ir5bD # Urzn7Fm/R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