HelperFunctions.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function ConvertTo-Base64
{
    <#
    .SYNOPSIS
        Converts an image to its base64 string representation.
    .DESCRIPTION
        A base64 string can be passed to 'Show-Anybox' to show an image, which eliminates
        the reliance on the external file, making the script more easily portable.
    .PARAMETER ImagePath
        Specifies a path to one or more locations.
    .PARAMETER ImagePath
        Specifies a path to one or more locations.
    .EXAMPLE
        [string]$base64 = 'C:\Path\to\img.png' | ConvertTo-Base64
        Show-AnyBox -Image $base64 -Message 'Hello World'
    .INPUTS
        The path to an image file.
    .OUTPUTS
        The base64 string representation of the image at $ImagePath.
    #>

    param(
        [Parameter(ValueFromPipeline=$true)]
        [string[]]$ImagePath,
        [ValidateNotNullOrEmpty()]
        [System.Drawing.Imaging.ImageFormat]$ImageFormat = [System.Drawing.Imaging.ImageFormat]::Png
    )

    process {
        foreach ($img in $ImagePath) {
            $bmp = [System.Drawing.Bitmap]::FromFile($img)

            $memory = New-Object System.IO.MemoryStream
            $null = $bmp.Save($memory, $ImageFormat)

            [byte[]]$bytes = $memory.ToArray()

            $memory.Close()

            [System.Convert]::ToBase64String($bytes)
        }
    }
}

function ConvertTo-BitmapImage
{
    <#
    .SYNOPSIS
        Converts a base64 string to a BitmapImage object.
    .DESCRIPTION
        Used by 'Show-AnyBox' to convert a base64 string into a [System.Windows.Media.Imaging.BitmapImage].
    .PARAMETER base64
        The base64 string representing an image.
    .INPUTS
        The base64 string representing an image.
    .OUTPUTS
        A [System.Windows.Media.Imaging.BitmapImage] object.
    #>

    param([
        Parameter(ValueFromPipeline=$true)]
        [string[]]$base64
    )

    process {
        foreach($str in $base64) {
            $bmp = [System.Drawing.Bitmap]::FromStream((New-Object System.IO.MemoryStream (@(, [Convert]::FromBase64String($base64)))))

            $memory = New-Object System.IO.MemoryStream
            $null = $bmp.Save($memory, [System.Drawing.Imaging.ImageFormat]::Png)
            $memory.Position = 0

            $img = New-Object System.Windows.Media.Imaging.BitmapImage
            $img.BeginInit()
            $img.StreamSource = $memory
            $img.CacheOption = [System.Windows.Media.Imaging.BitmapCacheOption]::OnLoad
            $img.EndInit()
            $img.Freeze()

            $memory.Close()

            $img
        }
    }
}

function ConvertTo-Long
{
    <#
    .SYNOPSIS
        "Melts" object(s) into an array of key-value pairs.
    .DESCRIPTION
        Converts object(s) wide objects into a long array object for better display.
    .PARAMETER obj
        The object(s) to melt.
    .PARAMETER KeyName
        The name of the resulting key column; defaults to "Name".
    .PARAMETER obj
        The name of the resulting value column; defaults to "Value".
    .INPUTS
        One or more objects.
    .OUTPUTS
        An array of objects with properties "$KeyName" and "$ValueName".
    #>

    param(
        [Parameter(ValueFromPipeline=$true)]
        [object[]]$obj,
        [ValidateNotNullOrEmpty()]
        [string]$KeyName = 'Name',
        [ValidateNotNullOrEmpty()]
        [string]$ValueName = 'Value'
    )
    
    process {
        foreach ($o in $obj) {
            $o.psobject.Properties | foreach { [pscustomobject]@{ $KeyName = $_.Name; $ValueName = $_.Value } }
        }
    }
}