Public/Get-VcList.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Function Get-VcList {
    <#
        .SYNOPSIS
            Returns an array of Visual C++ Redistributables.

        .DESCRIPTION
            This function reads the Visual C++ Redistributables listed in an internal manifest or an external JSON file into an array that can be passed to other VcRedist functions.

            A complete listing of the supported and all known redistributables is included in the module. These internal manifests can be exported with Export-VcManifest.

        .OUTPUTS
            System.Array
        
        .NOTES
            Author: Aaron Parker
            Twitter: @stealthpuppy

        .LINK
            https://docs.stealthpuppy.com/docs/vcredist/usage/getting-the-vcredist-list

        .PARAMETER Manifest
            The JSON file that contains the details about the Visual C++ Redistributables. This must be in the expected format.

        .PARAMETER Export
            Defines the list of Visual C++ Redistributables to export - All, Supported or Unsupported Redistributables.
            Defaults to exporting the Supported Redistributables.

        .PARAMETER Release
            Specifies the release (or version) of the redistributables to import into MDT.

        .PARAMETER Architecture
            Specifies the processor architecture to import into MDT. Can be x86 or x64.

        .EXAMPLE
            Get-VcList

            Description:
            Return an array of the supported Visual C++ Redistributables from the embedded manifest.

        .EXAMPLE
            Get-VcList

            Description:
            Returns the 2008, 2010, 2012, 2013 and 2019, x86 and x64 versions of the supported Visual C++ Redistributables from the embedded manifest.

        .EXAMPLE
            Get-VcList -Export All

            Description:
            Returns a list of the all Visual C++ Redistributables from the embedded manifest, including unsupported versions.

        .EXAMPLE
            Get-VcList -Export Supported

            Description:
            Returns the full list of supported Visual C++ Redistributables from the embedded manifest.

        .EXAMPLE
            Get-VcList -Release 2013, 2019 -Architecture x86

            Description:
            Returns the 2013 and 2019 x64 Visual C++ Redistributables from the list of supported Redistributables in the embedded manifest.

        .EXAMPLE
            Get-VcList -Path ".\VisualCRedistributables.json"

            Description:
            Returns a list of the Visual C++ Redistributables listed in the external manifest VisualCRedistributables.json.
    #>

    [OutputType([System.Management.Automation.PSObject])]
    [CmdletBinding(SupportsShouldProcess = $False, DefaultParameterSetName = 'Manifest', `
            HelpURI = "https://docs.stealthpuppy.com/docs/vcredist/usage/getting-the-vcredist-list")]
    Param (
        [Parameter(Mandatory = $False, Position = 0, ValueFromPipeline, ParameterSetName = 'Manifest')]
        [ValidateNotNull()]
        [ValidateScript( { If (Test-Path $_ -PathType 'Leaf') { $True } Else { Throw "Cannot find file $_" } })]
        [Alias("Xml")]
        [string] $Path = (Join-Path (Join-Path $MyInvocation.MyCommand.Module.ModuleBase "Manifests") "VisualCRedistributables.json"),

        [Parameter(Mandatory = $False, ParameterSetName = 'Export')]
        [ValidateSet('Supported', 'All', 'Unsupported')]
        [string] $Export = "Supported",

        [Parameter(Mandatory = $False, ParameterSetName = 'Manifest')]
        [ValidateSet('2005', '2008', '2010', '2012', '2013', '2015', '2017', '2019')]
        [string[]] $Release = @("2008", "2010", "2012", "2013", "2019"),

        [Parameter(Mandatory = $False, ParameterSetName = 'Manifest')]
        [ValidateSet('x86', 'x64')]
        [string[]] $Architecture = @("x86", "x64")
    )
    
    try {
        Write-Verbose -Message "Reading JSON document $Path."
        $content = Get-Content -Raw -Path $Path -ErrorVariable readError -ErrorAction SilentlyContinue
    }
    catch {
        Throw "Unable to read manifest $Path. $readError"
        Break
    }
    
    try {
        # Convert the JSON content to an object
        Write-Verbose -Message "Converting JSON."
        $json = $content | ConvertFrom-Json -ErrorVariable convertError -ErrorAction SilentlyContinue
    }
    catch {
        Throw "Unable to convert manifest JSON to required object. Please validate the input manifest."
        Break
    }
    finally {
        If ($Null -ne $json) {
            If ($PSBoundParameters.ContainsKey('Export')) {
                Switch ($Export) {
                    "Supported" {
                        Write-Verbose -Message "Exporting supported VcRedists."
                        [PSCustomObject] $output = $json.Supported
                    }
                    "All" {
                        Write-Verbose -Message "Exporting all VcRedists."
                        Write-Warning -Message "This list includes unsupported Visual C++ Redistributables."
                        [PSCustomObject] $output = $json.Supported + $json.Unsupported
                    }
                    "Unsupported" {
                        Write-Verbose -Message "Exporting unsupported VcRedists."
                        Write-Warning -Message "This list includes unsupported Visual C++ Redistributables."
                        [PSCustomObject] $output = $json.Unsupported
                    }
                }
            }
            Else {
                # Filter the list for architecture and release
                If ($json | Get-Member -Name "Supported" -MemberType "Properties") {
                    [PSCustomObject] $supported = $json.Supported
                }
                Else {
                    [PSCustomObject] $supported = $json
                }
                [PSCustomObject] $release = $supported | Where-Object { $Release -contains $_.Release }
                [PSCustomObject] $output = $release | Where-Object { $Architecture -contains $_.Architecture }
            }
        }
        Write-Output $output
    }
}