22# Requires -Modules Microsoft .Graph .Authentication
33
44<# PSScriptInfo
5- .VERSION 3.4.4
5+ .VERSION 3.4.5
66.GUID c6e25ec6-5787-45ef-95af-8abeb8a17daf
77.AUTHOR ugurk
88.PROJECTURI https://github.com/ugurkocde/IntuneAssignmentChecker
99. DESCRIPTION
1010This script enables IT administrators to efficiently analyze and audit Intune assignments. It checks assignments for specific users, groups, or devices, displays all policies and their assignments, identifies unassigned policies, detects empty groups in assignments, and searches for specific settings across policies.
1111.RELEASENOTES
12+ Version 3.4.5:
13+ - Added tenant switching capability - users can now disconnect and connect to a different tenant without restarting the script
14+ - Menu now displays current connected tenant name and logged-in user
15+ - New menu option [12] to switch between tenants mid-session
16+
1217Version 3.4.4:
1318- Fix Permission Error for Health Scripts
1419
@@ -249,7 +254,7 @@ $certThumbprint = if ($CertificateThumbprint) { $CertificateThumbprint } else {
249254# ###################################################################################################
250255
251256# Version of the local script
252- $localVersion = " 3.4.4 "
257+ $localVersion = " 3.4.5 "
253258
254259Write-Host " 🔍 INTUNE ASSIGNMENT CHECKER" - ForegroundColor Cyan
255260Write-Host " Made by Ugur Koc with" - NoNewline; Write-Host " ❤️ and ☕" - NoNewline
@@ -332,6 +337,9 @@ catch {
332337# Script-level variables
333338$script :GraphEndpoint = $null
334339$script :GraphEnvironment = $null
340+ $script :CurrentTenantId = $null
341+ $script :CurrentTenantName = $null
342+ $script :CurrentUserUPN = $null
335343
336344# Ask user to select the Intune environment
337345function Set-Environment {
@@ -496,6 +504,24 @@ try {
496504 $context = Get-MgContext
497505 $currentPermissions = $context.Scopes
498506
507+ # Store tenant information
508+ if ($context ) {
509+ $script :CurrentTenantId = $context.TenantId
510+ $script :CurrentUserUPN = $context.Account
511+
512+ # Try to get tenant display name
513+ try {
514+ $org = Invoke-MgGraphRequest - Method GET - Uri " $script :GraphEndpoint /v1.0/organization" - ErrorAction SilentlyContinue
515+ if ($org.value -and $org.value.Count -gt 0 ) {
516+ $script :CurrentTenantName = $org.value [0 ].displayName
517+ }
518+ }
519+ catch {
520+ # If we can't get the display name, use tenant ID
521+ $script :CurrentTenantName = $context.TenantId
522+ }
523+ }
524+
499525 Write-Host " Checking required permissions:" - ForegroundColor Cyan
500526 $missingPermissions = @ ()
501527 foreach ($permissionInfo in $requiredPermissions ) {
@@ -1246,7 +1272,26 @@ function Add-AppExportData {
12461272 }
12471273}
12481274
1249- function Show-Menu {
1275+ function Show-Menu {
1276+ # Display current connection status
1277+ if ($script :CurrentTenantName -and $script :CurrentUserUPN ) {
1278+ Write-Host " Connected to: " - ForegroundColor Green - NoNewline
1279+ Write-Host " $script :CurrentTenantName " - ForegroundColor White
1280+ Write-Host " Logged in as: " - ForegroundColor Green - NoNewline
1281+ Write-Host " $script :CurrentUserUPN " - ForegroundColor White
1282+ Write-Host " "
1283+ }
1284+ elseif ($script :CurrentUserUPN ) {
1285+ Write-Host " Logged in as: " - ForegroundColor Green - NoNewline
1286+ Write-Host " $script :CurrentUserUPN " - ForegroundColor White
1287+ Write-Host " "
1288+ }
1289+ else {
1290+ Write-Host " Status: " - ForegroundColor Yellow - NoNewline
1291+ Write-Host " Not Connected" - ForegroundColor Red
1292+ Write-Host " "
1293+ }
1294+
12501295 Write-Host " Assignment Checks:" - ForegroundColor Cyan
12511296 Write-Host " [1] Check User(s) Assignments" - ForegroundColor White
12521297 Write-Host " [2] Check Group(s) Assignments" - ForegroundColor White
@@ -1266,8 +1311,9 @@ function Show-Menu {
12661311 Write-Host " [10] Compare Assignments Between Groups" - ForegroundColor White
12671312 Write-Host " [11] Show All Failed Assignments" - ForegroundColor White
12681313 Write-Host " "
1269-
1314+
12701315 Write-Host " System:" - ForegroundColor Cyan
1316+ Write-Host " [12] Disconnect and Connect to Different Tenant" - ForegroundColor White
12711317 Write-Host " [0] Exit" - ForegroundColor White
12721318 Write-Host " [98] Support the Project 💝" - ForegroundColor Magenta
12731319 Write-Host " [99] Report a Bug or Request a Feature" - ForegroundColor White
@@ -1276,6 +1322,63 @@ function Show-Menu {
12761322 Write-Host " Select an option: " - ForegroundColor Yellow - NoNewline
12771323}
12781324
1325+ # Function to switch tenants
1326+ function Switch-Tenant {
1327+ Write-Host " `n Disconnecting from current tenant..." - ForegroundColor Yellow
1328+
1329+ try {
1330+ # Disconnect from current Graph session
1331+ Disconnect-MgGraph - ErrorAction SilentlyContinue | Out-Null
1332+
1333+ # Clear tenant variables
1334+ $script :CurrentTenantId = $null
1335+ $script :CurrentTenantName = $null
1336+ $script :CurrentUserUPN = $null
1337+
1338+ Write-Host " Disconnected successfully." - ForegroundColor Green
1339+ Write-Host " "
1340+
1341+ # Prompt for new connection
1342+ Write-Host " Please log in to connect to a different tenant..." - ForegroundColor Cyan
1343+
1344+ # Get required permissions
1345+ $permissionsList = ($requiredPermissions | ForEach-Object { $_.Permission }) -join ' , '
1346+
1347+ # Prompt for environment selection
1348+ Set-Environment
1349+
1350+ # Attempt new connection
1351+ $connectionResult = Connect-MgGraph - Scopes $permissionsList - Environment $script :GraphEnvironment - NoWelcome - ErrorAction Stop
1352+
1353+ # Get and store new tenant context
1354+ $context = Get-MgContext
1355+ if ($context ) {
1356+ $script :CurrentTenantId = $context.TenantId
1357+ $script :CurrentUserUPN = $context.Account
1358+
1359+ # Try to get tenant display name
1360+ try {
1361+ $org = Invoke-MgGraphRequest - Method GET - Uri " $script :GraphEndpoint /v1.0/organization" - ErrorAction SilentlyContinue
1362+ if ($org.value -and $org.value.Count -gt 0 ) {
1363+ $script :CurrentTenantName = $org.value [0 ].displayName
1364+ }
1365+ }
1366+ catch {
1367+ # If we can't get the display name, use tenant ID
1368+ $script :CurrentTenantName = $context.TenantId
1369+ }
1370+
1371+ Write-Host " `n Successfully connected to new tenant!" - ForegroundColor Green
1372+ Write-Host " Tenant: $script :CurrentTenantName " - ForegroundColor White
1373+ Write-Host " User: $script :CurrentUserUPN " - ForegroundColor White
1374+ }
1375+ }
1376+ catch {
1377+ Write-Host " Failed to connect to new tenant: $_ " - ForegroundColor Red
1378+ Write-Host " You may need to reconnect manually." - ForegroundColor Yellow
1379+ }
1380+ }
1381+
12791382# Function to handle export
12801383function Export-ResultsIfRequested {
12811384 param (
@@ -7652,6 +7755,10 @@ do {
76527755 }
76537756 }
76547757
7758+ ' 12' {
7759+ Switch-Tenant
7760+ }
7761+
76557762 ' 0' {
76567763 Write-Host " Disconnecting from Microsoft Graph..." - ForegroundColor Yellow
76577764 Disconnect-MgGraph | Out-Null
@@ -7671,7 +7778,7 @@ do {
76717778 Start-Process " https://github.com/ugurkocde/IntuneAssignmentChecker"
76727779 }
76737780 default {
7674- Write-Host " Invalid choice, please select 1-11 , 98, 99, or 0." - ForegroundColor Red
7781+ Write-Host " Invalid choice, please select 1-12 , 98, 99, or 0." - ForegroundColor Red
76757782 }
76767783 }
76777784
0 commit comments