The question was this:
This is just a quick/fast snippet in response to that question, but I can think of a ton of enhancements that could made. Hopefully it will be useful as a starting point for someone to customize for their own use.
function Convert-FriendlyTime
{
param (
[string]$FriendlyTime
)
$Converted = $FriendlyTime.Split(" ")
switch ($Converted[1])
{
"days" { $parms = @{"days"=$Converted[0]}; break ;}
"hours" { $parms = @{"hours"=$Converted[0]}; break ;}
"minutes" { $parms = @{"minutes"=$Converted[0]}; break ;}
"seconds" { $parms = @{"seconds"=$Converted[0]}; break ;}
"default" { $parms = $null; break ;}
}
if ($parms) {
New-TimeSpan @parms
} else {
Throw "Invalid unit of time '$FriendlyTime'. Please use days, hours, minutes or seconds."
}
}
Convert-FriendlyTime -FriendlyTime "2 days"
Convert-FriendlyTime -FriendlyTime "68 hours"
Convert-FriendlyTime -FriendlyTime "68 InvalidUnits"
See also
- SQL Saturday 723, March 24, 2018 in Rochester, NY. Join us and kick your SQL Server knowledge up a notch!
- SQL Saturday 622. Join me July 29, 2017 in Albany, NY and learn to Paint with PowerShell!
- SQL Saturday 619. Join me April 29, 2017 in Rochester, NY and learn to Paint with PowerShell!
- Convert User Friendly Retention to DateTime value with PowerShell
- Speaking at SQL Saturday 513 in Albany, NY on July 30th - PowerShell and SQL Server