functions/objects/Set-PSUObjectType.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
67
68
69
70
71
72
73
74
function Set-PSUObjectType
{
    <#
        .SYNOPSIS
            Tries to convert an object from one type of another.
         
        .DESCRIPTION
            Tries to convert an object from one type of another.
         
        .PARAMETER InputObject
            The objects to convert.
         
        .PARAMETER Type
            ParSet: Type
            The type to cast to.
         
        .PARAMETER TypeName
            ParSet: String
            The type to cast to.
         
        .PARAMETER EnableException
            Replaces user friendly yellow warnings with bloody red exceptions of doom!
            Use this if you want the function to throw terminating errors you want to catch.
         
        .EXAMPLE
            PS C:\> "01", "02", "03", "42" | Set-PSUObjectType "int"
             
            Tries to convert strings with numeric values into pure integers (hint: This will probably succeede).
    #>

    [CmdletBinding(DefaultParameterSetName = "String")]
    Param (
        [Parameter(ValueFromPipeline = $true)]
        $InputObject,
        
        [Parameter(ParameterSetName = "Type")]
        [Type]
        $Type,
        
        [Parameter(ParameterSetName = "String", Position = 0)]
        [String]
        $TypeName,
        
        [switch]
        $EnableException
    )
    
    Begin
    {
        Write-PSFMessage -Level Debug -Message "Casting Objects to another type" -Tag start
        $ParSet = $PSCmdlet.ParameterSetName
        Write-PSFMessage -Level InternalComment -Message "Active Parameterset: $ParSet | Bound Parameters: $($PSBoundParameters.Keys -join ", ")" -Tag start
        
        switch ($ParSet)
        {
            "Type" { $name = $Type.FullName }
            "String" { $name = $TypeName }
        }
    }
    Process
    {
        foreach ($object in $InputObject)
        {
            $temp = $null
            $temp = $object -as $name
            if ($temp) { $temp }
            else { Stop-PSFFunction -Message "Failed to convert '$object' to '$name'" -EnableException $EnableException -Category InvalidData -Tag fail, cast -Target $object -Continue }
        }
    }
    End
    {
        Write-PSFMessage -Level Debug -Message "Casting Objects to another type" -Tag end
    }
}
Import-PSUAlias -Name "cast" -Command "Set-PSUObjectType"