Sample code for blog post A handy Powershell filter for converting plain text to objects
Last active
August 29, 2015 14:21
-
-
Save latkin/1b849d8832babf38e4e9 to your computer and use it in GitHub Desktop.
Blog: A handy Powershell filter for converting plain text to objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# converts text to objects via regex, | |
# with properties corresponding to capture groups | |
filter ro | |
{ | |
param($pattern) | |
if($_ -match $pattern) | |
{ | |
$result = @{} | |
$matches.Keys |?{ $_ } |%{ | |
$raw = $matches[$_] | |
$asInt = 0 | |
$asFloat = 0.0 | |
$asDate = [datetime]::Now | |
if([int]::TryParse($raw, [ref] $asInt)){ $result[$_] = $asInt } | |
elseif([double]::TryParse($raw, [ref] $asFloat)){ $result[$_] = $asFloat } | |
elseif([datetime]::TryParse($raw, [ref] $asDate)){ $result[$_] = $asDate } | |
else{ $result[$_] = $raw } | |
} | |
[pscustomobject]$result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment