Functions/Test-IsNumeric.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
Function Test-IsNumeric {
<#
.SYNOPSIS
    Determines if specified string can be parsed to a number
.DESCRIPTION
    Determines if specified string can be parsed to a number. Can specify a string, an array of strings, or input from the pipeline
.PARAMETER NumString
    A string or string array to test to determine if they are numeric. Aliased as 'Number'
.PARAMETER IncludeInput
    Switch to include the input parameters in the output
.EXAMPLE
    Test-IsNumeric '1.2'
 
    True
.EXAMPLE
    Test-IsCapsLock -Verbose
#>


    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','')]
    [CmdletBinding(ConfirmImpact='None')]
    [OutputType('bool')]
    Param (
        [parameter(ValueFromPipeLine,ValueFromPipeLineByPropertyName)]
        [Alias('Number')]
        [string[]] $NumString,

        [switch] $IncludeInput
    )

    begin {
        Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
    }

    process {
        foreach ($n in $NumString) {
            $n = $n.Trim()
            if (($n -eq '') -or ($null -eq $n)) {
                if ($IncludeInput) {
                    New-Object -TypeName psobject -Property ([ordered] @{Input="$n";Result=$false})
                } else {
                    Write-Output -InputObject $false
                }
            } else {
                try {
                    [double] $tmp = 0 + $n
                    if ($IncludeInput) {
                        New-Object -TypeName psobject -Property ([ordered] @{Input="$n";Result=$true})
                    } else {
                        Write-Output -InputObject $true
                    }
                } catch {
                    if ($IncludeInput) {
                        New-Object -TypeName psobject -Property ([ordered] @{Input="$n";Result=$false})
                    } else {
                        Write-Output -InputObject $false
                    }
                }
            }
        }
    }

    end {
        Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
    }
} #EndFunction Test-IsNumeric