-
Notifications
You must be signed in to change notification settings - Fork 3
/
Get-ElapsedTime.ps1
183 lines (156 loc) · 4.89 KB
/
Get-ElapsedTime.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 Get-ElapsedTime
{
<#
.SYNOPSIS
Will return information about elapsed time for the given StopWatch.
.DESCRIPTION
Function requires a [System.Diagnostics.Stopwatch] object as input and will output information about elapsed time.
By default a [TimeSpan] object is returned containing all information about elapsed time.
If any other parameter like -Days is used function will return an Int or Double instead depending on the switch used.
.PARAMETER ElapsedTime
Will return a [TimeSpan] object representing the elapsed time for the given stopwatch.
.PARAMETER Days
Will return an [Int] object representing the number of days since the stopwatch was started.
.PARAMETER Hours
Will return an [Int] object representing the number of hours since the stopwatch was started.
.PARAMETER Minutes
Will return an [Int] object representing the number of minutes since the stopwatch was started.
.PARAMETER Seconds
Will return an [Int] object representing the number of seconds since the stopwatch was started.
.PARAMETER TotalDays
Will return a [Double] object representing the number of TotalDays since the stopwatch was started.
.PARAMETER TotalHours
Will return a [Double] object representing the number of TotalHours since the stopwatch was started.
.PARAMETER TotalMinutes
Will return a [Double] object representing the number of TotalMinutes since the stopwatch was started.
.PARAMETER TotalSeconds
Will return a [Double] object representing the number of TotalSeconds since the stopwatch was started.
.PARAMETER TotalMilliseconds
Will return a [Double] object representing the number of TotalMilliseconds since the stopwatch was started.
.EXAMPLE
PS C:\> Get-ElapsedTime -ElapsedTime $ElapsedTime -Days
.OUTPUTS
System.TimeSpan, System.Double, System.Int32
#>
[CmdletBinding(DefaultParameterSetName = 'FullOutput',
ConfirmImpact = 'High',
SupportsPaging = $false,
SupportsShouldProcess = $false)]
[OutputType([timespan], ParameterSetName = 'FullOutput')]
[OutputType([int], ParameterSetName = 'Days')]
[OutputType([int], ParameterSetName = 'Hours')]
[OutputType([int], ParameterSetName = 'Minutes')]
[OutputType([int], ParameterSetName = 'Seconds')]
[OutputType([double], ParameterSetName = 'TotalDays')]
[OutputType([double], ParameterSetName = 'TotalHours')]
[OutputType([double], ParameterSetName = 'TotalMinutes')]
[OutputType([double], ParameterSetName = 'TotalSeconds')]
[OutputType([double], ParameterSetName = 'TotalMilliseconds')]
[OutputType([timespan])]
param
(
[Parameter(ParameterSetName = 'FullOutput',
Mandatory = $true)]
[Parameter(ParameterSetName = 'Days')]
[Parameter(ParameterSetName = 'Hours')]
[Parameter(ParameterSetName = 'Minutes')]
[Parameter(ParameterSetName = 'Seconds')]
[Parameter(ParameterSetName = 'TotalDays')]
[Parameter(ParameterSetName = 'TotalHours')]
[Parameter(ParameterSetName = 'TotalMilliseconds')]
[Parameter(ParameterSetName = 'TotalMinutes')]
[Parameter(ParameterSetName = 'TotalSeconds')]
[System.Diagnostics.Stopwatch]
$ElapsedTime,
[Parameter(ParameterSetName = 'Days')]
[switch]
$Days,
[Parameter(ParameterSetName = 'Hours')]
[switch]
$Hours,
[Parameter(ParameterSetName = 'Minutes')]
[switch]
$Minutes,
[Parameter(ParameterSetName = 'Seconds')]
[switch]
$Seconds,
[Parameter(ParameterSetName = 'TotalDays')]
[switch]
$TotalDays,
[Parameter(ParameterSetName = 'TotalHours')]
[switch]
$TotalHours,
[Parameter(ParameterSetName = 'TotalMinutes')]
[switch]
$TotalMinutes,
[Parameter(ParameterSetName = 'TotalSeconds')]
[switch]
$TotalSeconds,
[Parameter(ParameterSetName = 'TotalMilliseconds')]
[switch]
$TotalMilliseconds
)
switch ($PsCmdlet.ParameterSetName)
{
'FullOutput'
{
# Return full timespan object
return $ElapsedTime.Elapsed
break
}
'Days'
{
# Return days with no decimals
return $ElapsedTime.Elapsed.Days
break
}
'Hours'
{
# Return hours with no decimals
return $ElapsedTime.Elapsed.Hours
break
}
'Minutes'
{
# Return minutes with no decimals
return $ElapsedTime.Elapsed.Minutes
break
}
'Seconds'
{
# Return seconds with no decimals
return $ElapsedTime.Elapsed.Seconds
break
}
'TotalDays'
{
# Return days with double precision
return $ElapsedTime.Elapsed.TotalDays
break
}
'TotalHours'
{
# Return hours with double precision
return $ElapsedTime.Elapsed.TotalHours
break
}
'TotalMinutes'
{
# Return minutes with double precision
return $ElapsedTime.Elapsed.TotalMinutes
break
}
'TotalSeconds'
{
# Return seconds with double precision
return $ElapsedTime.Elapsed.TotalSeconds
break
}
'TotalMilliseconds'
{
# Return milliseconds with double precision
return $ElapsedTime.Elapsed.TotalMilliseconds
break
}
}
}