-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathInvoke-MgConsoleGuiGraphSearch.ps1
More file actions
382 lines (344 loc) · 15.8 KB
/
Invoke-MgConsoleGuiGraphSearch.ps1
File metadata and controls
382 lines (344 loc) · 15.8 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<#
.SYNOPSIS
Interactive Microsoft Graph search tool using console GUI for various Microsoft 365 and Azure AD object types.
.DESCRIPTION
This function provides an interactive console-based GUI interface to search and view details of various Microsoft Graph objects.
It supports different object types including Users, Groups, Devices, Mobile Apps, Service Principals, Settings Catalog policies,
and Configuration Profiles. The function utilizes Microsoft Graph Beta API endpoints and requires appropriate permissions.
.PARAMETER ObjectType
Specifies the type of object to search for. Valid values include:
- "User": Microsoft 365 users
- "Group": Microsoft 365 groups
- "Device": Devices managed in Microsoft Endpoint Manager
- "MobileApp": Mobile apps managed in Microsoft Endpoint Manager
- "ServicePrincipal": Service principals in Azure AD
- "SettingsCatalog": Settings catalog policies in Microsoft Endpoint Manager
- "ConfigProfile": Configuration profiles in Microsoft Endpoint Manager
.PARAMETER Search
Optional search string to filter results. Search behavior varies by object type:
- Users/Groups: Supports server-side search
- Devices: Supports server-side search
- MobileApps/ServicePrincipals: Uses client-side filtering
- SettingsCatalog/ConfigProfile: Uses OData filtering
.EXAMPLE
Invoke-MgConsoleGuiGraphSearch -ObjectType User
Shows all users in an interactive grid view
.EXAMPLE
Invoke-MgConsoleGuiGraphSearch -ObjectType Group -Search "IT"
Shows groups containing "IT" in their display name
.NOTES
Prerequisites:
- Microsoft.Graph PowerShell SDK (Beta profile)
- Microsoft.PowerShell.ConsoleGuiTools module
- Appropriate Microsoft Graph permissions based on object type
Required Permissions:
- User: User.Read.All
- Group: Group.Read.All
- Device: Device.Read.All
- MobileApp: DeviceManagementApps.Read.All
- ServicePrincipal: Application.Read.All
- SettingsCatalog: DeviceManagementConfiguration.Read.All
- ConfigProfile: DeviceManagementConfiguration.Read.All
- AppProtection: DeviceManagementApps.Read.All
- CompliancePolicy: DeviceManagementConfiguration.Read.All
- DiscoveredApps: DeviceManagementManagedDevices.Read.All
- Note: The script checks for the required permissions and prompts the user if they are missing.
.VERSION
0.1.0
Initial version of the script.
.LINK
https://learn.microsoft.com/graph/api/overview?view=graph-rest-beta
https://github.com/PowerShell/ConsoleGuiTools
#>
#requires -modules Microsoft.PowerShell.ConsoleGuiTools
#requires -version 6.0
Function Invoke-MgConsoleGuiGraphSearch {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
# Object types and their purposes:
# "User" - Represents Microsoft 365 users.
# "Group" - Represents Microsoft 365 groups.
# "Device" - Represents devices managed in Microsoft Endpoint Manager.
# "MobileApp" - Represents mobile apps managed in Microsoft Endpoint Manager.
# "ServicePrincipal" - Represents service principals in Azure AD.
# "SettingsCatalog" - Represents settings catalog policies in Microsoft Endpoint Manager.
# "ConfigProfile" - Represents configuration profiles in Microsoft Endpoint Manager.
# "AppProtection" - Represents app protection policies in Microsoft Endpoint Manager.
# "CompliancePolicy" - Represents compliance policies in Microsoft Endpoint Manager.
[ValidateSet(
"User",
"Group",
"Device",
"MobileApp",
"ServicePrincipal",
"SettingsCatalog",
"ConfigProfile",
"AppProtection",
"CompliancePolicy",
"DiscoveredApps"
)]
[string]$ObjectType,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[string]$Search , # Search string is optional
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[ValidateSet("JSON", "Grid", "List", "Table", "System.Object")]
[string]$OutputType = "System.Object" # Default output type
)
# Begin block to initialize resources
begin {
# The helper function remains local
Function IsNullOrWhitespace {
param([string]$InputString)
return [string]::IsNullOrEmpty($InputString) -or $InputString.Trim() -eq ""
}
function Get-GraphItemsForType {
param(
[Parameter(Mandatory = $true)]
[string]$ObjectType,
[string]$Search
)
# Determine whether a search term is provided.
$noSearch = [string]::IsNullOrWhiteSpace($Search)
$splat = @{ All = $true }
if ($ObjectType -match "User|Group|Device|ServicePrincipal" -and (-not $noSearch)) {
$splat += @{ Search = "displayName:$Search"; ConsistencyLevel = "eventual" }
}
$items = $null
switch ($ObjectType) {
"User" {
return Get-MgBetaUser @splat
}
"Group" {
return Get-MgBetaGroup @splat
}
"Device" {
return Get-MgBetaDevice @splat
}
"ServicePrincipal" {
return Get-MgBetaServicePrincipal @splat
}
"SettingsCatalog" {
$items = Get-MgBetaDeviceManagementConfigurationPolicy -All
if (-not $noSearch) {
return $items | Where-Object { $_.Name -like "*$Search*" }
}
return $items
}
"MobileApp" {
$items = Get-MgBetaDeviceAppManagementMobileApp @splat
}
"ConfigProfile" {
$items = Get-MgBetaDeviceManagementDeviceConfiguration @splat
}
"AppProtection" {
$items = Get-MgBetaDeviceAppManagementManagedAppPolicy @splat
}
"CompliancePolicy" {
$items = Get-MgBetaDeviceManagementDeviceCompliancePolicy @splat
}
"DiscoveredApps" {
$items = Get-MgBetaDeviceManagementDetectedApp @splat
}
default {
throw "Unsupported ObjectType: $ObjectType"
}
}
if (-not $noSearch) {
return $items | Where-Object { $_.DisplayName -like "*$Search*" }
}
return $items
}
}
# Process block: Runs once for each piped input
process {
$noSearch = IsNullOrWhitespace $Search
# Check required Microsoft Graph and Console GUI commands.
$requiredCommand = switch ($ObjectType) {
"User" { "Get-MgBetaUser" }
"Group" { "Get-MgBetaGroup" }
"Device" { "Get-MgBetaDevice" }
"MobileApp" { "Get-MgBetaDeviceAppManagementMobileApp" }
"ServicePrincipal" { "Get-MgBetaServicePrincipal" }
"SettingsCatalog" { "Get-MgBetaDeviceManagementConfigurationPolicy" }
"ConfigProfile" { "Get-MgBetaDeviceManagementDeviceConfiguration" }
"AppProtection" { "Get-MgBetaDeviceAppManagementManagedAppPolicy" }
"CompliancePolicy" { "Get-MgBetaDeviceManagementDeviceCompliancePolicy" }
"DiscoveredApps" { "Get-MgBetaDeviceManagementManagedDeviceDetectedApp" }
}
if (-not (Get-Command $requiredCommand -ErrorAction SilentlyContinue)) {
Write-Error "The required command '$requiredCommand' for object type '$ObjectType' is not available. Please ensure the Microsoft Graph module is installed and imported."
return
}
if (-not (Get-Command Out-ConsoleGridView -ErrorAction SilentlyContinue)) {
Write-Error "The required command 'Out-ConsoleGridView' is not available. Please ensure the Microsoft.PowerShell.ConsoleGuiTools module is installed and imported."
return
}
# Check if the user has the required permissions
$permissions = switch ($ObjectType) {
"User" {
"User.Read.All"
}
"Group" {
"Group.Read.All"
}
"Device" {
"Device.Read.All"
}
"MobileApp" {
"DeviceManagementApps.Read.All"
}
"ServicePrincipal" {
"Application.Read.All"
}
"SettingsCatalog" {
"DeviceManagementConfiguration.Read.All"
}
"ConfigProfile" {
"DeviceManagementConfiguration.Read.All"
}
"AppProtection" {
"DeviceManagementApps.Read.All"
}
"CompliancePolicy" {
"DeviceManagementConfiguration.Read.All"
}
"DiscoveredApps" {
"DeviceManagementManagedDevices.Read.All"
}
}
$context = Get-MgContext
if (-not $context) {
Write-Error "Not connected to Microsoft Graph. Please connect using Connect-MgGraph."
return
}
if (-not $context.Scopes.Contains($permissions)) {
Write-Error "You do not have the required permission: $permissions. Current scopes: $($context.Scopes -join ', ')"
return
}
# Retrieve data from Microsoft Graph based on object type and search input.
$items = Get-GraphItemsForType -ObjectType $ObjectType -Search $Search
# Define common properties for the grid view.
$commonProperties = switch ($ObjectType) {
"User" { "DisplayName", "Id", "Mail", "accountEnabled", "UserPrincipalName", "UserType" }
"Group" { "DisplayName", "Id", "MailEnabled", "MailNickname", "SecurityEnabled" }
"Device" { "DisplayName", "Id", "OperatingSystem", "OperatingSystemVersion", "Model", "Manufacturer" }
"MobileApp" { "DisplayName", "Id", "Publisher", "AppType", "IsFeatured", "IsAssigned" }
"ServicePrincipal" { "DisplayName", "Id", "AppId" }
"SettingsCatalog" { "Name", "Id", "Description" }
"ConfigProfile" { "DisplayName", "Id", "Description", "Version" }
"AppProtection" { "DisplayName", "Id", "Description", "CreatedDateTime", "LastModifiedDateTime" }
"CompliancePolicy" { "DisplayName", "Id", "Description", "CreatedDateTime", "LastModifiedDateTime" }
"DiscoveredApps" { "DisplayName", "Platform", "Version", "DeviceCount", "Id" }
default { "*" }
}
$gridItems = $items | Select-Object -Property $commonProperties
$title = "Select a $ObjectType" + $(if (-not $noSearch) { " matching '$Search'" } else { "" })
$selections = $gridItems | Out-ConsoleGridView -Title $title -OutputMode "Multiple"
function Get-FullObject {
param(
[string]$Type,
[string]$Id
)
switch ($Type) {
"User" { return Get-MgBetaUser -UserId $Id }
"Group" { return Get-MgBetaGroup -GroupId $Id }
"Device" { return Get-MgBetaDevice -DeviceId $Id }
"MobileApp" { return Get-MgBetaDeviceAppManagementMobileApp -MobileAppId $Id }
"ServicePrincipal" { return Get-MgBetaServicePrincipal -ServicePrincipalId $Id }
"SettingsCatalog" { return Get-MgBetaDeviceManagementConfigurationPolicy -DeviceManagementConfigurationPolicyId $Id }
"ConfigProfile" { return Get-MgBetaDeviceManagementDeviceConfiguration -DeviceConfigurationId $Id }
"AppProtection" { return Get-MgBetaDeviceAppManagementManagedAppPolicy -ManagedAppPolicyId $Id }
"CompliancePolicy" { return Get-MgBetaDeviceManagementDeviceCompliancePolicy -DeviceCompliancePolicyId $Id }
"DiscoveredApps" { return Get-MgBetaDeviceManagementDetectedApp -DetectedAppId $Id }
default { return $null }
}
}
if ($selections) {
# Initialize $Output as an array
$Output = @()
if ($selections -is [System.Collections.IEnumerable] -and $selections.Count -gt 1) {
foreach ($selection in $selections) {
if (-not $selection.PSObject.Properties["Id"]) {
Write-Warning "Selected $ObjectType object does not contain an 'Id' property."
continue
}
$result = Get-FullObject -Type $ObjectType -Id $selection.Id
# Add the current result to the $Output array
$Output += $result
}
} else {
$selection = $selections
if (-not $selection.PSObject.Properties["Id"]) {
Write-Warning "Selected $ObjectType object does not contain an 'Id' property."
return
}
$Output = Get-FullObject -Type $ObjectType -Id $selection.Id
}
switch ($OutputType) {
"JSON" {
$Output | ConvertTo-Json -Depth 10
}
"Grid" {
$Output | Out-ConsoleGridView -Title "Selected $ObjectType Details"
}
"Table" {
$Output | Format-Table -Property * -Force
}
default {
$Output | Format-List -Property * -Force
}
}
} else {
Write-Warning "No items selected."
}
}
# End block (if any cleanup is required)
end {
# Optionally, perform any final actions.
}
}
# Prevent execution if the script is run directly
if ($MyInvocation.InvocationName -eq $MyInvocation.MyCommand.Name) {
Write-Host "`nThis script is intended to be used as a function. Please import it into your PowerShell session."
Write-Host "`nUsage:"
Write-Host "`n. .\Invoke-MgConsoleGuiGraphSearch.ps1`n" -ForegroundColor Yellow
Write-Host "Then call the function with appropriate parameters."
Write-Host "`nExample:"
Write-Host "`nInvoke-MgConsoleGuiGraphSearch -ObjectType User -Search 'Jorge'`n" -ForegroundColor Yellow
Write-Host "Exiting script."
}
Exit 0
# Sample commands:
# Connect to Microsoft Graph with required permissions
$mgParams = @{
Scopes = @(
"User.Read.All"
"Group.Read.All"
"Device.Read.All"
"DeviceManagementApps.Read.All"
"Application.Read.All"
"DeviceManagementConfiguration.Read.All"
)
}
Connect-MgGraph @mgParams
# Search for users
Invoke-MgConsoleGuiGraphSearch -ObjectType User -Search "Jorge"
# List all groups
Invoke-MgConsoleGuiGraphSearch -ObjectType Group
# Search for devices running Windows
Invoke-MgConsoleGuiGraphSearch -ObjectType Device -Search "JORGEASAURUS-28"
# List all mobile apps
Invoke-MgConsoleGuiGraphSearch -ObjectType MobileApp
# Search for service principals
Invoke-MgConsoleGuiGraphSearch -ObjectType ServicePrincipal -Search "Microsoft"
# List settings catalog policies
Invoke-MgConsoleGuiGraphSearch -ObjectType SettingsCatalog
# Search configuration profiles
Invoke-MgConsoleGuiGraphSearch -ObjectType ConfigProfile -Search "MacOS"
# Retrieve App Protection policies with "iOS" in their name and output the result in JSON format.
Invoke-MgConsoleGuiGraphSearch -ObjectType AppProtection -Search "iOS" -OutputType JSON
# Retrieve Compliance policies
Invoke-MgConsoleGuiGraphSearch -ObjectType CompliancePolicy
# Retrieve discovered apps
Invoke-MgConsoleGuiGraphSearch -ObjectType DiscoveredApps -Search "Zune"