<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Scheduling_Within_a_PowerShell_Script</id>
	<title>Scheduling Within a PowerShell Script - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://vwiki.co.uk/index.php?action=history&amp;feed=atom&amp;title=Scheduling_Within_a_PowerShell_Script"/>
	<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Scheduling_Within_a_PowerShell_Script&amp;action=history"/>
	<updated>2026-05-20T08:30:23Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>http://vwiki.co.uk/index.php?title=Scheduling_Within_a_PowerShell_Script&amp;diff=2208&amp;oldid=prev</id>
		<title>Sstrutt: Typo fix</title>
		<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Scheduling_Within_a_PowerShell_Script&amp;diff=2208&amp;oldid=prev"/>
		<updated>2012-11-18T00:00:47Z</updated>

		<summary type="html">&lt;p&gt;Typo fix&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en-GB&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 00:00, 18 November 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Script to &lt;/del&gt;run a function at a specific interval throughout the day&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;This script &lt;/ins&gt;run&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&#039;s &lt;/ins&gt;a function at a specific interval throughout the day&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;.  The script runs as a continuous task and is a simple scheduling engine.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;$start = Get-Date&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;$start = Get-Date&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l23&quot;&gt;Line 23:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 23:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;function Log-Perf {&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;function Log-Perf {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     # Keeps track of script resource usage&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     # Keeps track of script resource usage&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     Log (&quot;Perf CPU(sec): &quot; + [Math]::Round($proc.cpu, 0) + &quot;, Paged Mem (MB): &quot; + [Math]::Round(($proc.pm/1024), 0) + &quot;, WrkSet Mem (MB): &quot; +  Math]::Round(($proc.ws/1024), 0))&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     Log (&quot;Perf CPU(sec): &quot; + [Math]::Round($proc.cpu, 0) + &quot;, Paged Mem (MB): &quot; + [Math]::Round(($proc.pm/1024), 0) + &quot;, WrkSet Mem (MB): &quot; +  &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[&lt;/ins&gt;Math]::Round(($proc.ws/1024), 0))&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;}&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Sstrutt</name></author>
	</entry>
	<entry>
		<id>http://vwiki.co.uk/index.php?title=Scheduling_Within_a_PowerShell_Script&amp;diff=1829&amp;oldid=prev</id>
		<title>Sstrutt: Initial creation - content from PowerShell Examples page</title>
		<link rel="alternate" type="text/html" href="http://vwiki.co.uk/index.php?title=Scheduling_Within_a_PowerShell_Script&amp;diff=1829&amp;oldid=prev"/>
		<updated>2012-04-18T13:30:51Z</updated>

		<summary type="html">&lt;p&gt;Initial creation - content from PowerShell Examples page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Script to run a function at a specific interval throughout the day&lt;br /&gt;
&amp;lt;source lang=&amp;quot;powershell&amp;quot;&amp;gt;&lt;br /&gt;
$start = Get-Date&lt;br /&gt;
&lt;br /&gt;
# Scheduler stuff...&lt;br /&gt;
$DoSingleRun = 0            # Ignore scheduling and just run once (useful for debugging main function)&lt;br /&gt;
$IntervalMins = 15          # Should fit into an hour exactly&lt;br /&gt;
$End = &amp;quot;18:50&amp;quot;              # Time of day that script should cease (should be just after last required run time)&lt;br /&gt;
$CheckThrottle = 250        # Idle throttle / time check interval (msec)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Scheduled and helper functions ================================================================&lt;br /&gt;
&lt;br /&gt;
function Log ($text) {&lt;br /&gt;
    $stamp = (Get-Date).ToString(&amp;quot;HH:mm:ss.fff&amp;quot;)&lt;br /&gt;
    Write-Host &amp;quot;$stamp | $text&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function Do-Stuff {&lt;br /&gt;
    # This is where the business of the script goes&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function Log-Perf {&lt;br /&gt;
    # Keeps track of script resource usage&lt;br /&gt;
    Log (&amp;quot;Perf CPU(sec): &amp;quot; + [Math]::Round($proc.cpu, 0) + &amp;quot;, Paged Mem (MB): &amp;quot; + [Math]::Round(($proc.pm/1024), 0) + &amp;quot;, WrkSet Mem (MB): &amp;quot; +  Math]::Round(($proc.ws/1024), 0))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# MAIN SCRIPT ====================================================================================&lt;br /&gt;
&lt;br /&gt;
# Initial prep -----------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
if ($DoSingleRun) {&lt;br /&gt;
    Start-Transcript -Path check-test.log -Append&lt;br /&gt;
} else {&lt;br /&gt;
    Rename-Item -Path check.log -NewName check-1.log -Force&lt;br /&gt;
    Start-Transcript -Path check.log -Append&lt;br /&gt;
}&lt;br /&gt;
Log &amp;quot;Started script run at $start&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Get process for this script&lt;br /&gt;
$proc = [System.Diagnostics.Process]::GetCurrentProcess()&lt;br /&gt;
&lt;br /&gt;
# Check scheduler variables&lt;br /&gt;
if ((60 % $IntervalMins) -ne 0) {&lt;br /&gt;
    Log &amp;quot;Interval error - $IntervalMins mins doesn&amp;#039;t fit into an hour!&amp;quot;&lt;br /&gt;
    Exit&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
try {&lt;br /&gt;
    $EndTime = Get-Date $End&lt;br /&gt;
} catch {&lt;br /&gt;
    Log &amp;quot;Invalid end time: $End hrs&amp;quot;&lt;br /&gt;
    Log $_.Exception.GetType().FullName&lt;br /&gt;
    Log $_.Exception.Message&lt;br /&gt;
    Exit&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# Set dummy last run time (aligned to nice start time), and next run time&lt;br /&gt;
$LastRunTime = Get-Date&lt;br /&gt;
$offset = ($IntervalMins + ($LastRunTime.Minute % $IntervalMins))&lt;br /&gt;
$LastRunTime = $LastRunTime.AddMinutes(-$offset)&lt;br /&gt;
$LastRunTime = $LastRunTime.AddSeconds(-$LastRunTime.Second)&lt;br /&gt;
$NextRunTime = $LastRunTime.AddMinutes($IntervalMins)&lt;br /&gt;
&lt;br /&gt;
#Write-Host &amp;quot;Offset is       &amp;quot; $offset&lt;br /&gt;
Log (&amp;quot;Last runtime is &amp;quot; + $LastRunTime)&lt;br /&gt;
Log (&amp;quot;Next runtime is &amp;quot; + $NextRunTime)&lt;br /&gt;
Log (&amp;quot;Script cease at &amp;quot; + $EndTime)&lt;br /&gt;
&lt;br /&gt;
# Do any once-off initialisation here, &lt;br /&gt;
#  eg connect to a MySQL database, open an &amp;quot;I&amp;#039;m alive&amp;quot; TCP port, load credentials&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Main loop -------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
if ($DoSingleRun) {&lt;br /&gt;
    Log (&amp;quot;Doing single run...&amp;quot;)&lt;br /&gt;
    Do-Stuff&lt;br /&gt;
} else {&lt;br /&gt;
    While (1) {&lt;br /&gt;
        if ($NextRunTime -lt (Get-Date)) {&lt;br /&gt;
            Log-Perf&lt;br /&gt;
            Log (&amp;quot;Starting run at &amp;quot; + (Get-Date))&lt;br /&gt;
            Do-Stuff&lt;br /&gt;
            Log (&amp;quot;Completed run at &amp;quot; + (Get-Date))&lt;br /&gt;
            $LastRunTime = $LastRunTime.AddMinutes($IntervalMins)&lt;br /&gt;
            $NextRunTime = $LastRunTime.AddMinutes($IntervalMins)&lt;br /&gt;
            if ($NextRunTime -lt (Get-Date)) {&lt;br /&gt;
                Log &amp;quot;WARNING: Next run is going to be late!&amp;quot;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        if ($EndTime -lt (Get-Date)) {&lt;br /&gt;
            Log (&amp;quot;Script ending at &amp;quot; + (Get-Date))&lt;br /&gt;
            Break&lt;br /&gt;
        }&lt;br /&gt;
        Start-Sleep -Milliseconds $CheckThrottle&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# Do any final once-off completion stuff here&lt;br /&gt;
# - EG disconnect from database, send &amp;quot;I&amp;#039;m finished&amp;quot; email&lt;br /&gt;
&lt;br /&gt;
Stop-Transcript&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:PowerShell]]&lt;/div&gt;</summary>
		<author><name>Sstrutt</name></author>
	</entry>
</feed>