Monday, February 10, 2025

Microsoft Graph, AppProfile, PowerShell, intune to get devices OS versions dump


 
  # You need an appProfile with intune permissions
$tenantId = 'xxxxxxxxxxxx' # You Tenant ID
$appId = 'xxxxxxxxxxxxx'  # Application (client) ID
$appSecret = 'xxxxxxxxxxxxxx' #Value


$body = @{  
    grant_type    = "client_credentials"  
    scope = "https://graph.microsoft.com/.default"  
    client_id     = $appId  
    client_secret = $appSecret  
}  
 
$response = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method Post -Body $body -ContentType "application/x-www-form-urlencoded"  
$token = $response.access_token  

# connecting as the application with the permission on the service
Connect-MgGraph -AccessToken ($Token |ConvertTo-SecureString -AsPlainText -Force)  
#disConnect-MgGraph

 #(get-mgcontext).Scopes
 # (get-mgcontext)
   if (get-mgcontext) {write-host "Connected to O365" -ForegroundColor Green}
   else { write-host "Disconnected from O365"
   break}

    
# Retrieve all devices  
$devices = Get-MgDevice -All  
 
# Check if devices were retrieved  
if ($devices -ne $null) {  
    # Create an array to store device details  
    $deviceDetails = @()  

    
# Filter devices where the OperatingSystem property contains "Windows"  
$devices.Count
    $windowsDevices = $devices | Where-Object {   $_.OperatingSystem -like '*Windows*'   }  
 
# Display the count of devices  
  $devices.Count
    #Write-Host ($devices[44] | Format-List | Out-String)
  $windowsDevices.Count  
    #Write-Host ($windowsDevices[44] | Format-List | Out-String)

# whats available
Write-Host ($windowsDevices[10] | Format-List | Out-String)
 
# Loop through each device and extract details  
foreach ($device in $windowsDevices) {  
    # Use the correct property name for last sync date  
    $formattedDate = if ($device.ApproximateLastSignInDateTime -ne $null) {  
        $device.ApproximateLastSignInDateTime.ToString("yyyy-MM-dd")  
    } else {   "N/A"   }  
      
    # Write the output with the formatted date  
    Write-Host "Name;$($device.DisplayName);OS;$($device.OperatingSystemVersion);LastSync;$formattedDate;DeviceID;$($device.Id)"  
        }  

  }

No comments:

Blog Archive