1
+ function Get-ManagedDeviceDetails {
2
+ [CmdletBinding (DefaultParameterSetName = ' Specific' )]
3
+ param (
4
+ [Parameter (Mandatory , ParameterSetName = " All" )]
5
+ # Allows you to search for all devices based on Operating System rather than a set list
6
+ [switch ]$SearchAll ,
7
+ [Parameter (Mandatory , ParameterSetName = ' Specific' )]
8
+ # Used to specify the Variable where the details on devices is held within Powershell
9
+ [PSCustomObject ]$SearchVariable ,
10
+ [Parameter (Mandatory , ParameterSetName = ' Specific' )]
11
+ [validateset (' Hostname' , ' IntuneID' , ' AzureID' , ' ObjectID' , ' SerialNo' , ' IMEI' )]
12
+ # Used to specify the type of search to perform based on the Hostname, IntuneID, AzureID or ObjectID
13
+ [String ]$SearchType ,
14
+ [Parameter (Mandatory , ParameterSetName = ' Specific' )]
15
+ # Used to specify the -SearchVariable Header to check against for -SearchType
16
+ [String ]$SearchHeader ,
17
+ [Parameter (Mandatory , ParameterSetName = " All" )]
18
+ # Specifies OS to search through if -SearchAll is selected
19
+ [validateset (' Windows' , ' Android' , ' iOS' , ' MacOS' , ' Linux' , ' All' )]
20
+ [string ]$OperatingSystem
21
+ )
22
+ <#
23
+ . SYNOPSIS
24
+ Get Various Device Details from Intune and Azure.
25
+
26
+ . DESCRIPTION
27
+ Version 4 (22/04/25)
28
+ This script searches through Intune Azure and the mgraph API to gather various details about devices, it has the ability to search using
29
+ Hostnames, IntuneID's, AzureID's and ObjectID's. This requires the following Permissions to mgraph which the function will request.
30
+ - User.Read.All
31
+ - Group.ReadWrite.All
32
+ - DeviceManagementManagedDevices.Read.All
33
+
34
+ . PARAMETER SearchVariable
35
+ Specifies the existing variable which contains the desired device details that are going to be searched for.
36
+
37
+ . PARAMETER SearchHeader
38
+ Specifies the Header of the SearchVariable to check against. Note that the Variable containing data MUST have a header.
39
+
40
+ . PARAMETER SearchType
41
+ Specifies what details you are providing to search against, this accepts Hostname, IntuneID, AzureID, ObjectID, SerialNo or IMEI.
42
+
43
+ . PARAMETER SearchAll
44
+ Allows you to search through All devices in Intune rather than a set list of devices.
45
+
46
+ . PARAMETER OperatingSystem
47
+ Specifies the operating system to search through when -SearchAll is used, this accpets Windows, Android, iOS, MacOS, Linux and All.
48
+
49
+ . OUTPUTS
50
+ Outputs a PSCustomObject for each device searched.
51
+
52
+ . EXAMPLE
53
+ PS> Get-DeviceDetails -SearchAll -OperatingSystem Android.
54
+
55
+ . EXAMPLE
56
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType ObjectID -SearchHeader ObjectID.
57
+
58
+ . EXAMPLE
59
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType IntuneID -SearchHeader ID.
60
+
61
+ . EXAMPLE
62
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType AzureID -SearchHeader AzureAdDeviceId.
63
+
64
+ . EXAMPLE
65
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType Hostname -SearchHeader DeviceName.
66
+
67
+ . EXAMPLE
68
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType SerialNo -SearchHeader SerialNumber.
69
+
70
+ . EXAMPLE
71
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType IMEI -SearchHeader IMEI.
72
+
73
+ . EXAMPLE
74
+ PS> Get-DeviceDetails -SearchVariable $DeviceList -SearchType AzureID -SearchHeader "Azure Device ID".
75
+
76
+ #--------------------------------------Start--------------------------------------#>
77
+
78
+ # Connect to Mgraph
79
+ Connect-MgGraph - Scopes " User.Read.All" , " Group.ReadWrite.All" , ' DeviceManagementManagedDevices.Read.All' - NoWelcome
80
+
81
+ if ($SearchAll ) {
82
+ # Warns user that they'll be searching everything and provides status
83
+ Write-Warning " This will query EVERY device in Intune and Azure, are you sure you want to Continue?" - WarningAction Inquire
84
+ Write-Host " Warning acknowledged, this may take some time to complete."
85
+ Write-Host " Gathering Devices Please Wait"
86
+ # Searches through all devices based on chosen OS
87
+ switch ($OperatingSystem ) {
88
+ Windows { $AllThingies = Get-MgBetaDeviceManagementManagedDevice - Filter " OperatingSystem eq 'Windows'" - All }
89
+ Android { $AllThingies = Get-MgBetaDeviceManagementManagedDevice - Filter " OperatingSystem eq 'Android'" - All }
90
+ iOS { $AllThingies = Get-MgBetaDeviceManagementManagedDevice - Filter " OperatingSystem eq 'iOS'" - All }
91
+ MacOS { $AllThingies = Get-MgBetaDeviceManagementManagedDevice - Filter " OperatingSystem eq 'MacOS'" - All }
92
+ Linux { $AllThingies = Get-MgBetaDeviceManagementManagedDevice - Filter " OperatingSystem eq 'Linux'" - All }
93
+ All { $AllThingies = Get-MgBetaDeviceManagementManagedDevice - All }
94
+ }
95
+ # Gets Azure details of each device from $AllThingies and filters out any with invalid Azure ID's to prevent them being searched
96
+ foreach ($thingy in $AllThingies ) {
97
+ $thingyIntune = $thingy
98
+ Write-Progress - Activity " Gathering Device Details" - status " $ ( $thingy.DeviceName ) "
99
+ switch ($thingy.AzureAdDeviceId ) {
100
+ " 00000000-0000-0000-0000-000000000000" { $thingyAzure = $null }
101
+ Default {
102
+ $ThingyAzure = Get-MgDeviceByDeviceId - DeviceId $thingy.AzureAdDeviceId
103
+ }
104
+ }
105
+ # Gathers hardware info of the devices
106
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingy.Id ) " + ' ?$select=hardwareinformation' )
107
+ # Created custom object for each device searched
108
+ [PSCustomObject ]@ {
109
+ IntuneID = $thingyIntune.Id
110
+ AzureID = if ($thingyIntune.AzureAdDeviceId -eq " 00000000-0000-0000-0000-000000000000" ) { $null } else { $thingyIntune.AzureAdDeviceId }
111
+ ObjectID = $thingyAzure.ID
112
+ DeviceType = $thingyIntune.ChassisType
113
+ Hostname = $thingyIntune.DeviceName
114
+ EnrolmentType = $thingyIntune.DeviceEnrollmentType
115
+ WiredIPV4 = if ($ThingyHardware.hardwareInformation.wiredIPv4Addresses ) { ($ThingyHardware.hardwareInformation.wiredIPv4Addresses ).replace(" {" , " " ) } else { " Empty" }
116
+ WirelessIPV4 = if ($ThingyHardware.hardwareInformation.ipAddressV4 ) { ($ThingyHardware.hardwareInformation.ipAddressV4 ).replace(" {" , " " ) } else { " Empty" }
117
+ OperatingSystem = $thingyIntune.OperatingSystem
118
+ OSVersion = $thingyIntune.OSVersion
119
+ Model = $thingyIntune.Model
120
+ SerialNumber = $thingyIntune.SerialNumber
121
+ Manufacturer = $thingyIntune.Manufacturer
122
+ IMEI = if ($thingyIntune.IMEI ) { $thingyIntune.IMEI } else { " N/A" }
123
+ PrimaryUser = $thingyIntune.UserDisplayName
124
+ PrimaryUserEmail = $thingyIntune.EmailAddress
125
+ UserID = $thingyIntune.UserId
126
+ LastSync = $thingyIntune.LastSyncDateTime
127
+ }
128
+
129
+ }
130
+ }
131
+ else {
132
+ foreach ($thingy in $SearchVariable ) {
133
+ # skips any devices within provided data that are blank or null to speed up processing
134
+ if ($Thingy .$SearchHeader -ne " " -and $null -ne $Thingy .$SearchHeader ) {
135
+ switch ($SearchType ) {
136
+ ' Hostname' {
137
+ # Searches Intune for devices that match the specific Hostname using the $searchHeader Specified
138
+ $thingyIntune = Get-MgBetaDeviceManagementManagedDevice - Filter " DeviceName eq '$ ( $Thingy .$SearchHeader ) '"
139
+ if ($thingyIntune ) {
140
+ if ($thingyIntune.count -gt 1 ) {
141
+ $thingyIntune = $thingyIntune | Sort-Object - Property [date ]LastSyncDateTime - Descending | Select-Object - First 1
142
+ Write-Warning " Multiple Devices found for $ ( $Thingy .$SearchHeader ) . Returning Device with latest Sync Date, please validate manually."
143
+ }
144
+ # Uses the gathered AzureID from Intune to search the devices Azure Details
145
+ switch ($thingyIntune.AzureAdDeviceId ) {
146
+ " 00000000-0000-0000-0000-000000000000" { $thingyAzure = $null }
147
+ Default { $ThingyAzure = Get-MgDeviceByDeviceId - DeviceId $thingyIntune.AzureAdDeviceId }
148
+ }
149
+ # Queries mgraph API for hardware information
150
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingyIntune.Id ) " + ' ?$select=hardwareinformation' )
151
+ }
152
+ }
153
+ ' IntuneID' {
154
+ # Searches Intune for devices that match the specific IntuneID using the $searchHeader Specified
155
+ $thingyIntune = Get-MgBetaDeviceManagementManagedDevice - ManagedDeviceId $thingy .$SearchHeader
156
+ # Uses the gathered AzureID from Intune to search the devices Azure Details
157
+ switch ($thingyIntune.AzureAdDeviceId ) {
158
+ " 00000000-0000-0000-0000-000000000000" { $thingyAzure = $null }
159
+ Default { $ThingyAzure = Get-MgDeviceByDeviceId - DeviceId $thingyIntune.AzureAdDeviceId }
160
+ }
161
+ # Queries mgraph API for hardware information
162
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingyIntune.Id ) " + ' ?$select=hardwareinformation' )
163
+ }
164
+ ' AzureID' {
165
+ # Searches Azure for devices that match the specific AzureID using the $searchHeader Specified
166
+ $ThingyAzure = Get-MgDeviceByDeviceId - DeviceId $thingy .$SearchHeader
167
+ # Uses the gathered AzureID from Azure to search the devices Intune Details
168
+ $thingyIntune = Get-MgBetaDeviceManagementManagedDevice - Filter " AzureAdDeviceId eq '$ ( $ThingyAzure.DeviceId ) '"
169
+ # Queries mgraph API for hardware information
170
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingyIntune.Id ) " + ' ?$select=hardwareinformation' )
171
+ }
172
+ ' ObjectID' {
173
+ # Searches Azure for devices that match the specific ObjectID using the $searchHeader Specified
174
+ $ThingyAzure = Get-MgDevice - Filter " ID eq '$ ( $Thingy .$SearchHeader ) '"
175
+ # Uses the gathered AzureID from Azure to search the devices Intune Details
176
+ $thingyIntune = Get-MgBetaDeviceManagementManagedDevice - Filter " AzureAdDeviceId eq '$ ( $ThingyAzure.DeviceId ) '"
177
+ # Queries mgraph API for hardware information
178
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingyIntune.Id ) " + ' ?$select=hardwareinformation' )
179
+ }
180
+ ' SerialNo' {
181
+ # Searches Intune for devices that match the specific Hostname using the $searchHeader Specified
182
+ $thingyIntune = Get-MgBetaDeviceManagementManagedDevice - Filter " SerialNumber eq '$ ( $Thingy .$SearchHeader ) '"
183
+ if ($thingyIntune ) {
184
+ if ($thingyIntune.count -gt 1 ) {
185
+ $thingyIntune = $thingyIntune | Sort-Object - Property [date ]LastSyncDateTime - Descending | Select-Object - First 1
186
+ Write-Warning " Multiple Devices found for $ ( $Thingy .$SearchHeader ) . Returning Device with latest Sync Date, please validate manually."
187
+ }
188
+ # Uses the gathered AzureID from Intune to search the devices Azure Details
189
+ switch ($thingyIntune.AzureAdDeviceId ) {
190
+ " 00000000-0000-0000-0000-000000000000" { $thingyAzure = $null }
191
+ Default { $ThingyAzure = Get-MgDeviceByDeviceId - DeviceId $thingyIntune.AzureAdDeviceId }
192
+ }
193
+ # Queries mgraph API for hardware information
194
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingyIntune.Id ) " + ' ?$select=hardwareinformation' )
195
+ }
196
+ }
197
+ ' IMEI' {
198
+ # Searches Intune for devices that match the specific Hostname using the $searchHeader Specified
199
+ $thingyIntune = Get-MgBetaDeviceManagementManagedDevice - Filter " IMEI eq '$ ( $Thingy .$SearchHeader ) '"
200
+ if ($thingyIntune ) {
201
+ if ($thingyIntune.count -gt 1 ) {
202
+ $thingyIntune = $thingyIntune | Sort-Object - Property [date ]LastSyncDateTime - Descending | Select-Object - First 1
203
+ Write-Warning " Multiple Devices found for $ ( $Thingy .$SearchHeader ) . Returning Device with latest Sync Date, please validate manually."
204
+ }
205
+ # Uses the gathered AzureID from Intune to search the devices Azure Details
206
+ switch ($thingyIntune.AzureAdDeviceId ) {
207
+ " 00000000-0000-0000-0000-000000000000" { $thingyAzure = $null }
208
+ Default { $ThingyAzure = Get-MgDeviceByDeviceId - DeviceId $thingyIntune.AzureAdDeviceId }
209
+ }
210
+ # Queries mgraph API for hardware information
211
+ $ThingyHardware = Invoke-MgGraphRequest - Method GET - Uri (" https://graph.microsoft.com/beta/deviceManagement/managedDevices/$ ( $thingyIntune.Id ) " + ' ?$select=hardwareinformation' )
212
+ }
213
+ }
214
+ }
215
+ if ($null -ne $thingyIntune ) {
216
+ Write-Progress - Activity " Gathering Device Details" - status " $ ( $thingyIntune.DeviceName ) "
217
+ # Creates custom object to output details in a desired format
218
+ [PSCustomObject ]@ {
219
+ SearchedTerm = $thingy .$SearchHeader
220
+ IntuneID = $thingyIntune.Id
221
+ AzureID = if ($thingyIntune.AzureAdDeviceId -eq " 00000000-0000-0000-0000-000000000000" ) { $null } else { $thingyIntune.AzureAdDeviceId }
222
+ ObjectID = $thingyAzure.ID
223
+ DeviceType = $thingyIntune.ChassisType
224
+ Hostname = $thingyIntune.DeviceName
225
+ EnrolmentType = $thingyIntune.DeviceEnrollmentType
226
+ WiredIPV4 = if ($ThingyHardware.hardwareInformation.wiredIPv4Addresses ) { ($ThingyHardware.hardwareInformation.wiredIPv4Addresses ).replace(" {" , " " ) } else { " Empty" }
227
+ WirelessIPV4 = if ($ThingyHardware.hardwareInformation.ipAddressV4 ) { ($ThingyHardware.hardwareInformation.ipAddressV4 ).replace(" {" , " " ) } else { " Empty" }
228
+ OperatingSystem = $thingyIntune.OperatingSystem
229
+ OSVersion = $thingyIntune.OSVersion
230
+ Model = $thingyIntune.Model
231
+ SerialNumber = $thingyIntune.SerialNumber
232
+ Manufacturer = $thingyIntune.Manufacturer
233
+ IMEI = if ($thingyIntune.IMEI ) { $thingyIntune.IMEI } else { " N/A" }
234
+ PrimaryUser = $thingyIntune.UserDisplayName
235
+ PrimaryUserEmail = $thingyIntune.EmailAddress
236
+ UserID = $thingyIntune.UserId
237
+ LastSync = $thingyIntune.LastSyncDateTime
238
+ }
239
+ }
240
+ else {
241
+ # If no device is found when searching Intune, return as an Invalid device with null data
242
+ Write-Host " Invalid Device $ ( $thingy .$SearchHeader ) "
243
+ [PSCustomObject ]@ {
244
+ SearchedTerm = $thingy .$SearchHeader
245
+ IntuneID = $Null
246
+ AzureID = $Null
247
+ ObjectID = $Null
248
+ DeviceType = $Null
249
+ Hostname = $Null
250
+ EnrolmentType = $Null
251
+ WiredIPV4 = $Null
252
+ WirelessIPV4 = $Null
253
+ OperatingSystem = $Null
254
+ OSVersion = $Null
255
+ Model = $Null
256
+ SerialNumber = $Null
257
+ Manufacturer = $Null
258
+ IMEI = $Null
259
+ PrimaryUser = $Null
260
+ PrimaryUserEmail = $Null
261
+ UserID = $Null
262
+ LastSync = $Null
263
+ }
264
+ }
265
+ # Clears variables each iteration to prevent objects containing incorrect data if they are null
266
+ Clear-Variable thingy, thingyIntune, ThingyHardware, thingyAzure
267
+ }
268
+ }
269
+ }
270
+ }
0 commit comments