Microsoft Intune Automation Scripts
Automate and manage Microsoft Intune activities.
### FILE: index.php
<?php
// Ensure user is authenticated and has a valid access token
if (!isset($_SESSION['ms_access_token'])) {
echo 'Access token is missing.';
exit;
}
// Fetch device compliance policies from Microsoft Graph
function fetchDeviceCompliancePolicies($ms, $accessToken) {
try {
$endpoint = 'https://graph.microsoft.com/v1.0/deviceManagement/deviceCompliancePolicies';
$response = $ms->graphCall($endpoint, $accessToken, 'GET');
return $response;
} catch (Exception $e) {
echo 'Error fetching compliance policies: ' . $e->getMessage();
return null;
}
}
// Retrieve the list
$compliancePolicies = fetchDeviceCompliancePolicies($ms, $_SESSION['ms_access_token']);
// Render premium card with fetched data
echo render_premium_card('Device Compliance Policies', json_encode($compliancePolicies));
?>
### FILE: scripts/Automation.ps1
<#
.SYNOPSIS
Retrieves and exports all devices under Intune management.
.DESCRIPTION
This script connects to Microsoft Graph using the MgGraph module, retrieves all managed devices, and exports the list to a CSV file.
.EXAMPLE
.\Automation.ps1
.NOTES
Author: Souhaiel Morhag
Company: MSEndpoint.com
Blog: https://msendpoint.com
Academy: https://app.msendpoint.com/academy
LinkedIn: https://linkedin.com/in/souhaiel-morhag
GitHub: https://github.com/Msendpoint
License: MIT
#>
try {
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "DeviceManagementConfiguration.Read.All"
# Retrieve the list of managed devices
$devices = Get-MgDeviceManagementManagedDevice
# Export the devices to a CSV file
$devices | Export-Csv -Path "intune_devices.csv" -NoTypeInformation
Write-Output "Device list exported successfully to intune_devices.csv."
} catch {
Write-Error "Failed to export device list: $_.Exception.Message"
}
⚠️ Always review and validate PowerShell scripts in a non-production pilot group before tenant-wide deployment.