diff --git a/Automations/AZ Automation with Managed Identity/MgId_getOldDevices.ps1 b/Automations/AZ Automation with Managed Identity/MgId_getOldDevices.ps1 index 1ffec76..b83e8bb 100644 --- a/Automations/AZ Automation with Managed Identity/MgId_getOldDevices.ps1 +++ b/Automations/AZ Automation with Managed Identity/MgId_getOldDevices.ps1 @@ -1,11 +1,14 @@ -# Conenction with Managed Identity -Connect-MgGraph -Identity - -Connect-MgGraph -Scopes "DeviceManagementManagedDevices.Read.All" +# Connect to Microsoft Graph within Azure Automation (Microsoft Graph PowerShell v1) +Connect-AzAccount -Identity +$token = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com" +Connect-MgGraph -AccessToken $token.Token # Define device age to include $inactiveDays = "180" +# YOUR Webhook URL +$WebHookURL = "https://xxxx.webhook.office.com/someID..." + # Construct the Graph API request URI $graphUri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices" $filter = "lastSyncDateTime le $((Get-Date).AddDays(-$inactiveDays).ToString('yyyy-MM-ddTHH:mm:ssZ'))" @@ -33,29 +36,28 @@ foreach ($device in $response.value) { $report -############################################################################### - -# YOUR Webhook URL -$WebHookURL = "https://xxxx.webhook.office.com/someID..." +if($report){ + # Message JSON + $Message_Json = [PSCustomObject][Ordered]@{ + "@type" = "MessageCard" + "@context" = "" + "summary" = "You have $($report.count) Inactive Devices which haven't have contatc in the last $inactiveDays" + "themeColor" = '0078D7' + "title" = "Inactive Devices ($($report.count))" + "text" = "

Inactive Devices for $inactiveDays+ days

+
$($report | Format-Table DeviceName, LastSyncDateTime | Out-String)
" + } | ConvertTo-Json -# Message JSON -$Message_Json = [PSCustomObject][Ordered]@{ - "@type" = "MessageCard" - "@context" = "" - "summary" = "You have $($report.count) Inactive Devices which haven't have contatc in the last $inactiveDays" - "themeColor" = '0078D7' - "title" = "Inactive Devices ($($report.count))" - "text" = "

Inactive Devices for $inactiveDays+ days

-
$($report | Format-Table DeviceName, LastSyncDateTime | Out-String)
" -} | ConvertTo-Json + $parameters = @{ + "URI" = $WebHookURL + "Method" = 'POST' + "Body" = $Message_Json + "ContentType" = 'application/json' + } -$parameters = @{ - "URI" = $WebHookURL - "Method" = 'POST' - "Body" = $Message_Json - "ContentType" = 'application/json' + Invoke-RestMethod @parameters } -Invoke-RestMethod @parameters +