Public/Get-ALHADBitlockerRecoveryKey.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<#PSScriptInfo
 
.VERSION 1.0.1
 
.GUID 759f9aaf-e0c8-4a27-bf43-72acf1dca5db
 
.AUTHOR Dieter Koch
 
.COMPANYNAME
 
.COPYRIGHT (c) 2021-2023 Dieter Koch
 
.TAGS Bitlocker, AD, Active Directory
 
.LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE
 
.PROJECTURI https://github.com/admins-little-helper/ALH
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
    1.0.0
    Initial release.
 
    1.0.1
    Fixed issue in returning computer name.
 
#>



<#
 
.DESCRIPTION
Contains a function to retrieve the Bitlocker recovery key for a computer account stored in Active Directory.
This functions is a proxy function for the 'Get-ADComputer' cmdlet.
 
.LINK
https://github.com/admins-little-helper/ALH
 
#>



function Get-ALHADBitlockerRecoveryKey {
    <#
    .SYNOPSIS
    Retrieves the Bitlocker recovery key for a computer account stored in Active Directory.
 
    .DESCRIPTION
    Queries the Bitlocker recovery key for a computer account stored in Active Directory.
    This functions is a proxy function for the 'Get-ADComputer' cmdlet. It supports the same parameters as the
    'Get-ADComputer' cmdlet. For more information check out the help for that cmdlet.
 
    .EXAMPLE
    Get-ALHADBitlockerRecoveryKey -Identity MyComputer
 
    .EXAMPLE
    Get-ALHADBitlockerRecoveryKey -Identity MyComputer1, MyComputer2
 
    .EXAMPLE
    MyComputer1, MyComputer2 | Get-ALHADBitlockerRecoveryKey
 
    .INPUTS
    System.String
 
    .OUTPUTS
    PSCustomObject
 
    .NOTES
    Author: Dieter Koch
    Email: diko@admins-little-helper.de
 
    .LINK
    https://github.com/admins-little-helper/ALH/blob/main/Help/Get-ALHADBitlockerRecoveryKey.txt
    #>


    [CmdletBinding(DefaultParameterSetName = 'Filter')]
    param()

    dynamicparam {
        try {
            $targetCmd = $ExecutionContext.InvokeCommand.GetCommand('ActiveDirectory\Get-ADComputer', [System.Management.Automation.CommandTypes]::Cmdlet, $PSBoundParameters)
            $dynamicParams = @($targetCmd.Parameters.GetEnumerator() | Microsoft.PowerShell.Core\Where-Object { $_.Value.IsDynamic })
            if ($dynamicParams.Length -gt 0) {
                $paramDictionary = [Management.Automation.RuntimeDefinedParameterDictionary]::new()
                foreach ($param in $dynamicParams) {
                    $param = $param.Value

                    if (-not $MyInvocation.MyCommand.Parameters.ContainsKey($param.Name)) {
                        $dynParam = [Management.Automation.RuntimeDefinedParameter]::new($param.Name, $param.ParameterType, $param.Attributes)
                        $paramDictionary.Add($param.Name, $dynParam)
                    }
                }

                return $paramDictionary
            }
        }
        catch {
            throw
        }
    }

    begin {
        try {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) {
                $PSBoundParameters['OutBuffer'] = 1
            }

            $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('ActiveDirectory\Get-ADComputer', [System.Management.Automation.CommandTypes]::Cmdlet)
            $scriptCmd = { & $wrappedCmd @PSBoundParameters | ForEach-Object {
                    $BitlockerInfo = Get-ADObject -Filter { objectclass -eq 'msFVE-RecoveryInformation' } -SearchBase $_.DistinguishedName -Properties 'msFVE-RecoveryPassword'

                    if ($null -ne $BitlockerInfo) {
                        foreach ($BitlockerRecoveryKey in $BitlockerInfo) {
                            $ComputerInfo = [ordered]@{}
                            $ComputerInfo.Name = $_.Name
                            $ComputerInfo.PasswordID = ($BitlockerRecoveryKey.Name -split "{")[1] -replace "}", ""
                            $ComputerInfo.PasswordDate = Get-Date (($BitlockerRecoveryKey.Name -split "{")[0])
                            $ComputerInfo.RecoveryPassword = $BitlockerRecoveryKey.'msFVE-RecoveryPassword'

                            foreach ($Parameter in $PSBoundParameters.Properties) {
                                $ComputerInfo.$Parameter = $_.$Parameter
                            }

                            $ComputerInfoObj = New-Object -TypeName PSObject -Property $ComputerInfo
                            $ComputerInfoObj
                        }
                    }
                    else {
                        Write-Verbose -Message "No Bitlocker information found for computer '$($_.Name)'"
                    }
                }
            }

            $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
            $steppablePipeline.Begin($PSCmdlet)
        }
        catch {
            throw
        }
    }

    process {
        try {
            $steppablePipeline.Process($_)
        }
        catch {
            throw
        }
    }

    end {
        try {
            $steppablePipeline.End()
        }
        catch {
            throw
        }
    }

    <#
    #.ForwardHelpTargetName ActiveDirectory\Get-ADComputer
    #.ForwardHelpCategory Cmdlet
    #>


}


#region EndOfScript
<#
################################################################################
################################################################################
#
# ______ _ __ _____ _ _
# | ____| | | / _| / ____| (_) | |
# | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_
# | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __|
# | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_
# |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__|
# | |
# |_|
################################################################################
################################################################################
# created with help of http://patorjk.com/software/taag/
#>

#endregion