Last active
April 27, 2021 11:19
-
-
Save JohnLBevan/d2adbe1d5f1f453ac046d505046ef1b2 to your computer and use it in GitHub Desktop.
A helpful cmdlet for converting a file containing a bunch of things of a certain type to that type, whilst handling comments and whitespace in the file easily.
This file contains hidden or 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
Function Import-CommentedListFile { | |
[CmdletBinding()] | |
Param ( | |
[Parameter(Mandatory)] | |
[ValidateScript({(Test-Path -Path $_) -or (&{throw "Path does not exist: '$_'"})})] | |
[string]$Path | |
, | |
[Parameter()] | |
[string]$Encoding = 'UTF8' | |
, | |
[Parameter()] | |
[Type]$Type = ([string]) | |
) | |
Get-Content -Path $Path -Encoding $Encoding | | |
ForEach-Object {$_ -replace '#.*$',''} | | |
ForEach-Object {$_.Trim()} | | |
Where-Object {$_} | | |
ForEach-Object { | |
$converted = $_ -as $Type | |
if ($null -eq $converted) { | |
Write-Warning "Cound not convert '$_' to '$($type.FullName)'" | |
} else { | |
$converted | |
} | |
} | |
} |
This file contains hidden or 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
# | |
# Notes on this file's format: | |
# Whitespace is ignored | |
# Anything following a hash on the same line is ignored / seen as a comment | |
# Anything that doesn't cast to a GUID results in a warning, and is skipped | |
# Any valid GUIDs are returned | |
# | |
9b0ae2db-468c-1b11-baf2-38eae400de2d # this guid is just for demo purposes; but you can see how this may be useful? | |
9b0ae2db-468c-2b11-baf2-38eae400de2e | |
9b0ae2db-468c-3b11-baf2-38eae400de2f | |
9b0ae2db-468c-4b11-baf2-38eae400de2g | |
9b0ae2db-468c-5b11-baf2-38eae400de2h # ooh, this one's wrong, as was the previous row... will we catch it | |
9b0ae2db-468c-6b11-baf2-38eae400de3a | |
9b0ae2db-468c-7b11-baf2-38eae400de3b | |
37 | |
3879516 # some numbers so you can try [int] instead of [guid] | |
generic text | |
# that's all folks | |
This file contains hidden or 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
> Import-CommentedListFile -Path '.\SampleData.txt' | |
9b0ae2db-468c-1b11-baf2-38eae400de2d | |
9b0ae2db-468c-2b11-baf2-38eae400de2e | |
9b0ae2db-468c-3b11-baf2-38eae400de2f | |
9b0ae2db-468c-4b11-baf2-38eae400de2g | |
9b0ae2db-468c-5b11-baf2-38eae400de2h | |
9b0ae2db-468c-6b11-baf2-38eae400de3a | |
9b0ae2db-468c-7b11-baf2-38eae400de3b | |
37 | |
3879516 | |
generic text | |
> Import-CommentedListFile -Path '.\SampleData.txt' -Type ([guid]) | |
WARNING: Cound not convert '9b0ae2db-468c-4b11-baf2-38eae400de2g' to 'System.Guid' | |
WARNING: Cound not convert '9b0ae2db-468c-5b11-baf2-38eae400de2h' to 'System.Guid' | |
WARNING: Cound not convert '37' to 'System.Guid' | |
WARNING: Cound not convert '3879516' to 'System.Guid' | |
WARNING: Cound not convert 'generic text' to 'System.Guid' | |
Guid | |
---- | |
9b0ae2db-468c-1b11-baf2-38eae400de2d | |
9b0ae2db-468c-2b11-baf2-38eae400de2e | |
9b0ae2db-468c-3b11-baf2-38eae400de2f | |
9b0ae2db-468c-6b11-baf2-38eae400de3a | |
9b0ae2db-468c-7b11-baf2-38eae400de3b | |
> Import-CommentedListFile -Path '.\SampleData.txt' -Type ([int]) | |
WARNING: Cound not convert '9b0ae2db-468c-1b11-baf2-38eae400de2d' to 'System.Int32' | |
WARNING: Cound not convert '9b0ae2db-468c-2b11-baf2-38eae400de2e' to 'System.Int32' | |
WARNING: Cound not convert '9b0ae2db-468c-3b11-baf2-38eae400de2f' to 'System.Int32' | |
WARNING: Cound not convert '9b0ae2db-468c-4b11-baf2-38eae400de2g' to 'System.Int32' | |
WARNING: Cound not convert '9b0ae2db-468c-5b11-baf2-38eae400de2h' to 'System.Int32' | |
WARNING: Cound not convert '9b0ae2db-468c-6b11-baf2-38eae400de3a' to 'System.Int32' | |
WARNING: Cound not convert '9b0ae2db-468c-7b11-baf2-38eae400de3b' to 'System.Int32' | |
37 | |
3879516 | |
WARNING: Cound not convert 'generic text' to 'System.Int32' | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment