|
| 1 | +function Search-CIPPBitlockerKeys { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Search for BitLocker recovery keys and merge with device information |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Searches cached BitLocker recovery keys and automatically enriches results with device information |
| 8 | + by cross-referencing the deviceId with Devices or ManagedDevices data. |
| 9 | +
|
| 10 | + .PARAMETER TenantFilter |
| 11 | + Tenant domain or GUID to search. If not specified, searches all tenants. |
| 12 | +
|
| 13 | + .PARAMETER KeyId |
| 14 | + Optional BitLocker recovery key ID to search for. If not specified, returns all keys. |
| 15 | +
|
| 16 | + .PARAMETER DeviceId |
| 17 | + Optional device ID to filter BitLocker keys by device. |
| 18 | +
|
| 19 | + .PARAMETER SearchTerms |
| 20 | + Optional search terms to filter results (searches across all BitLocker key fields). |
| 21 | +
|
| 22 | + .PARAMETER Limit |
| 23 | + Maximum number of results to return. Default is unlimited (0). |
| 24 | +
|
| 25 | + .EXAMPLE |
| 26 | + Search-CIPPBitlockerKeys -TenantFilter 'contoso.onmicrosoft.com' -KeyId '8911a878-b631-47e8-b5e8-bcb00e586c74' |
| 27 | +
|
| 28 | + .EXAMPLE |
| 29 | + Search-CIPPBitlockerKeys -DeviceId '1b418b08-a0c6-4db1-95cd-08a9b943b70e' |
| 30 | +
|
| 31 | + .EXAMPLE |
| 32 | + Search-CIPPBitlockerKeys -SearchTerms 'device-name' |
| 33 | +
|
| 34 | + .FUNCTIONALITY |
| 35 | + Internal |
| 36 | + #> |
| 37 | + [CmdletBinding()] |
| 38 | + param( |
| 39 | + [Parameter(Mandatory = $false)] |
| 40 | + [string]$TenantFilter, |
| 41 | + |
| 42 | + [Parameter(Mandatory = $false)] |
| 43 | + [string]$KeyId, |
| 44 | + |
| 45 | + [Parameter(Mandatory = $false)] |
| 46 | + [string]$DeviceId, |
| 47 | + |
| 48 | + [Parameter(Mandatory = $false)] |
| 49 | + [string[]]$SearchTerms, |
| 50 | + |
| 51 | + [Parameter(Mandatory = $false)] |
| 52 | + [int]$Limit = 0 |
| 53 | + ) |
| 54 | + |
| 55 | + try { |
| 56 | + # Build search parameters |
| 57 | + $SearchParams = @{ |
| 58 | + Types = @('BitlockerKeys') |
| 59 | + } |
| 60 | + |
| 61 | + if ($TenantFilter) { |
| 62 | + $SearchParams.TenantFilter = @($TenantFilter) |
| 63 | + } |
| 64 | + |
| 65 | + # Determine what to search for |
| 66 | + if ($KeyId) { |
| 67 | + $SearchParams.SearchTerms = @($KeyId) |
| 68 | + } elseif ($DeviceId) { |
| 69 | + $SearchParams.SearchTerms = @($DeviceId) |
| 70 | + } elseif ($SearchTerms) { |
| 71 | + $SearchParams.SearchTerms = $SearchTerms |
| 72 | + } else { |
| 73 | + # If no search criteria, search for a pattern that matches any GUID or just get all |
| 74 | + $SearchParams.SearchTerms = @('[a-f0-9]{8}-') |
| 75 | + } |
| 76 | + |
| 77 | + if ($Limit -gt 0) { |
| 78 | + $SearchParams.Limit = $Limit |
| 79 | + } |
| 80 | + |
| 81 | + Write-Verbose "Searching for BitLocker keys with params: $($SearchParams | ConvertTo-Json -Compress)" |
| 82 | + |
| 83 | + # Search for BitLocker keys |
| 84 | + $BitlockerResults = Search-CIPPDbData @SearchParams |
| 85 | + |
| 86 | + if (-not $BitlockerResults -or $BitlockerResults.Count -eq 0) { |
| 87 | + Write-Verbose 'No BitLocker keys found' |
| 88 | + return @() |
| 89 | + } |
| 90 | + |
| 91 | + Write-Verbose "Found $($BitlockerResults.Count) BitLocker key(s)" |
| 92 | + |
| 93 | + # Enrich each result with device information |
| 94 | + $EnrichedResults = foreach ($Result in $BitlockerResults) { |
| 95 | + $BitlockerData = $Result.Data |
| 96 | + $DeviceInfo = $null |
| 97 | + |
| 98 | + if ($BitlockerData.deviceId) { |
| 99 | + Write-Verbose "Looking up device info for deviceId: $($BitlockerData.deviceId)" |
| 100 | + |
| 101 | + # Try to find device in Devices collection first |
| 102 | + try { |
| 103 | + $DeviceSearch = Search-CIPPDbData -TenantFilter $Result.Tenant -Types 'Devices' -SearchTerms $BitlockerData.deviceId -Limit 1 |
| 104 | + if ($DeviceSearch -and $DeviceSearch.Count -gt 0) { |
| 105 | + $DeviceInfo = $DeviceSearch[0].Data |
| 106 | + Write-Verbose "Found device in Devices collection: $($DeviceInfo.displayName)" |
| 107 | + } |
| 108 | + } catch { |
| 109 | + Write-Verbose "Error searching Devices: $($_.Exception.Message)" |
| 110 | + } |
| 111 | + |
| 112 | + # If not found in Devices, try ManagedDevices |
| 113 | + if (-not $DeviceInfo) { |
| 114 | + try { |
| 115 | + $DeviceSearch = Search-CIPPDbData -TenantFilter $Result.Tenant -Types 'ManagedDevices' -SearchTerms $BitlockerData.deviceId -Limit 1 |
| 116 | + if ($DeviceSearch -and $DeviceSearch.Count -gt 0) { |
| 117 | + $DeviceInfo = $DeviceSearch[0].Data |
| 118 | + Write-Verbose "Found device in ManagedDevices collection: $($DeviceInfo.deviceName)" |
| 119 | + } |
| 120 | + } catch { |
| 121 | + Write-Verbose "Error searching ManagedDevices: $($_.Exception.Message)" |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + # Create enriched result |
| 127 | + $EnrichedData = [PSCustomObject]@{ |
| 128 | + # BitLocker key information |
| 129 | + id = $BitlockerData.id |
| 130 | + createdDateTime = $BitlockerData.createdDateTime |
| 131 | + volumeType = $BitlockerData.volumeType |
| 132 | + deviceId = $BitlockerData.deviceId |
| 133 | + |
| 134 | + # Device information (if found) |
| 135 | + deviceName = if ($DeviceInfo) { $DeviceInfo.displayName ?? $DeviceInfo.deviceName } else { $null } |
| 136 | + operatingSystem = if ($DeviceInfo) { $DeviceInfo.operatingSystem } else { $null } |
| 137 | + osVersion = if ($DeviceInfo) { $DeviceInfo.operatingSystemVersion ?? $DeviceInfo.osVersion } else { $null } |
| 138 | + lastSignIn = if ($DeviceInfo) { $DeviceInfo.approximateLastSignInDateTime ?? $DeviceInfo.lastSyncDateTime } else { $null } |
| 139 | + accountEnabled = if ($DeviceInfo) { $DeviceInfo.accountEnabled ?? $DeviceInfo.isCompliant } else { $null } |
| 140 | + trustType = if ($DeviceInfo) { $DeviceInfo.trustType ?? $DeviceInfo.joinType } else { $null } |
| 141 | + |
| 142 | + # Metadata |
| 143 | + deviceFound = $null -ne $DeviceInfo |
| 144 | + } |
| 145 | + |
| 146 | + [PSCustomObject]@{ |
| 147 | + Tenant = $Result.Tenant |
| 148 | + Type = $Result.Type |
| 149 | + RowKey = $Result.RowKey |
| 150 | + Data = $EnrichedData |
| 151 | + Timestamp = $Result.Timestamp |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + Write-Verbose "Returning $($EnrichedResults.Count) enriched result(s)" |
| 156 | + return $EnrichedResults |
| 157 | + |
| 158 | + } catch { |
| 159 | + Write-LogMessage -API 'SearchBitlockerKeys' -tenant $TenantFilter -message "Failed to search BitLocker keys: $($_.Exception.Message)" -sev Error |
| 160 | + throw |
| 161 | + } |
| 162 | +} |
0 commit comments