forked from CodyMathis123/CM-Ramblings
-
Notifications
You must be signed in to change notification settings - Fork 1
/
New-LoopAction.ps1
183 lines (183 loc) · 7.85 KB
/
New-LoopAction.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function New-LoopAction {
<#
.SYNOPSIS
Function to loop a specified scriptblock until certain conditions are met
.DESCRIPTION
This function is a wrapper for a ForLoop or a DoUntil loop. This allows you to specify if you want to exit based on a timeout, or a number of iterations.
Additionally, you can specify an optional delay between loops, and the type of dealy (Minutes, Seconds). If needed, you can also perform an action based on
whether the 'Exit Condition' was met or not. This is the IfTimeoutScript and IfSucceedScript.
.PARAMETER LoopTimeout
A time interval integer which the loop should timeout after. This is for a DoUntil loop.
.PARAMETER LoopTimeoutType
Provides the time increment type for the LoopTimeout, defaulting to Seconds. ('Seconds', 'Minutes', 'Hours', 'Days')
.PARAMETER LoopDelay
An optional delay that will occur between each loop.
.PARAMETER LoopDelayType
Provides the time increment type for the LoopDelay between loops, defaulting to Seconds. ('Milliseconds', 'Seconds', 'Minutes')
.PARAMETER Iterations
Implies that a ForLoop is wanted. This will provide the maximum number of Iterations for the loop. [i.e. "for ($i = 0; $i -lt $Iterations; $i++)..."]
.PARAMETER ScriptBlock
A script block that will run inside the loop. Recommend encapsulating inside { } or providing a [scriptblock]
.PARAMETER ExitCondition
A script block that will act as the exit condition for the do-until loop. Will be evaluated each loop. Recommend encapsulating inside { } or providing a [scriptblock]
.PARAMETER IfTimeoutScript
A script block that will act as the script to run if the timeout occurs. Recommend encapsulating inside { } or providing a [scriptblock]
.PARAMETER IfSucceedScript
A script block that will act as the script to run if the exit condition is met. Recommend encapsulating inside { } or providing a [scriptblock]
.EXAMPLE
C:\PS> $newLoopActionSplat = @{
LoopTimeoutType = 'Seconds'
ScriptBlock = { 'Bacon' }
ExitCondition = { 'Bacon' -Eq 'eggs' }
IfTimeoutScript = { 'Breakfast'}
LoopDelayType = 'Seconds'
LoopDelay = 1
LoopTimeout = 10
}
New-LoopAction @newLoopActionSplat
Bacon
Bacon
Bacon
Bacon
Bacon
Bacon
Bacon
Bacon
Bacon
Bacon
Bacon
Breakfast
.EXAMPLE
C:\PS> $newLoopActionSplat = @{
ScriptBlock = { if($Test -eq $null){$Test = 0};$TEST++ }
ExitCondition = { $Test -eq 4 }
IfTimeoutScript = { 'Breakfast' }
IfSucceedScript = { 'Dinner'}
Iterations = 5
LoopDelay = 1
}
New-LoopAction @newLoopActionSplat
Dinner
C:\PS> $newLoopActionSplat = @{
ScriptBlock = { if($Test -eq $null){$Test = 0};$TEST++ }
ExitCondition = { $Test -eq 6 }
IfTimeoutScript = { 'Breakfast' }
IfSucceedScript = { 'Dinner'}
Iterations = 5
LoopDelay = 1
}
New-LoopAction @newLoopActionSplat
Breakfast
.NOTES
Play with the conditions a bit. I've tried to provide some examples that demonstrate how the loops, timeouts, and scripts work!
#>
param
(
[parameter(Mandatory = $true, ParameterSetName = 'DoUntil')]
[int32]$LoopTimeout,
[parameter(Mandatory = $true, ParameterSetName = 'DoUntil')]
[ValidateSet('Seconds', 'Minutes', 'Hours', 'Days')]
[string]$LoopTimeoutType,
[parameter(Mandatory = $true)]
[int32]$LoopDelay,
[parameter(Mandatory = $false, ParameterSetName = 'DoUntil')]
[ValidateSet('Milliseconds', 'Seconds', 'Minutes')]
[string]$LoopDelayType = 'Seconds',
[parameter(Mandatory = $true, ParameterSetName = 'ForLoop')]
[int32]$Iterations,
[parameter(Mandatory = $true)]
[scriptblock]$ScriptBlock,
[parameter(Mandatory = $true, ParameterSetName = 'DoUntil')]
[parameter(Mandatory = $false, ParameterSetName = 'ForLoop')]
[scriptblock]$ExitCondition,
[parameter(Mandatory = $false)]
[scriptblock]$IfTimeoutScript,
[parameter(Mandatory = $false)]
[scriptblock]$IfSucceedScript
)
begin {
switch ($PSCmdlet.ParameterSetName) {
'DoUntil' {
$paramNewTimeSpan = @{
$LoopTimeoutType = $LoopTimeout
}
$TimeSpan = New-TimeSpan @paramNewTimeSpan
$StopWatch = [System.Diagnostics.Stopwatch]::StartNew()
$FirstRunDone = $false
}
}
}
process {
switch ($PSCmdlet.ParameterSetName) {
'DoUntil' {
do {
switch ($FirstRunDone) {
$false {
$FirstRunDone = $true
}
Default {
$paramStartSleep = @{
$LoopDelayType = $LoopDelay
}
Start-Sleep @paramStartSleep
}
}
. $ScriptBlock
}
until ((. $ExitCondition) -or $StopWatch.Elapsed -ge $TimeSpan)
}
'ForLoop' {
for ($i = 0; $i -lt $Iterations; $i++) {
switch ($FirstRunDone) {
$false {
$FirstRunDone = $true
}
Default {
$paramStartSleep = @{
$LoopDelayType = $LoopDelay
}
Start-Sleep @paramStartSleep
}
}
. $ScriptBlock
if ($PSBoundParameters.ContainsKey('ExitCondition')) {
if (. $ExitCondition) {
break
}
}
}
}
}
}
end {
switch ($PSCmdlet.ParameterSetName) {
'DoUntil' {
if ((-not (. $ExitCondition)) -and $StopWatch.Elapsed -ge $TimeSpan -and $PSBoundParameters.ContainsKey('IfTimeoutScript')) {
. $IfTimeoutScript
}
if ((. $ExitCondition) -and $PSBoundParameters.ContainsKey('IfSucceedScript')) {
. $IfSucceedScript
}
$StopWatch.Reset()
}
'ForLoop' {
if ($PSBoundParameters.ContainsKey('ExitCondition')) {
if ((-not (. $ExitCondition)) -and $i -ge $Iterations -and $PSBoundParameters.ContainsKey('IfTimeoutScript')) {
. $IfTimeoutScript
}
elseif ((. $ExitCondition) -and $PSBoundParameters.ContainsKey('IfSucceedScript')) {
. $IfSucceedScript
}
}
else {
if ($i -ge $Iterations -and $PSBoundParameters.ContainsKey('IfTimeoutScript')) {
. $IfTimeoutScript
}
elseif ($i -lt $Iterations -and $PSBoundParameters.ContainsKey('IfSucceedScript')) {
. $IfSucceedScript
}
}
}
}
}
}