-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathConvertFrom-ISO8601Duration.ps1
56 lines (42 loc) · 1.41 KB
/
ConvertFrom-ISO8601Duration.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
Function ConvertFrom-ISO8601Duration {
<#
.SYNOPSIS
Convert an ISO8601 duration into a human-readable string
.DESCRIPTION
Convert an ISO8601 duration into a human-readable string
.PARAMETER InputValue
String ISO8601 value
.EXAMPLE
ConvertFrom-ISO8601Duration -InputValue 'P2DT12H30S'
Returns a time span object
.EXAMPLE
ConvertFrom-ISO8601Duration -InputValue 'PT2H3M4S' -AsString
Returns '2 hours 3 minutes 4 seconds'
.NOTES
For additional information please see my GitHub wiki page
.LINK
https://github.com/My-Random-Thoughts
#>
Param (
[Parameter(Mandatory = $true, ValueFromPipeline)]
[string]$InputValue,
[switch]$AsString
)
Begin {
[string] $return = ''
[string[]]$properties = @('days', 'hours', 'minutes', 'seconds')
}
Process {
[timespan]$timeSpan = ([System.Xml.XmlConvert]::ToTimeSpan($InputValue))
If (-not $AsString.IsPresent) {
Return $timeSpan
}
ForEach ($prop In $properties) {
If ($timeSpan.$prop -eq 1) { $return += "$($timeSpan.$prop) $($prop.TrimEnd('s')), " }
If ($timeSpan.$prop -gt 1) { $return += "$($timeSpan.$prop) $($prop), " }
}
Return $return.TrimEnd(', ')
}
End {
}
}