├── License.txt ├── New-ReleaseChangeLog.ps1 ├── PrepNewRelease.ps1 ├── README.md ├── ScheduledJobTools.psd1 ├── ScheduledJobTools.psm1 ├── changelog.md ├── docs ├── Export-ScheduledJob.md ├── Get-ScheduledJobDetail.md ├── Get-ScheduledJobResult.md ├── Import-ScheduledJob.md └── Remove-OldJobResult.md ├── en-us └── ScheduledJobTools-help.xml ├── formats ├── ScheduledjobResult.format.ps1xml └── scheduledjob.format.ps1xml ├── functions ├── Export-ScheduledJob.ps1 ├── Get-ScheduledJobDetail.ps1 ├── Get-ScheduledJobResult.ps1 ├── Import-ScheduledJob.ps1 └── Remove-OldJobResult.ps1 └── types └── ScheduledJobDefinition.types.ps1xml /License.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-2024 JDH Information Technology Solutions, Inc. 4 | 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /New-ReleaseChangeLog.ps1: -------------------------------------------------------------------------------- 1 | #requires -module ChangeLogManagement 2 | 3 | #get ISO8601 date 4 | $dt = Get-Date -format "yyyy-MM-dd HH:mm:ss" 5 | 6 | $moduleName = Split-Path -path $PSScriptRoot -Leaf 7 | #get changelog data 8 | $change = Get-ChangelogData -Path .\ChangeLog.md 9 | 10 | #get latest release 11 | $modVersion = $change.LastVersion 12 | 13 | #get previous releases 14 | [regex]$rx = "(?<=\[)\d\.\d\.\d" 15 | $releases = $change.footer.split("`n") | Select-Object -skip 1 16 | $prev = $rx.match($releases[1]).value 17 | 18 | $link = "[$modVersion]: https://github.com/jdhitsolutions/$moduleName/compare/v$prev...v$modVersion" 19 | 20 | #$change.footer.split("`n").where({$_ -match "\[$modVersion\]"}) 21 | if ($modVersion -as [version]) { 22 | $md = [System.Collections.Generic.list[String]]::new() 23 | $md.add($change.header.split("`n")[0]) 24 | $md.add("## v[$modVersion] - $dt`n") 25 | $md.add($change.ReleaseNotes) 26 | $md.Add("`n$link") 27 | $md | Out-File .\scratch-change.md 28 | code .\scratch-change.md 29 | } 30 | else { 31 | Write-Warning "Changelog does not follow the newer format." 32 | } 33 | 34 | <# 35 | 36 | # Changelog for PSWorkItem 37 | 38 | ## 0.8.0 39 | 40 | ### Added 41 | 42 | - Add database path as a property to workitem and archived workitem objects. 43 | 44 | ### Changed 45 | 46 | - Update default format view to group by database path. 47 | - Help updates. 48 | 49 | ### Fixed 50 | = Merged [PR#4](https://github.com/jdhitsolutions/PSWorkItem/pull/4) that resolves [Issue #3](https://github.com/jdhitsolutions/PSWorkItem/issues/3) Thank you @jbelina. 51 | 52 | #> -------------------------------------------------------------------------------- /PrepNewRelease.ps1: -------------------------------------------------------------------------------- 1 | #requires -module ChangeLogManagement 2 | 3 | #PrepNewRelease.ps1 4 | 5 | #get module version from manifest 6 | $moduleName = Split-Path $PSScriptRoot -Leaf 7 | $data = Import-PowerShellDataFile .\$moduleName.psd1 8 | $ver = $data.ModuleVersion 9 | 10 | Write-Host "Building release for $moduleName version $ver. Has help been updated? Are you ready to continue?" -ForegroundColor Cyan 11 | Pause 12 | #FirstRelease="https://github.com/jdhitsolutions/$moduleName/tree/v{CUR}"; 13 | 14 | Update-Changelog -ReleaseVersion $ver -LinkMode Automatic -LinkPattern @{ 15 | NormalRelease="https://github.com/jdhitsolutions/$moduleName/compare/v{PREV}..v{CUR}"; 16 | Unreleased ="https://github.com/jdhitsolutions/$moduleName/compare/v{CUR}..HEAD" 17 | } 18 | 19 | #verify and touch-up change log 20 | code $PSScriptRoot\changelog.md 21 | 22 | $msg = @' 23 | Next steps: 24 | - Finalize change log 25 | - Run New-ReleaseChangeLog.ps1 26 | - Update ReleaseNotes in module manifest 27 | $c = Get-ChangelogData 28 | "## $($c.lastVersion)`n`n$($c.ReleaseNotes)" | Set-Clipboard 29 | - git add . 30 | - git commit -m "v" 31 | - publish project 32 | - push release 33 | '@ 34 | 35 | Write-Host $msg -foreground yellow 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ScheduledJobTools 2 | 3 | [![PSGallery Version](https://img.shields.io/powershellgallery/v/ScheduledJobTools.png?style=for-the-badge&logo=powershell&label=PowerShell%20Gallery)](https://www.powershellgallery.com/packages/ScheduledJobTools/) [![PSGallery Downloads](https://img.shields.io/powershellgallery/dt/ScheduledJobTools.png?style=for-the-badge&label=Downloads)](https://www.powershellgallery.com/packages/ScheduledJobTools/) 4 | 5 | A PowerShell module with commands for working with scheduled jobs. PowerShell scheduled jobs are only supported on Windows platforms. You can install this module from the PowerShell Gallery: 6 | 7 | ```powershell 8 | Install-Module ScheduledJobTools 9 | ``` 10 | 11 | This module will not run on PowerShell 7 since it does not support the ScheduledJob module. 12 | 13 | ## Commands 14 | 15 | The module consists of these commands: 16 | 17 | [Export-ScheduledJob](/docs/Export-ScheduledJob.md) 18 | 19 | This command will export a scheduled job configuration to an XML file, making it easier to recreate on another computer or if you need to re-install. 20 | 21 | [Import-ScheduledJob](/docs/Import-ScheduledJob.md) 22 | 23 | Assuming you have exported the scheduled job, use this command to import it and recreate the configuration. 24 | 25 | [Get-ScheduledJobResult](/docs/Get-ScheduledJobResult.md) 26 | 27 | This command is designed to make it easier to get the most recent results of your scheduled job. 28 | 29 | ```text 30 | PS C:\> Get-ScheduledJobResult 31 | 32 | ID Name StartTime EndTime Runtime State 33 | -- ---- --------- ------- ------- ----- 34 | 551 OfflineTickle 9/4/2024 10:00:13 AM 9/4/2024 10:00:14 AM 00:00:01.0010018 Completed 35 | 154 myTasksEmail 9/4/2024 8:00:08 AM 9/4/2024 8:00:11 AM 00:00:02.9260021 Completed 36 | 58 WeeklyFullBackup 8/28/2024 10:00:08 PM 8/28/2024 10:14:43 PM 00:14:35.6991443 Completed 37 | 553 RemoteOpWatcher 9/4/2024 10:33:56 AM 9/4/2024 10:33:56 AM 00:00:00.6534636 Completed 38 | 153 DailyIncremental 9/3/2024 10:00:07 PM 9/3/2024 10:00:39 PM 00:00:31.3470147 Completed 39 | 72 JDHITBackup 9/3/2024 6:00:07 PM 9/3/2024 6:00:10 PM 00:00:03.1494835 Completed 40 | ``` 41 | 42 | The function has an alias of `ljr`. 43 | 44 | [Remove-OldJobResult](/docs/Remove-OldJobResult.md) 45 | 46 | Use this command to remove all but the most recent scheduled job result. 47 | 48 | [Get-ScheduledJobDetail](/docs/Get-ScheduledJobDetail.md) 49 | 50 | PowerShell Scheduled jobs are intertwined with Scheduled Tasks. There is a lot of useful information, but it is buried in nested objects and properties. This command is designed to make it easier to get detailed information about a scheduled job. 51 | 52 | ```text 53 | PS C:\> Get-ScheduledJobDetail -Name DailyIncremental 54 | 55 | 56 | ID : 3 57 | Name : DailyIncremental 58 | Command : C:\scripts\PSBackup\DailyIncrementalBackup.ps1 59 | Enabled : True 60 | State : Ready 61 | NextRun : 9/5/2024 10:00:00 PM 62 | MaxHistory : 7 63 | RunAs : BOVINE320\Jeff 64 | Frequency : Weekly 65 | Days : {Sunday, Monday, Tuesday, Wednesday...} 66 | RepetitionDuration : 67 | RepetitionInterval : 68 | DoNotAllowDemandStart : False 69 | IdleDuration : 00:10:00 70 | IdleTimeout : 01:00:00 71 | MultipleInstancePolicy : IgnoreNew 72 | RestartOnIdleResume : False 73 | RunElevated : True 74 | RunWithoutNetwork : False 75 | ShowInTaskScheduler : True 76 | StartIfNotIdle : True 77 | StartIfOnBatteries : False 78 | StopIfGoingOffIdle : False 79 | StopIfGoingOnBatteries : True 80 | WakeToRun : True 81 | ``` 82 | 83 | ## Customizations 84 | 85 | When you import the module, it will modify the ScheduledJob object to define a script property called `NextRun`. This makes it easier to view when a job is scheduled to run again. 86 | 87 | ```text 88 | PS C:\> Get-ScheduledJob JDHITBackup | Select-Object Name,NextRun 89 | 90 | Name NextRun 91 | ---- ------- 92 | JDHITBackup 9/4/2024 6:00:00 PM 93 | ``` 94 | 95 | Or you can use a custom table view. 96 | 97 | ```text 98 | PS C:\> Get-ScheduledJob | Format-Table -view NextRun 99 | 100 | Id Enabled Name NextRun Command 101 | -- ------- ---- ------- ------- 102 | 2 False DailyDiskReport 9/4/2024 11:00:00 PM C:\scripts\DiskReports.ps1 103 | 3 True DailyIncremental 9/5/2024 10:00:00 PM C:\scripts\PSBackup\DailyIncrementalBackup.ps1 104 | 4 True DailyWatcher ... 105 | 5 False HelpUpdate 9/28/2024 12:00:00 PM Update-Help 106 | 6 True JDHITBackup 9/4/2024 6:00:00 PM C:\scripts\Backup-JDHIT.ps1 107 | 8 True myTasksEmail 9/5/2024 8:00:00 AM ... 108 | 9 True OfflineTickle 9/4/2024 11:00:00 AM ... 109 | 10 True RemoteOpWatcher 9/4/2024 10:41:41 AM ... 110 | 11 True WeeklyFullBackup 9/4/2024 10:00:00 PM C:\scripts\PSBackup\WeeklyFullBackup.ps1 111 | 73 True tmp50E8 9/4/2024 12:45:45 PM ... 112 | ``` 113 | 114 | Version 2.3.0 introduced a property set called `RunInfo`. 115 | 116 | ```text 117 | PS C:\> Get-ScheduledJob WeeklyFullBackup | Select-Object RunInfo 118 | 119 | Name NextRun LastRun Enabled 120 | ---- ------- ------- ------- 121 | WeeklyFullBackup 10/18/2024 10:00:00 PM 10/11/2024 10:14:16 PM True 122 | ``` 123 | 124 | The view uses ANSI escape sequences and will color `False` in red. 125 | -------------------------------------------------------------------------------- /ScheduledJobTools.psd1: -------------------------------------------------------------------------------- 1 | # Module manifest for module 'ScheduledJobTools' 2 | 3 | @{ 4 | RootModule = 'ScheduledJobTools.psm1' 5 | ModuleVersion = '2.3.0' 6 | CompatiblePSEditions = @("Desktop") 7 | GUID = '35b74812-dfdb-405f-a3bd-5178dcd9827e' 8 | Author = 'Jeff Hicks' 9 | CompanyName = 'JDH Information Technology Solutions, Inc.' 10 | Copyright = '(c)2013-2024 JDH Information Technology Solutions, Inc. All Rights Reserved' 11 | Description = 'A PowerShell module for managing PowerShell scheduled jobs and their results.' 12 | PowerShellVersion = '5.1' 13 | PowerShellHostVersion = '5.1' 14 | TypesToProcess = @(".\types\ScheduledJobDefinition.types.ps1xml") 15 | FormatsToProcess = @("formats\ScheduledJobResult.format.ps1xml", ".\formats\ScheduledJob.format.ps1xml") 16 | FunctionsToExport = "Export-ScheduledJob", "Import-ScheduledJob", "Get-ScheduledJobResult", "Remove-OldJobResult", 17 | "Get-ScheduledJobDetail" 18 | AliasesToExport = "esj", "isj", "ljr" 19 | PrivateData = @{ 20 | PSData = @{ 21 | ExternalModuleDependencies = @('PSScheduledJob', 'ScheduledTasks') 22 | Tags = @('ScheduledJob') 23 | LicenseUri = 'https://github.com/jdhitsolutions/ScheduledJobtools/blob/v1.3/License.txt' 24 | ProjectUri = 'https://github.com/jdhitsolutions/ScheduledJobtools' 25 | 26 | # A URL to an icon representing this module. 27 | # IconUri = '' 28 | 29 | # ReleaseNotes of this module 30 | ReleaseNotes = @" 31 | ## ScheduledJobTools 2.3.0 32 | 33 | ### Added 34 | 35 | - Added property set `RunInfo` for the scheduled job definition type. 36 | - Added script property `LastRun` to the scheduled job definition type. 37 | 38 | ### Changed 39 | 40 | - Moved script property definitions to the types ps1xml file. 41 | - Code cleanup. 42 | - Converted Changelog to new format. 43 | - Reorganized module layout. 44 | - Updated online help links. 45 | - Minor help updates. 46 | "@ 47 | 48 | } # End of PSData hashtable 49 | 50 | } # End of PrivateData hashtable 51 | 52 | # HelpInfo URI of this module 53 | # HelpInfoURI = '' 54 | 55 | # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. 56 | # DefaultCommandPrefix = '' 57 | 58 | } 59 | -------------------------------------------------------------------------------- /ScheduledJobTools.psm1: -------------------------------------------------------------------------------- 1 |  2 | (Get-ChildItem -Path $PSScriptRoot\functions\*.ps1).Foreach( { . $_.FullName }) 3 | 4 | Class ScheduledJobResult { 5 | [string]$Name 6 | [DateTime]$StartTime 7 | [DateTime]$EndTime 8 | [TimeSpan]$Runtime 9 | [Int]$ID 10 | [string]$State 11 | [bool]$HasMoreData 12 | [System.Management.Automation.Job[]]$ChildJobs 13 | [PSObject[]]$Output 14 | 15 | [PSObject[]] GetResults() { 16 | if ($this.HasMoreData) { 17 | $data = Receive-Job -Id $this.id -Keep 18 | } 19 | else { 20 | $data = $null 21 | } 22 | return $data 23 | } 24 | 25 | ScheduledJobResult ([int]$ID) { 26 | $job = Get-Job -Id $ID 27 | $this.Name = $job.Name 28 | $this.StartTime = $job.PSBeginTime 29 | $this.EndTime = $job.PSEndTime 30 | $this.Runtime = $job.PSEndTime - $job.PSBeginTime 31 | $this.ID = $job.Id 32 | $this.state = $job.State 33 | $this.HasMoreData = $job.HasMoreData 34 | $this.ChildJobs = $job.ChildJobs 35 | $this.Output = $job.Output 36 | } 37 | 38 | ScheduledJobResult ([Microsoft.PowerShell.ScheduledJob.ScheduledJob]$job) { 39 | $this.Name = $job.Name 40 | $this.StartTime = $job.PSBeginTime 41 | $this.EndTime = $job.PSEndTime 42 | $this.Runtime = $job.PSEndTime - $job.PSBeginTime 43 | $this.ID = $job.Id 44 | $this.state = $job.State 45 | $this.HasMoreData = $job.HasMoreData 46 | $this.ChildJobs = $job.ChildJobs 47 | $this.Output = $job.Output 48 | } 49 | 50 | } 51 | 52 | #set default display properties 53 | Update-TypeData -TypeName ScheduledJobResult -DefaultDisplayPropertySet Name, ID, State, Runtime, StartTime, EndTime, HasMoreData -Force 54 | 55 | #add a property to the ScheduledJob that shows the next run time 56 | #17 Oct 2024 -these have been moved to the types ps1xml file. 57 | # Update-TypeData -TypeName Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition -MemberName "NextRun" -MemberType ScriptProperty -Value { (Get-ScheduledTask -TaskName $this.name | Get-ScheduledTaskInfo).nextRunTime } -Force 58 | # Update-TypeData -TypeName Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition -MemberName "LastRun" -MemberType ScriptProperty -Value { (Get-job $This.Name -Newest 1).PSEndTime } -Force 59 | 60 | #add argument completers to auto populate scheduled job names 61 | $sb = { 62 | param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) 63 | (Get-ScheduledJob -Name "$WordToComplete*").name | ForEach-Object { 64 | [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) 65 | } 66 | } 67 | 68 | $completerParams = @{ 69 | CommandName = 'Export-ScheduledJob', 'Get-ScheduledJobResult', 'Remove-OldJobResult', 'Get-ScheduledJobDetail' 70 | ParameterName = 'Name' 71 | ScriptBlock = $sb 72 | } 73 | Register-ArgumentCompleter @completerParams 74 | 75 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Change Log for ScheduledJobTools 2 | 3 | ## [Unreleased] 4 | 5 | ## [2.3.0] - 2024-10-17 6 | 7 | ### Added 8 | 9 | - Added property set `RunInfo` for the scheduled job definition type. 10 | - Added script property `LastRun` to the scheduled job definition type. 11 | 12 | ### Changed 13 | 14 | - Moved script property definitions to the types ps1xml file. 15 | - Code cleanup. 16 | - Converted Changelog to new format. 17 | - Reorganized module layout. 18 | - Updated online help links. 19 | - Minor help updates. 20 | 21 | ## [v2.2.0] - 2020-09-04 22 | 23 | - Defined new custom object type for `Get-ScheduledJobDetail`. 24 | - Added a custom table view called `NextRun` to be used when running `Get-ScheduledJob`. 25 | - Added a script property called `NextRun` to the ScheduledJob object. 26 | - Reorganized the module. 27 | - Help updates. 28 | - Updated `README.md`. 29 | 30 | ## [v2.1.0] - 2019-02-15 31 | 32 | - Added `PSScheduledJob` as a required module to the manifest. 33 | - Added `ScheduledJobResult.format.ps1xml` to format output from `Get-ScheduledJobResult` as a table. 34 | - Revised `Get-ScheduledJobDetail` to have a default parameter set. 35 | - Updated license copyright year. 36 | 37 | ## [v2.0.0] - 2018-10-23 38 | 39 | - Changed minimum version to 5.1 and supporting Desktop edition only. 40 | - file cleanup for the PowerShell Gallery. 41 | - Moved alias definitions to respective functions. 42 | - Updated help. 43 | 44 | ## v1.4.0 - 2018-06-29 45 | 46 | - Added `Get-ScheduledJobDetail`. 47 | 48 | ## [v1.3.1] - 2018-06-29 49 | 50 | - manifest changes. 51 | - published to PowerShell Gallery. 52 | 53 | ## v1.3 54 | 55 | - Added `Remove-OldJobResult`. 56 | - Updated `README.md`. 57 | 58 | ## v1.2 59 | 60 | - moved to external help. 61 | - code cleanup. 62 | - Added `Get-ScheduledJobResult` command and associated class. 63 | - Added an auto-completer for Name parameter in `Export-ScheduledJob` and `Get-ScheduledJobResult`. 64 | - increased required PowerShell version to 5.0. 65 | 66 | ## v1.1 67 | 68 | - initial release. 69 | 70 | [Unreleased]: https://github.com/jdhitsolutions/ScheduledJobTools/compare/v2.3.0..HEAD 71 | [2.3.0]: https://github.com/jdhitsolutions/ScheduledJobTools/compare/vv2.2.0..v2.3.0 72 | [v2.2.0]: https://github.com/jdhitsolutions/ScheduledJobtools/compare/v2.1.0..v2.2.0 73 | [v2.1.0]: https://github.com/jdhitsolutions/ScheduledJobtools/compare/v2.0.0..v2.1.0 74 | [v2.0.0]: https://github.com/jdhitsolutions/ScheduledJobTools/compare/v2.0.0..v1.3.1 75 | [v1.3.1]: https://github.com/jdhitsolutions/ScheduledJobTools/compare/v1.3.1..v1.3 -------------------------------------------------------------------------------- /docs/Export-ScheduledJob.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: ScheduledJobTools-help.xml 3 | Module Name: ScheduledJobTools 4 | online version: https://bit.ly/3eQGcf3 5 | schema: 2.0.0 6 | --- 7 | 8 | # Export-ScheduledJob 9 | 10 | ## SYNOPSIS 11 | 12 | Export a PowerShell scheduled job. 13 | 14 | ## SYNTAX 15 | 16 | ### name (Default) 17 | 18 | ```yaml 19 | Export-ScheduledJob [-Name] [-Path ] [-Passthru] [-WhatIf] [-Confirm] [] 20 | ``` 21 | 22 | ### job 23 | 24 | ```yaml 25 | Export-ScheduledJob [-ScheduledJob] [-Path ] [-Passthru] [-WhatIf] [-Confirm] [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | This command will export a PowerShell scheduled job to an XML file using the Export-CliXML cmdlet. This file can be used to import the scheduled task on another computer running PowerShell 3.0 or later. 31 | 32 | ## EXAMPLES 33 | 34 | ### EXAMPLE 1 35 | 36 | ```PowerShell 37 | PS C:\> Export-ScheduledJob MyJob -path \\chi-fp01\it\jobs 38 | ``` 39 | 40 | Export a single job to \\chi-fp01\it\jobs. The filename will be MyJob.xml. 41 | 42 | ### EXAMPLE 2 43 | 44 | ```powershell 45 | PS C:\> Get-ScheduledJob | Export-ScheduledJob -path d:\backup 46 | ``` 47 | 48 | Export all scheduled jobs to a backup folder. 49 | 50 | ## PARAMETERS 51 | 52 | ### -Confirm 53 | 54 | Prompts you for confirmation before running the cmdlet. 55 | 56 | ```yaml 57 | Type: SwitchParameter 58 | Parameter Sets: (All) 59 | Aliases: cf 60 | 61 | Required: False 62 | Position: Named 63 | Default value: None 64 | Accept pipeline input: False 65 | Accept wildcard characters: False 66 | ``` 67 | 68 | ### -Name 69 | 70 | The name of a PowerShell scheduled job. 71 | 72 | ```yaml 73 | Type: String 74 | Parameter Sets: name 75 | Aliases: 76 | 77 | Required: True 78 | Position: 0 79 | Default value: None 80 | Accept pipeline input: True (ByValue) 81 | Accept wildcard characters: False 82 | ``` 83 | 84 | ### -Passthru 85 | 86 | Write the XML file object to the pipeline. 87 | 88 | ```yaml 89 | Type: SwitchParameter 90 | Parameter Sets: (All) 91 | Aliases: 92 | 93 | Required: False 94 | Position: Named 95 | Default value: False 96 | Accept pipeline input: False 97 | Accept wildcard characters: False 98 | ``` 99 | 100 | ### -Path 101 | 102 | The path to store the XML file. The file name will be the same as the job name. The default is the current location. 103 | 104 | ```yaml 105 | Type: String 106 | Parameter Sets: (All) 107 | Aliases: 108 | 109 | Required: False 110 | Position: Named 111 | Default value: (Get-Location).Path 112 | Accept pipeline input: False 113 | Accept wildcard characters: False 114 | ``` 115 | 116 | ### -ScheduledJob 117 | 118 | A PowerShell scheduled job object. 119 | 120 | ```yaml 121 | Type: ScheduledJobDefinition 122 | Parameter Sets: job 123 | Aliases: job 124 | 125 | Required: True 126 | Position: 0 127 | Default value: None 128 | Accept pipeline input: True (ByValue) 129 | Accept wildcard characters: False 130 | ``` 131 | 132 | ### -WhatIf 133 | 134 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 135 | 136 | ```yaml 137 | Type: SwitchParameter 138 | Parameter Sets: (All) 139 | Aliases: wi 140 | 141 | Required: False 142 | Position: Named 143 | Default value: None 144 | Accept pipeline input: False 145 | Accept wildcard characters: False 146 | ``` 147 | 148 | ### CommonParameters 149 | 150 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 151 | 152 | ## INPUTS 153 | 154 | ### System.String 155 | 156 | ### Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 157 | 158 | ## OUTPUTS 159 | 160 | ### None 161 | 162 | ### System.IO.FileInfo 163 | 164 | ## NOTES 165 | 166 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 167 | 168 | ## RELATED LINKS 169 | 170 | [Export-Clixml]() 171 | 172 | [Get-ScheduledJob]() 173 | 174 | [Import-ScheduledJob](Import-ScheduledJob.md) 175 | -------------------------------------------------------------------------------- /docs/Get-ScheduledJobDetail.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: ScheduledJobTools-help.xml 3 | Module Name: ScheduledJobTools 4 | online version: https://bit.ly/3nNLImP 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-ScheduledJobDetail 9 | 10 | ## SYNOPSIS 11 | 12 | Get details for a PowerShell Scheduled Job. 13 | 14 | ## SYNTAX 15 | 16 | ### name (Default) 17 | 18 | ```yaml 19 | Get-ScheduledJobDetail [-Name] [] 20 | ``` 21 | 22 | ### job 23 | 24 | ```yaml 25 | Get-ScheduledJobDetail [-ScheduledJob] [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | The PowerShell scheduled job object is very rich. There is a great deal of information but much of it is nested. This command will expose the most common information you might want to know about a scheduled job. 31 | 32 | ## EXAMPLES 33 | 34 | ### Example 1 35 | 36 | ```powershell 37 | PS C:\> Get-ScheduledJobDetail jdhitbackup 38 | 39 | 40 | 41 | ID : 19 42 | Name : JDHITBackup 43 | Command : C:\scripts\Backup-JDHIT.ps1 44 | Enabled : True 45 | State : Ready 46 | NextRun : 6/29/2024 6:00:00 PM 47 | MaxHistory : 5 48 | RunAs : Jeff 49 | Frequency : Daily 50 | Days : 51 | RepetitionDuration : 52 | RepetitionInterval : 53 | DoNotAllowDemandStart : False 54 | IdleDuration : 00:10:00 55 | IdleTimeout : 01:00:00 56 | MultipleInstancePolicy : IgnoreNew 57 | RestartOnIdleResume : False 58 | RunElevated : True 59 | RunWithoutNetwork : False 60 | ShowInTaskScheduler : True 61 | StartIfNotIdle : True 62 | StartIfOnBatteries : False 63 | StopIfGoingOffIdle : False 64 | StopIfGoingOnBatteries : True 65 | WakeToRun : False 66 | ``` 67 | 68 | Get job details for a single scheduled job. 69 | 70 | ### Example 2 71 | 72 | ```powershell 73 | PS C:\> Get-ScheduledJob | Get-ScheduledJobDetail | Out-GridView 74 | ``` 75 | 76 | Get job detail for all scheduled jobs and pipe the results to Out-GridView. 77 | 78 | ## PARAMETERS 79 | 80 | ### -Name 81 | 82 | Enter the name of a scheduled job. 83 | 84 | ```yaml 85 | Type: String[] 86 | Parameter Sets: name 87 | Aliases: 88 | 89 | Required: True 90 | Position: 0 91 | Default value: None 92 | Accept pipeline input: True (ByValue) 93 | Accept wildcard characters: False 94 | ``` 95 | 96 | ### -ScheduledJob 97 | 98 | A scheduled job object. 99 | 100 | ```yaml 101 | Type: ScheduledJobDefinition 102 | Parameter Sets: job 103 | Aliases: job 104 | 105 | Required: True 106 | Position: 0 107 | Default value: None 108 | Accept pipeline input: True (ByValue) 109 | Accept wildcard characters: False 110 | ``` 111 | 112 | ### CommonParameters 113 | 114 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 115 | 116 | ## INPUTS 117 | 118 | ### System.String[] 119 | 120 | ### Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 121 | 122 | ## OUTPUTS 123 | 124 | ### System.Object 125 | 126 | ## NOTES 127 | 128 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 129 | 130 | ## RELATED LINKS 131 | 132 | [Get-ScheduledJob]() 133 | 134 | [Get-ScheduledTask]() 135 | 136 | [Get-ScheduledTaskInfo]() 137 | -------------------------------------------------------------------------------- /docs/Get-ScheduledJobResult.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: ScheduledJobTools-help.xml 3 | Module Name: ScheduledJobTools 4 | online version: https://bit.ly/3vUaiVZ 5 | schema: 2.0.0 6 | --- 7 | 8 | # Get-ScheduledJobResult 9 | 10 | ## SYNOPSIS 11 | 12 | Get a PowerShell scheduled job result. 13 | 14 | ## SYNTAX 15 | 16 | ```yaml 17 | Get-ScheduledJobResult [[-Name] ] [-Newest ] [-All] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | 22 | This command will retrieve the last job result for a given PowerShell scheduled job. By default, when you create a scheduled job, PowerShell will retain the last 32 job results. Although you can modify this MaxHistoryCount value. This command can help you get the most recent job result. The default behavior is to retrieve the newest job for all enabled scheduled jobs. But you can specify scheduled jobs by name, indicate if you want to see all jobs, and also the number of recent jobs. 23 | 24 | The command uses a custom type definition based on the Job object. 25 | 26 | ## EXAMPLES 27 | 28 | ### EXAMPLE 1 29 | 30 | ```PowerShell 31 | PS C:\> Get-ScheduledJobResult 32 | 33 | 34 | ID Name StartTime EndTime Runtime State 35 | -- ---- --------- ------- ------- ----- 36 | 20 OfflineTickle 2/15/2024 12:00:08 PM 2/15/2024 12:00:09 PM 00:00:00.9199788 Completed 37 | 25 DownloadHelp 2/15/2024 8:00:04 AM 2/15/2024 8:00:06 AM 00:00:02.2474913 Completed 38 | 44 RemoteOpWatcher 2/15/2024 12:49:53 PM 2/15/2024 12:49:53 PM 00:00:00.4630425 Completed 39 | 15 myTasksEmail 2/15/2024 8:00:04 AM 2/15/2024 8:00:06 AM 00:00:01.6949456 Completed 40 | ``` 41 | 42 | ### EXAMPLE 2 43 | 44 | ```powershell 45 | PS C:\> Get-ScheduledJobResult myTasksEmail -newest 3 46 | 47 | ID Name StartTime EndTime Runtime State 48 | -- ---- --------- ------- ------- ----- 49 | 15 myTasksEmail 2/15/2024 8:00:04 AM 2/15/2024 8:00:06 AM 00:00:01.6949456 Completed 50 | 14 myTasksEmail 2/14/2024 8:00:04 AM 2/14/2024 8:00:06 AM 00:00:01.6648106 Completed 51 | 13 myTasksEmail 2/13/2024 8:00:04 AM 2/13/2024 8:00:05 AM 00:00:01.4718193 Completed 52 | ``` 53 | 54 | Get the newest 3 job results for the myTasksEmail scheduled job. 55 | 56 | ### EXAMPLE 3 57 | 58 | ```powershell 59 | PS C:\> Get-ScheduledJobResult offlinetickle | format-list 60 | 61 | Name : OfflineTickle 62 | ID : 20 63 | State : Completed 64 | Runtime : 00:00:00.9199788 65 | StartTime : 2/15/2024 12:00:08 PM 66 | EndTime : 2/15/2024 12:00:09 PM 67 | HasMoreData : True 68 | ``` 69 | 70 | Get all properties of the result object. 71 | 72 | ## PARAMETERS 73 | 74 | ### -Name 75 | 76 | The name of a PowerShell scheduled job. 77 | 78 | ```yaml 79 | Type: String 80 | Parameter Sets: (All) 81 | Aliases: 82 | 83 | Required: False 84 | Position: 1 85 | Default value: * 86 | Accept pipeline input: False 87 | Accept wildcard characters: False 88 | ``` 89 | 90 | ### -Newest 91 | 92 | The number of newest job results to retrieve. 93 | 94 | ```yaml 95 | Type: Int32 96 | Parameter Sets: (All) 97 | Aliases: 98 | 99 | Required: False 100 | Position: Named 101 | Default value: 1 102 | Accept pipeline input: False 103 | Accept wildcard characters: False 104 | ``` 105 | 106 | ### -All 107 | 108 | Display all scheduled jobs. The default is enabled only. 109 | 110 | ```yaml 111 | Type: SwitchParameter 112 | Parameter Sets: (All) 113 | Aliases: 114 | 115 | Required: False 116 | Position: Named 117 | Default value: False 118 | Accept pipeline input: False 119 | Accept wildcard characters: False 120 | ``` 121 | 122 | ### CommonParameters 123 | 124 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 125 | 126 | ## INPUTS 127 | 128 | ### None 129 | 130 | ## OUTPUTS 131 | 132 | ### ScheduledJobResult 133 | 134 | ## NOTES 135 | 136 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 137 | 138 | ## RELATED LINKS 139 | 140 | [Get-Job]() 141 | 142 | [Get-ScheduledJob]() 143 | -------------------------------------------------------------------------------- /docs/Import-ScheduledJob.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: ScheduledJobTools-help.xml 3 | Module Name: ScheduledJobTools 4 | online version: https://bit.ly/2Rn2EEn 5 | schema: 2.0.0 6 | --- 7 | 8 | # Import-ScheduledJob 9 | 10 | ## SYNOPSIS 11 | 12 | Import a PowerShell scheduled job from an XML file. 13 | 14 | ## SYNTAX 15 | 16 | ```yaml 17 | Import-ScheduledJob [-Path] [-Passthru] [-WhatIf] [-Confirm] [] 18 | ``` 19 | 20 | ## DESCRIPTION 21 | 22 | This command will take an exported PowerShell scheduled job stored in an XML file and import it, re-creating the scheduled job. If the job uses credentials you will be prompted for the credential password. 23 | 24 | ## EXAMPLES 25 | 26 | ### EXAMPLE 1 27 | 28 | ```powershell 29 | PS C:\> Import-ScheduledJob \\chi-fp01\it\jobs\myjob.xml 30 | ``` 31 | 32 | Import a scheduled job from an XML file located on a file share. 33 | 34 | ## PARAMETERS 35 | 36 | ### -Confirm 37 | 38 | Prompts you for confirmation before running the cmdlet. 39 | 40 | ```yaml 41 | Type: SwitchParameter 42 | Parameter Sets: (All) 43 | Aliases: cf 44 | 45 | Required: False 46 | Position: Named 47 | Default value: None 48 | Accept pipeline input: False 49 | Accept wildcard characters: False 50 | ``` 51 | 52 | ### -Passthru 53 | 54 | Write the new job to the pipeline. 55 | 56 | ```yaml 57 | Type: SwitchParameter 58 | Parameter Sets: (All) 59 | Aliases: 60 | 61 | Required: False 62 | Position: Named 63 | Default value: False 64 | Accept pipeline input: False 65 | Accept wildcard characters: False 66 | ``` 67 | 68 | ### -Path 69 | 70 | The path and file name with the exported job definition. 71 | 72 | ```yaml 73 | Type: String 74 | Parameter Sets: (All) 75 | Aliases: name, pspath 76 | 77 | Required: True 78 | Position: 0 79 | Default value: None 80 | Accept pipeline input: True (ByValue) 81 | Accept wildcard characters: False 82 | ``` 83 | 84 | ### -WhatIf 85 | 86 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 87 | 88 | ```yaml 89 | Type: SwitchParameter 90 | Parameter Sets: (All) 91 | Aliases: wi 92 | 93 | Required: False 94 | Position: Named 95 | Default value: None 96 | Accept pipeline input: False 97 | Accept wildcard characters: False 98 | ``` 99 | 100 | ### CommonParameters 101 | 102 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 103 | 104 | ## INPUTS 105 | 106 | ### System.String 107 | 108 | ## OUTPUTS 109 | 110 | ### None 111 | 112 | ### Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 113 | 114 | ## NOTES 115 | 116 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 117 | 118 | ## RELATED LINKS 119 | 120 | [Register-ScheduledJob]() 121 | 122 | [Export-ScheduledJob]() 123 | 124 | [Import-CliXML]() 125 | -------------------------------------------------------------------------------- /docs/Remove-OldJobResult.md: -------------------------------------------------------------------------------- 1 | --- 2 | external help file: ScheduledJobTools-help.xml 3 | Module Name: ScheduledJobTools 4 | online version: https://bit.ly/3eVNb6b 5 | schema: 2.0.0 6 | --- 7 | 8 | # Remove-OldJobResult 9 | 10 | ## SYNOPSIS 11 | 12 | Remove old job results 13 | 14 | ## SYNTAX 15 | 16 | ### name (Default) 17 | 18 | ```yaml 19 | Remove-OldJobResult [-Name] [-WhatIf] [-Confirm] [] 20 | ``` 21 | 22 | ### job 23 | 24 | ```yaml 25 | Remove-OldJobResult [-ScheduledJob] [-WhatIf] [-Confirm] [] 26 | ``` 27 | 28 | ## DESCRIPTION 29 | 30 | Use this command to remove all but the most recent scheduled job result. 31 | 32 | ## EXAMPLES 33 | 34 | ### Example 1 35 | 36 | ```powershell 37 | PS C:\> Remove-OldJobResult -name DailyBackup 38 | ``` 39 | 40 | Remove all the but the most recent result for the DailyBackup job. 41 | 42 | ### Example 2 43 | 44 | ```powershell 45 | PS C:\> Get-ScheduledJob | Remove-OldJobResult 46 | ``` 47 | 48 | Get all scheduled jobs and pipe them to this command to delete all but the most recent result. 49 | 50 | ## PARAMETERS 51 | 52 | ### -Confirm 53 | 54 | Prompts you for confirmation before running the cmdlet. 55 | 56 | ```yaml 57 | Type: SwitchParameter 58 | Parameter Sets: (All) 59 | Aliases: cf 60 | 61 | Required: False 62 | Position: Named 63 | Default value: None 64 | Accept pipeline input: False 65 | Accept wildcard characters: False 66 | ``` 67 | 68 | ### -Name 69 | 70 | The name of a scheduled job. 71 | 72 | ```yaml 73 | Type: String[] 74 | Parameter Sets: name 75 | Aliases: 76 | 77 | Required: True 78 | Position: 0 79 | Default value: None 80 | Accept pipeline input: True (ByValue) 81 | Accept wildcard characters: False 82 | ``` 83 | 84 | ### -ScheduledJob 85 | 86 | A scheduled job object. 87 | 88 | ```yaml 89 | Type: ScheduledJobDefinition 90 | Parameter Sets: job 91 | Aliases: job 92 | 93 | Required: True 94 | Position: 0 95 | Default value: None 96 | Accept pipeline input: True (ByValue) 97 | Accept wildcard characters: False 98 | ``` 99 | 100 | ### -WhatIf 101 | 102 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 103 | 104 | ```yaml 105 | Type: SwitchParameter 106 | Parameter Sets: (All) 107 | Aliases: wi 108 | 109 | Required: False 110 | Position: Named 111 | Default value: None 112 | Accept pipeline input: False 113 | Accept wildcard characters: False 114 | ``` 115 | 116 | ### CommonParameters 117 | 118 | This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216). 119 | 120 | ## INPUTS 121 | 122 | ### System.String[] 123 | 124 | ### Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 125 | 126 | ## OUTPUTS 127 | 128 | ### None 129 | 130 | ## NOTES 131 | 132 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 133 | 134 | ## RELATED LINKS 135 | 136 | [Get-Job]() 137 | 138 | [Remove-Job]()) 139 | 140 | [Get-ScheduledJob]() 141 | -------------------------------------------------------------------------------- /en-us/ScheduledJobTools-help.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Export-ScheduledJob 6 | Export 7 | ScheduledJob 8 | 9 | Export a PowerShell scheduled job. 10 | 11 | 12 | 13 | This command will export a PowerShell scheduled job to an XML file using the Export-CliXML cmdlet. This file can be used to import the scheduled task on another computer running PowerShell 3.0 or later. 14 | 15 | 16 | 17 | Export-ScheduledJob 18 | 19 | Name 20 | 21 | The name of a PowerShell scheduled job. 22 | 23 | String 24 | 25 | String 26 | 27 | 28 | None 29 | 30 | 31 | Confirm 32 | 33 | Prompts you for confirmation before running the cmdlet. 34 | 35 | 36 | SwitchParameter 37 | 38 | 39 | False 40 | 41 | 42 | Passthru 43 | 44 | Write the XML file object to the pipeline. 45 | 46 | 47 | SwitchParameter 48 | 49 | 50 | False 51 | 52 | 53 | Path 54 | 55 | The path to store the XML file. The file name will be the same as the job name. The default is the current location. 56 | 57 | String 58 | 59 | String 60 | 61 | 62 | (Get-Location).Path 63 | 64 | 65 | WhatIf 66 | 67 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 68 | 69 | 70 | SwitchParameter 71 | 72 | 73 | False 74 | 75 | 76 | 77 | Export-ScheduledJob 78 | 79 | ScheduledJob 80 | 81 | A PowerShell scheduled job object. 82 | 83 | ScheduledJobDefinition 84 | 85 | ScheduledJobDefinition 86 | 87 | 88 | None 89 | 90 | 91 | Confirm 92 | 93 | Prompts you for confirmation before running the cmdlet. 94 | 95 | 96 | SwitchParameter 97 | 98 | 99 | False 100 | 101 | 102 | Passthru 103 | 104 | Write the XML file object to the pipeline. 105 | 106 | 107 | SwitchParameter 108 | 109 | 110 | False 111 | 112 | 113 | Path 114 | 115 | The path to store the XML file. The file name will be the same as the job name. The default is the current location. 116 | 117 | String 118 | 119 | String 120 | 121 | 122 | (Get-Location).Path 123 | 124 | 125 | WhatIf 126 | 127 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 128 | 129 | 130 | SwitchParameter 131 | 132 | 133 | False 134 | 135 | 136 | 137 | 138 | 139 | Confirm 140 | 141 | Prompts you for confirmation before running the cmdlet. 142 | 143 | SwitchParameter 144 | 145 | SwitchParameter 146 | 147 | 148 | False 149 | 150 | 151 | Name 152 | 153 | The name of a PowerShell scheduled job. 154 | 155 | String 156 | 157 | String 158 | 159 | 160 | None 161 | 162 | 163 | Passthru 164 | 165 | Write the XML file object to the pipeline. 166 | 167 | SwitchParameter 168 | 169 | SwitchParameter 170 | 171 | 172 | False 173 | 174 | 175 | Path 176 | 177 | The path to store the XML file. The file name will be the same as the job name. The default is the current location. 178 | 179 | String 180 | 181 | String 182 | 183 | 184 | (Get-Location).Path 185 | 186 | 187 | ScheduledJob 188 | 189 | A PowerShell scheduled job object. 190 | 191 | ScheduledJobDefinition 192 | 193 | ScheduledJobDefinition 194 | 195 | 196 | None 197 | 198 | 199 | WhatIf 200 | 201 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 202 | 203 | SwitchParameter 204 | 205 | SwitchParameter 206 | 207 | 208 | False 209 | 210 | 211 | 212 | 213 | 214 | System.String 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | None 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | System.IO.FileInfo 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 250 | 251 | 252 | 253 | 254 | -------------------------- EXAMPLE 1 -------------------------- 255 | PS C:\> Export-ScheduledJob MyJob -path \\chi-fp01\it\jobs 256 | 257 | Export a single job to \chi-fp01\it\jobs. The filename will be MyJob.xml. 258 | 259 | 260 | 261 | -------------------------- EXAMPLE 2 -------------------------- 262 | PS C:\> Get-ScheduledJob | Export-ScheduledJob -path d:\backup 263 | 264 | Export all scheduled jobs to a backup folder. 265 | 266 | 267 | 268 | 269 | 270 | Online Version: 271 | https://bit.ly/3eQGcf3 272 | 273 | 274 | Export-Clixml 275 | 276 | 277 | 278 | Get-ScheduledJob 279 | 280 | 281 | 282 | Import-ScheduledJob 283 | 284 | 285 | 286 | 287 | 288 | 289 | Get-ScheduledJobDetail 290 | Get 291 | ScheduledJobDetail 292 | 293 | Get details for a PowerShell Scheduled Job. 294 | 295 | 296 | 297 | The PowerShell scheduled job object is very rich. There is a great deal of information but much of it is nested. This command will expose the most common information you might want to know about a scheduled job. 298 | 299 | 300 | 301 | Get-ScheduledJobDetail 302 | 303 | Name 304 | 305 | Enter the name of a scheduled job. 306 | 307 | String[] 308 | 309 | String[] 310 | 311 | 312 | None 313 | 314 | 315 | 316 | Get-ScheduledJobDetail 317 | 318 | ScheduledJob 319 | 320 | A scheduled job object. 321 | 322 | ScheduledJobDefinition 323 | 324 | ScheduledJobDefinition 325 | 326 | 327 | None 328 | 329 | 330 | 331 | 332 | 333 | Name 334 | 335 | Enter the name of a scheduled job. 336 | 337 | String[] 338 | 339 | String[] 340 | 341 | 342 | None 343 | 344 | 345 | ScheduledJob 346 | 347 | A scheduled job object. 348 | 349 | ScheduledJobDefinition 350 | 351 | ScheduledJobDefinition 352 | 353 | 354 | None 355 | 356 | 357 | 358 | 359 | 360 | System.String[] 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | System.Object 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 388 | 389 | 390 | 391 | 392 | -------------------------- Example 1 -------------------------- 393 | PS C:\> Get-ScheduledJobDetail jdhitbackup 394 | 395 | 396 | 397 | ID : 19 398 | Name : JDHITBackup 399 | Command : C:\scripts\Backup-JDHIT.ps1 400 | Enabled : True 401 | State : Ready 402 | NextRun : 6/29/2024 6:00:00 PM 403 | MaxHistory : 5 404 | RunAs : Jeff 405 | Frequency : Daily 406 | Days : 407 | RepetitionDuration : 408 | RepetitionInterval : 409 | DoNotAllowDemandStart : False 410 | IdleDuration : 00:10:00 411 | IdleTimeout : 01:00:00 412 | MultipleInstancePolicy : IgnoreNew 413 | RestartOnIdleResume : False 414 | RunElevated : True 415 | RunWithoutNetwork : False 416 | ShowInTaskScheduler : True 417 | StartIfNotIdle : True 418 | StartIfOnBatteries : False 419 | StopIfGoingOffIdle : False 420 | StopIfGoingOnBatteries : True 421 | WakeToRun : False 422 | 423 | Get job details for a single scheduled job. 424 | 425 | 426 | 427 | -------------------------- Example 2 -------------------------- 428 | PS C:\> Get-ScheduledJob | Get-ScheduledJobDetail | Out-GridView 429 | 430 | Get job detail for all scheduled jobs and pipe the results to Out-GridView. 431 | 432 | 433 | 434 | 435 | 436 | Online Version: 437 | https://bit.ly/3nNLImP 438 | 439 | 440 | Get-ScheduledJob 441 | 442 | 443 | 444 | Get-ScheduledTask 445 | 446 | 447 | 448 | Get-ScheduledTaskInfo 449 | 450 | 451 | 452 | 453 | 454 | 455 | Get-ScheduledJobResult 456 | Get 457 | ScheduledJobResult 458 | 459 | Get a PowerShell scheduled job result. 460 | 461 | 462 | 463 | This command will retrieve the last job result for a given PowerShell scheduled job. By default, when you create a scheduled job, PowerShell will retain the last 32 job results. Although you can modify this MaxHistoryCount value. This command can help you get the most recent job result. The default behavior is to retrieve the newest job for all enabled scheduled jobs. But you can specify scheduled jobs by name, indicate if you want to see all jobs, and also the number of recent jobs. 464 | The command uses a custom type definition based on the Job object. 465 | 466 | 467 | 468 | Get-ScheduledJobResult 469 | 470 | Name 471 | 472 | The name of a PowerShell scheduled job. 473 | 474 | String 475 | 476 | String 477 | 478 | 479 | * 480 | 481 | 482 | Newest 483 | 484 | The number of newest job results to retrieve. 485 | 486 | Int32 487 | 488 | Int32 489 | 490 | 491 | 1 492 | 493 | 494 | All 495 | 496 | Display all scheduled jobs. The default is enabled only. 497 | 498 | 499 | SwitchParameter 500 | 501 | 502 | False 503 | 504 | 505 | 506 | 507 | 508 | Name 509 | 510 | The name of a PowerShell scheduled job. 511 | 512 | String 513 | 514 | String 515 | 516 | 517 | * 518 | 519 | 520 | Newest 521 | 522 | The number of newest job results to retrieve. 523 | 524 | Int32 525 | 526 | Int32 527 | 528 | 529 | 1 530 | 531 | 532 | All 533 | 534 | Display all scheduled jobs. The default is enabled only. 535 | 536 | SwitchParameter 537 | 538 | SwitchParameter 539 | 540 | 541 | False 542 | 543 | 544 | 545 | 546 | 547 | None 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | ScheduledJobResult 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 567 | 568 | 569 | 570 | 571 | -------------------------- EXAMPLE 1 -------------------------- 572 | PS C:\> Get-ScheduledJobResult 573 | 574 | 575 | ID Name StartTime EndTime Runtime State 576 | -- ---- --------- ------- ------- ----- 577 | 20 OfflineTickle 2/15/2024 12:00:08 PM 2/15/2024 12:00:09 PM 00:00:00.9199788 Completed 578 | 25 DownloadHelp 2/15/2024 8:00:04 AM 2/15/2024 8:00:06 AM 00:00:02.2474913 Completed 579 | 44 RemoteOpWatcher 2/15/2024 12:49:53 PM 2/15/2024 12:49:53 PM 00:00:00.4630425 Completed 580 | 15 myTasksEmail 2/15/2024 8:00:04 AM 2/15/2024 8:00:06 AM 00:00:01.6949456 Completed 581 | 582 | 583 | 584 | 585 | 586 | -------------------------- EXAMPLE 2 -------------------------- 587 | PS C:\> Get-ScheduledJobResult myTasksEmail -newest 3 588 | 589 | ID Name StartTime EndTime Runtime State 590 | -- ---- --------- ------- ------- ----- 591 | 15 myTasksEmail 2/15/2024 8:00:04 AM 2/15/2024 8:00:06 AM 00:00:01.6949456 Completed 592 | 14 myTasksEmail 2/14/2024 8:00:04 AM 2/14/2024 8:00:06 AM 00:00:01.6648106 Completed 593 | 13 myTasksEmail 2/13/2024 8:00:04 AM 2/13/2024 8:00:05 AM 00:00:01.4718193 Completed 594 | 595 | Get the newest 3 job results for the myTasksEmail scheduled job. 596 | 597 | 598 | 599 | -------------------------- EXAMPLE 3 -------------------------- 600 | PS C:\> Get-ScheduledJobResult offlinetickle | format-list 601 | 602 | Name : OfflineTickle 603 | ID : 20 604 | State : Completed 605 | Runtime : 00:00:00.9199788 606 | StartTime : 2/15/2024 12:00:08 PM 607 | EndTime : 2/15/2024 12:00:09 PM 608 | HasMoreData : True 609 | 610 | Get all properties of the result object. 611 | 612 | 613 | 614 | 615 | 616 | Online Version: 617 | https://bit.ly/3vUaiVZ 618 | 619 | 620 | Get-Job 621 | 622 | 623 | 624 | Get-ScheduledJob 625 | 626 | 627 | 628 | 629 | 630 | 631 | Import-ScheduledJob 632 | Import 633 | ScheduledJob 634 | 635 | Import a PowerShell scheduled job from an XML file. 636 | 637 | 638 | 639 | This command will take an exported PowerShell scheduled job stored in an XML file and import it, re-creating the scheduled job. If the job uses credentials you will be prompted for the credential password. 640 | 641 | 642 | 643 | Import-ScheduledJob 644 | 645 | Path 646 | 647 | The path and file name with the exported job definition. 648 | 649 | String 650 | 651 | String 652 | 653 | 654 | None 655 | 656 | 657 | Confirm 658 | 659 | Prompts you for confirmation before running the cmdlet. 660 | 661 | 662 | SwitchParameter 663 | 664 | 665 | False 666 | 667 | 668 | Passthru 669 | 670 | Write the new job to the pipeline. 671 | 672 | 673 | SwitchParameter 674 | 675 | 676 | False 677 | 678 | 679 | WhatIf 680 | 681 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 682 | 683 | 684 | SwitchParameter 685 | 686 | 687 | False 688 | 689 | 690 | 691 | 692 | 693 | Confirm 694 | 695 | Prompts you for confirmation before running the cmdlet. 696 | 697 | SwitchParameter 698 | 699 | SwitchParameter 700 | 701 | 702 | False 703 | 704 | 705 | Passthru 706 | 707 | Write the new job to the pipeline. 708 | 709 | SwitchParameter 710 | 711 | SwitchParameter 712 | 713 | 714 | False 715 | 716 | 717 | Path 718 | 719 | The path and file name with the exported job definition. 720 | 721 | String 722 | 723 | String 724 | 725 | 726 | None 727 | 728 | 729 | WhatIf 730 | 731 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 732 | 733 | SwitchParameter 734 | 735 | SwitchParameter 736 | 737 | 738 | False 739 | 740 | 741 | 742 | 743 | 744 | System.String 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | None 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 772 | 773 | 774 | 775 | 776 | -------------------------- EXAMPLE 1 -------------------------- 777 | PS C:\> Import-ScheduledJob \\chi-fp01\it\jobs\myjob.xml 778 | 779 | Import a scheduled job from an XML file located on a file share. 780 | 781 | 782 | 783 | 784 | 785 | Online Version: 786 | https://bit.ly/2Rn2EEn 787 | 788 | 789 | Register-ScheduledJob 790 | 791 | 792 | 793 | Export-ScheduledJob 794 | 795 | 796 | 797 | Import-CliXML 798 | 799 | 800 | 801 | 802 | 803 | 804 | Remove-OldJobResult 805 | Remove 806 | OldJobResult 807 | 808 | Remove old job results 809 | 810 | 811 | 812 | Use this command to remove all but the most recent scheduled job result. 813 | 814 | 815 | 816 | Remove-OldJobResult 817 | 818 | Name 819 | 820 | The name of a scheduled job. 821 | 822 | String[] 823 | 824 | String[] 825 | 826 | 827 | None 828 | 829 | 830 | Confirm 831 | 832 | Prompts you for confirmation before running the cmdlet. 833 | 834 | 835 | SwitchParameter 836 | 837 | 838 | False 839 | 840 | 841 | WhatIf 842 | 843 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 844 | 845 | 846 | SwitchParameter 847 | 848 | 849 | False 850 | 851 | 852 | 853 | Remove-OldJobResult 854 | 855 | ScheduledJob 856 | 857 | A scheduled job object. 858 | 859 | ScheduledJobDefinition 860 | 861 | ScheduledJobDefinition 862 | 863 | 864 | None 865 | 866 | 867 | Confirm 868 | 869 | Prompts you for confirmation before running the cmdlet. 870 | 871 | 872 | SwitchParameter 873 | 874 | 875 | False 876 | 877 | 878 | WhatIf 879 | 880 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 881 | 882 | 883 | SwitchParameter 884 | 885 | 886 | False 887 | 888 | 889 | 890 | 891 | 892 | Confirm 893 | 894 | Prompts you for confirmation before running the cmdlet. 895 | 896 | SwitchParameter 897 | 898 | SwitchParameter 899 | 900 | 901 | False 902 | 903 | 904 | Name 905 | 906 | The name of a scheduled job. 907 | 908 | String[] 909 | 910 | String[] 911 | 912 | 913 | None 914 | 915 | 916 | ScheduledJob 917 | 918 | A scheduled job object. 919 | 920 | ScheduledJobDefinition 921 | 922 | ScheduledJobDefinition 923 | 924 | 925 | None 926 | 927 | 928 | WhatIf 929 | 930 | Shows what would happen if the cmdlet runs. The cmdlet is not run. 931 | 932 | SwitchParameter 933 | 934 | SwitchParameter 935 | 936 | 937 | False 938 | 939 | 940 | 941 | 942 | 943 | System.String[] 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | None 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/ 971 | 972 | 973 | 974 | 975 | -------------------------- Example 1 -------------------------- 976 | PS C:\> Remove-OldJobResult -name DailyBackup 977 | 978 | Remove all the but the most recent result for the DailyBackup job. 979 | 980 | 981 | 982 | -------------------------- Example 2 -------------------------- 983 | PS C:\> Get-ScheduledJob | Remove-OldJobResult 984 | 985 | Get all scheduled jobs and pipe them to this command to delete all but the most recent result. 986 | 987 | 988 | 989 | 990 | 991 | Online Version: 992 | https://bit.ly/3eVNb6b 993 | 994 | 995 | Get-Job 996 | 997 | 998 | 999 | Remove-Job 1000 | 1001 | 1002 | 1003 | Get-ScheduledJob 1004 | 1005 | 1006 | 1007 | 1008 | -------------------------------------------------------------------------------- /formats/ScheduledjobResult.format.ps1xml: -------------------------------------------------------------------------------- 1 |  2 | 5 | 6 | 7 | 8 | default 9 | 10 | ScheduledJobResult 11 | 12 | 13 | 14 | 15 | 16 | 17 | 5 18 | left 19 | 20 | 21 | 22 | 16 23 | left 24 | 25 | 26 | 27 | 24 28 | left 29 | 30 | 31 | 32 | 24 33 | left 34 | 35 | 36 | 37 | 19 38 | left 39 | 40 | 41 | 42 | 12 43 | left 44 | 45 | 46 | 47 | 48 | 49 | 50 | ID 51 | 52 | 53 | Name 54 | 55 | 56 | StartTime 57 | 58 | 59 | EndTime 60 | 61 | 62 | Runtime 63 | 64 | 65 | State 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /formats/scheduledjob.format.ps1xml: -------------------------------------------------------------------------------- 1 |  2 | 5 | 6 | 7 | 8 | 9 | nextrun 10 | 11 | Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 5 20 | left 21 | 22 | 23 | 24 | 10 25 | left 26 | 27 | 28 | 29 | 14 30 | left 31 | 32 | 33 | 34 | 22 35 | left 36 | 37 | 38 | 39 | 34 40 | left 41 | 42 | 43 | 44 | 45 | 46 | 47 | Id 48 | 49 | 50 | 51 | if ($_.enabled) { 52 | "True" 53 | } 54 | else { 55 | "$([char]0x1b)[91mFalse$([char]0x1b)[0m" 56 | } 57 | 58 | 59 | 60 | Name 61 | 62 | 63 | NextRun 64 | 65 | 66 | Command 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /functions/Export-ScheduledJob.ps1: -------------------------------------------------------------------------------- 1 | Function Export-ScheduledJob { 2 | 3 | [cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = "name")] 4 | [OutputType("None", "System.IO.FileInfo")] 5 | [Alias("esj")] 6 | 7 | Param( 8 | [Parameter(Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = "name")] 9 | [ValidateNotNullOrEmpty()] 10 | [string]$Name, 11 | 12 | [Parameter(Position = 0, Mandatory, ValueFromPipeline, ParameterSetName = "job")] 13 | [ValidateNotNullOrEmpty()] 14 | [alias("job")] 15 | [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]$ScheduledJob, 16 | 17 | [ValidateScript( { 18 | if (-Not (Test-Path -path $_)) { 19 | Throw "Could not verify the path." 20 | } 21 | else { 22 | $True 23 | } 24 | })] 25 | [ValidateScript( { Test-Path $_ })] 26 | [string]$Path = (Get-Location).Path, 27 | [switch]$Passthru 28 | ) 29 | 30 | Begin { 31 | Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN] Starting $($MyInvocation.MyCommand)" 32 | } #begin 33 | 34 | Process { 35 | 36 | if ($Name) { 37 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting scheduled job $job" 38 | Try { 39 | $ExportJob = Get-ScheduledJob -Name $name -ErrorAction Stop 40 | } 41 | Catch { 42 | Write-Warning "Failed to get scheduled job $name" 43 | #bail out 44 | Return 45 | } 46 | } #if 47 | else { 48 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using scheduled job $($ScheduledJob.name)" 49 | $ExportJob = $ScheduledJob 50 | } 51 | 52 | $ExportPath = Join-Path -Path $path -ChildPath "$($ExportJob.Name).xml" 53 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Starting the export process of $($ExportJob.Name) to $ExportPath" 54 | 55 | $ExportJob | Select-Object -property Name, 56 | @{Name = "Scriptblock"; 57 | Expression = { 58 | ($_.InvocationInfo.Parameters.Item(0) | 59 | Where-Object { $_.name -eq "ScriptBlock" }).value 60 | } 61 | }, 62 | @{Name = "FilePath"; 63 | Expression = { 64 | ($_.InvocationInfo.Parameters.Item(0) | 65 | Where-Object { $_.name -eq "FilePath" }).value 66 | } 67 | }, 68 | @{Name = "ArgumentList"; 69 | Expression = { 70 | ($_.InvocationInfo.Parameters.Item(0) | 71 | Where-Object { $_.name -eq "ArgumentList" }).value 72 | } 73 | }, 74 | @{Name = "Authentication"; 75 | Expression = { 76 | ($_.InvocationInfo.Parameters.Item(0) | 77 | Where-Object { $_.name -eq "Authentication" }).value 78 | } 79 | }, 80 | @{Name = "InitializationScript"; 81 | Expression = { 82 | ($_.InvocationInfo.Parameters.Item(0) | 83 | Where-Object { $_.name -eq "InitializationScript" }).value 84 | } 85 | }, 86 | @{Name = "RunAs32"; 87 | Expression = { 88 | ($_.InvocationInfo.Parameters.Item(0) | 89 | Where-Object { $_.name -eq "RunAs32" }).value 90 | } 91 | }, 92 | @{Name = "Credential"; 93 | Expression = { 94 | $_.Credential.UserName 95 | } 96 | }, 97 | @{Name = "Options"; 98 | Expression = { 99 | #don't export the job definition here 100 | $_.Options | Select-Object -property * -ExcludeProperty JobDefinition 101 | } 102 | }, 103 | @{Name = "JobTriggers"; 104 | Expression = { 105 | #don't export the job definition here 106 | $_.JobTriggers | Select-Object -property * -ExcludeProperty JobDefinition 107 | } 108 | }, ExecutionHistoryLength, Enabled | 109 | Export-Clixml -Path $ExportPath 110 | 111 | if ($Passthru) { 112 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Writing the export file item to the pipeline" 113 | Get-Item -Path $ExportPath 114 | } 115 | 116 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Export finished." 117 | } #process 118 | 119 | End { 120 | Write-Verbose "[$((Get-Date).TimeOfDay) END] Ending $($MyInvocation.MyCommand)" 121 | } #end 122 | } #end Export-ScheduledJob 123 | -------------------------------------------------------------------------------- /functions/Get-ScheduledJobDetail.ps1: -------------------------------------------------------------------------------- 1 | Function Get-ScheduledJobDetail { 2 | [CmdletBinding(DefaultParameterSetName = "name")] 3 | [OutputType("ScheduledJobDetail")] 4 | 5 | Param( 6 | [Parameter(Position = 0, ValueFromPipeline, Mandatory, ParameterSetName = "name")] 7 | [ValidateNotNullOrEmpty()] 8 | [string[]]$Name, 9 | 10 | [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "job")] 11 | [ValidateNotNullOrEmpty()] 12 | [alias("job")] 13 | [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]$ScheduledJob 14 | ) 15 | 16 | Begin { 17 | Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN] Starting $($MyInvocation.MyCommand)" 18 | } #begin 19 | 20 | Process { 21 | 22 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using parameter set $($PSCmdlet.ParameterSetName)" 23 | $jobs = @() 24 | if ($PSCmdlet.ParameterSetName -eq 'name') { 25 | foreach ($item in $name) { 26 | Try { 27 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting ScheduledJob $item" 28 | $jobs += Get-ScheduledJob -Name $item -ErrorAction Stop 29 | } 30 | Catch { 31 | Write-Warning $_.exception.message 32 | } 33 | } 34 | } #if Name 35 | else { 36 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using ScheduledJob $($ScheduledJob.name)" 37 | $jobs += $ScheduledJob 38 | } 39 | 40 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting ScheduledJob details" 41 | foreach ($job in $jobs) { 42 | #get corresponding task 43 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] $($job.name)" 44 | $task = Get-ScheduledTask -TaskName $job.name 45 | $info = $task | Get-ScheduledTaskInfo 46 | [PSCustomObject]@{ 47 | PSTypeName = "ScheduledJobDetail" 48 | ID = $job.ID 49 | Name = $job.name 50 | Command = $job.command 51 | Enabled = $job.enabled 52 | State = $task.State 53 | NextRun = $info.nextRunTime 54 | MaxHistory = $job.ExecutionHistoryLength 55 | RunAs = $task.Principal.UserID 56 | Frequency = $job.JobTriggers.Frequency 57 | Days = $job.JobTriggers.DaysOfWeek 58 | RepetitionDuration = $job.JobTriggers.RepetitionDuration 59 | RepetitionInterval = $job.JobTriggers.RepetitionInterval 60 | DoNotAllowDemandStart = $job.options.DoNotAllowDemandStart 61 | IdleDuration = $job.options.IdleDuration 62 | IdleTimeout = $job.options.IdleTimeout 63 | MultipleInstancePolicy = $job.options.MultipleInstancePolicy 64 | RestartOnIdleResume = $job.options.RestartOnIdleResume 65 | RunElevated = $job.options.RunElevated 66 | RunWithoutNetwork = $job.options.RunWithoutNetwork 67 | ShowInTaskScheduler = $job.options.ShowInTaskScheduler 68 | StartIfNotIdle = $job.options.StartIfNotIdle 69 | StartIfOnBatteries = $job.options.StartIfOnBatteries 70 | StopIfGoingOffIdle = $job.options.StopIfGoingOffIdle 71 | StopIfGoingOnBatteries = $job.options.StopIfGoingOnBatteries 72 | WakeToRun = $job.options.WakeToRun 73 | } 74 | 75 | } #foreach job 76 | 77 | } #process 78 | 79 | End { 80 | Write-Verbose "[$((Get-Date).TimeOfDay) END] Ending $($MyInvocation.MyCommand)" 81 | } #end 82 | } 83 | -------------------------------------------------------------------------------- /functions/Get-ScheduledJobResult.ps1: -------------------------------------------------------------------------------- 1 | Function Get-ScheduledJobResult { 2 | 3 | [cmdletbinding()] 4 | [OutputType("ScheduledJobResult")] 5 | [Alias("ljr")] 6 | 7 | Param( 8 | [Parameter(Position = 0)] 9 | [ValidateNotNullOrEmpty()] 10 | [string]$Name = "*", 11 | [ValidateScript({ $_ -gt 0 })] 12 | [int]$Newest = 1, 13 | [switch]$All 14 | ) 15 | 16 | Begin { 17 | Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN] Starting $($MyInvocation.MyCommand)" 18 | } #begin 19 | 20 | Process { 21 | #only show results for Enabled jobs 22 | Try { 23 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting scheduled jobs for $name" 24 | $jobs = Get-ScheduledJob -Name $name -ErrorAction Stop #-ErrorVariable ev 25 | } 26 | Catch { 27 | Write-Warning "$Name : $($_.exception.message)" 28 | # $ev.errorRecord.Exception 29 | } 30 | 31 | if ($jobs) { 32 | #filter unless asking for all jobs 33 | 34 | if ($All) { 35 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting all jobs" 36 | } 37 | else { 38 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting enabled jobs only" 39 | $jobs = $jobs | Where-Object Enabled 40 | } 41 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Processing $($jobs.count) found jobs" 42 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Getting newest $newest job results" 43 | 44 | $data = $jobs | ForEach-Object { 45 | #get job and select all properties to create a custom object 46 | Try { 47 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Trying to get jobs for $($_.name)" 48 | Get-Job -Name $_.name -Newest $Newest -ErrorAction stop | ForEach-Object { 49 | [ScheduledJobResult]::new($_) 50 | } 51 | } #Try 52 | Catch { 53 | Write-Warning $_.exception.message 54 | Write-Warning "Scheduled job $($_.TargetObject) has not been run yet." 55 | } 56 | } #Foreach Scheduled Job 57 | 58 | #write a sorted result to the pipeline 59 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Here are your $($data.count) results" 60 | $data | Sort-Object -Property PSEndTime -Descending 61 | 62 | } #if $jobs 63 | } #process 64 | End { 65 | Write-Verbose "[$((Get-Date).TimeOfDay) END] Ending $($MyInvocation.MyCommand)" 66 | } #end 67 | 68 | } #end function 69 | -------------------------------------------------------------------------------- /functions/Import-ScheduledJob.ps1: -------------------------------------------------------------------------------- 1 | Function Import-ScheduledJob { 2 | 3 | [cmdletbinding(SupportsShouldProcess)] 4 | [OutputType("None", "Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition")] 5 | [Alias("isj")] 6 | 7 | Param( 8 | [Parameter(Position = 0, Mandatory, ValueFromPipeline)] 9 | [alias("name", "pspath")] 10 | [ValidateScript( { 11 | if (-Not (Test-Path -path $_)) { 12 | Throw "Could not verify the path." 13 | } 14 | else { 15 | $True 16 | } 17 | })] 18 | [string]$Path, 19 | [switch]$Passthru 20 | ) 21 | 22 | Begin { 23 | Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN] Starting $($MyInvocation.MyCommand)" 24 | } #begin 25 | 26 | Process { 27 | 28 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] processing $path" 29 | 30 | #define a hashtable of values for Register-ScheduledJob 31 | $ParamHash = @{ 32 | ErrorAction = "Stop" 33 | } 34 | 35 | $Imported = Import-Clixml -path $Path 36 | 37 | #$cmd = "Register-ScheduledJob -name ""$($Imported.Name)""" 38 | $ParamHash.Add("Name", $Imported.Name) 39 | 40 | if ($Imported.scriptblock) { 41 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Found a scriptblock" 42 | 43 | $ParamHash.Add("ScriptBlock", [scriptblock]::Create($Imported.Scriptblock)) 44 | 45 | } 46 | elseif ($Imported.FilePath) { 47 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Found a filepath" 48 | #$cmd+= " -FilePath $($Imported.FilePath)" 49 | $ParamHash.Add("FilePath", $Imported.Filepath) 50 | } 51 | else { 52 | #this should never really happen 53 | Write-Warning "Failed to find a scriptblock or file path" 54 | #bail out 55 | Return 56 | } 57 | 58 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Processing additional job elements" 59 | if ($Imported.ArgumentList) { 60 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] ..ArgumentList" 61 | 62 | $ParamHash.Add("ArgumentList", $ImportFile.ArgumentList) 63 | } 64 | 65 | if ($Imported.Authentication) { 66 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] ..Authentication" 67 | 68 | $ParamHash.Add("Authentication", $Imported.Authentication) 69 | } 70 | 71 | if ($Imported.Credential) { 72 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] ..Credential" 73 | 74 | $ParamHash.Add("credential", $Imported.Credential) 75 | } 76 | 77 | if ($Imported.InitializationScript) { 78 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] ..InitializationScript" 79 | 80 | $ParamHash.Add("InitializationScript", [scriptblock]::Create($Imported.InitializationScript)) 81 | 82 | } 83 | 84 | if ($Imported.ExecutionHistoryLength) { 85 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] ..ExecutionHistoryLength" 86 | 87 | $ParamHash.Add("MaxResultCount", $Imported.ExecutionHistoryLength) 88 | } 89 | 90 | if ($Imported.RunAs32) { 91 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] ..RunAs32" 92 | 93 | $ParamHash.Add("RunAs32", $True) 94 | } 95 | 96 | if ($Imported.Options) { 97 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Processing Options" 98 | 99 | $optionHash = @{ 100 | RunElevated = $Imported.Options.RunElevated 101 | HideInTaskScheduler = -Not ($Imported.Options.ShowInTaskScheduler) 102 | RestartOnIdleResume = $Imported.Options.RestartOnIdleResume 103 | MultipleInstancePolicy = $Imported.Options.MultipleInstancePolicy 104 | DoNotAllowDemandStart = $Imported.Options.DoNotAllowDemandStart 105 | RequireNetwork = -Not ($Imported.Options.RunWithoutNetwork) 106 | StopIfGoingOffIdle = $Imported.Options.StopIfGoingOffIdle 107 | WakeToRun = $Imported.Options.WakeToRun 108 | ContinueIfGoingOnBattery = -Not ($Imported.Options.StopIfGoingOnBatteries) 109 | StartIfOnBattery = $Imported.Options.StartIfOnBatteries 110 | IdleTimeout = $Imported.Options.IdleTimeout 111 | IdleDuration = $Imported.Options.IdleDuration 112 | StartIfIdle = -Not ($Imported.Options.StartIfNotIdle) 113 | } 114 | 115 | $jobOption = New-ScheduledJobOption @optionHash 116 | 117 | $ParamHash.Add("ScheduledJobOption", $jobOption) 118 | } 119 | 120 | if ($Imported.JobTriggers) { 121 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Processing Job Triggers" 122 | 123 | #define an array to hold triggers 124 | $Triggers = @() 125 | 126 | #enumerate each job trigger 127 | foreach ($trigger in $Imported.JobTriggers) { 128 | #test for parameter set 129 | Switch ($trigger.Frequency.Value) { 130 | "Once" { 131 | $TriggerCmd = "New-JobTrigger -Once -At ""$($trigger.At)""" 132 | if ($trigger.RepetitionDuration) { 133 | $TriggerCmd += " -RepetitionDuration $($trigger.RepetitionDuration)" 134 | } 135 | if ($trigger.RepetitionInterval) { 136 | $TriggerCmd += "-RepetitionInterval $($trigger.RepetitionInterval)" 137 | } 138 | } #once 139 | "Weekly" { 140 | $TriggerCmd = "New-JobTrigger -Weekly -DaysOfWeek $($trigger.DaysOfWeek) -WeeksInterval $($trigger.Interval) -At ""$($trigger.At)""" 141 | } #weekly 142 | "Daily" { 143 | $TriggerCmd = "New-JobTrigger -Daily -DaysInterval $($trigger.Interval) -At ""$($trigger.At)""" 144 | } #Daily 145 | "AtLogon" { 146 | $TriggerCmd = "New-JobTrigger -AtLogOn -User $($trigger.user)" 147 | } #AtLogon 148 | "AtStartup" { 149 | $TriggerCmd = "New-JobTrigger -AtStartup" 150 | } #AtStartup 151 | Default { 152 | #no frequency 153 | Write-Warning "No trigger found" 154 | } #Default 155 | } #end switch 156 | 157 | #define a new trigger and add to the array 158 | $TriggerCmd += " -randomdelay $($trigger.RandomDelay)" 159 | Write-Verbose $triggerCmd 160 | $Triggers += Invoke-Expression -Command $TriggerCmd 161 | } #foreach trigger 162 | 163 | 164 | $ParamHash.Add("Trigger", $Triggers) 165 | } 166 | 167 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Creating the scheduled job" 168 | Write-Verbose ($ParamHash | Out-String) 169 | #create the scheduled job 170 | if ($PSCmdlet.ShouldProcess($imported.Name)) { 171 | $newJob = Register-ScheduledJob @ParamHash 172 | 173 | #if the job is disabled, then 174 | if (-Not ($Imported.Enabled)) { 175 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Disabling the job" 176 | $newJob | Disable-ScheduledJob 177 | } 178 | if ($Passthru) { 179 | Get-ScheduledJob $imported.Name 180 | } 181 | } #should process 182 | } #process 183 | 184 | End { 185 | Write-Verbose "[$((Get-Date).TimeOfDay) END] Ending $($MyInvocation.MyCommand)" 186 | } #end 187 | } #close Import-ScheduledJob 188 | -------------------------------------------------------------------------------- /functions/Remove-OldJobResult.ps1: -------------------------------------------------------------------------------- 1 | Function Remove-OldJobResult { 2 | [cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = "name")] 3 | [OutputType("None")] 4 | 5 | Param( 6 | [Parameter( 7 | Position = 0, 8 | Mandatory, 9 | ValueFromPipeline, 10 | ParameterSetName = "name" 11 | )] 12 | [ValidateNotNullOrEmpty()] 13 | [string[]]$Name, 14 | 15 | [Parameter( 16 | Position = 0, 17 | Mandatory, 18 | ValueFromPipeline, 19 | ParameterSetName = "job" 20 | )] 21 | [ValidateNotNullOrEmpty()] 22 | [alias("job")] 23 | [Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition]$ScheduledJob 24 | ) 25 | 26 | Begin { 27 | Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN] Starting $($MyInvocation.MyCommand)" 28 | } #begin 29 | 30 | Process { 31 | #get all but newest job result for cleanup 32 | if ($PSCmdlet.ParameterSetName -eq 'name') { 33 | $items = $Name 34 | } 35 | else { 36 | $items = $ScheduledJob.Name 37 | } 38 | Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Removing old job results for $($items -join ',')" 39 | Get-Job -name $items | Sort-Object PSEndTime -descending | Select-Object -skip 1 | 40 | Remove-Job 41 | } #process 42 | 43 | End { 44 | Write-Verbose "[$((Get-Date).TimeOfDay) END] Ending $($MyInvocation.MyCommand)" 45 | } #end 46 | } 47 | -------------------------------------------------------------------------------- /types/ScheduledJobDefinition.types.ps1xml: -------------------------------------------------------------------------------- 1 |  2 | 11 | 12 | 13 | Microsoft.PowerShell.ScheduledJob.ScheduledJobDefinition 14 | 15 | 16 | RunInfo 17 | 18 | Name 19 | NextRun 20 | LastRun 21 | Enabled 22 | 23 | 24 | 25 | NextRun 26 | (Get-ScheduledTask -TaskName $this.name | Get-ScheduledTaskInfo).nextRunTime 27 | 28 | 29 | LastRun 30 | (Get-job $This.Name -Newest 1).PSEndTime 31 | 32 | 33 | 34 | --------------------------------------------------------------------------------