Skip to content

Commit

Permalink
Managed Identity Login
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianSLZ committed May 22, 2023
1 parent 092acf3 commit 5950c27
Showing 1 changed file with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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'))"
Expand Down Expand Up @@ -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" = "<http://schema.org/extensions>"
"summary" = "You have $($report.count) Inactive Devices which haven't have contatc in the last $inactiveDays"
"themeColor" = '0078D7'
"title" = "Inactive Devices ($($report.count))"
"text" = "<h1>Inactive Devices for $inactiveDays+ days</h1>
<pre>$($report | Format-Table DeviceName, LastSyncDateTime | Out-String)</pre>"
} | ConvertTo-Json

# Message JSON
$Message_Json = [PSCustomObject][Ordered]@{
"@type" = "MessageCard"
"@context" = "<http://schema.org/extensions>"
"summary" = "You have $($report.count) Inactive Devices which haven't have contatc in the last $inactiveDays"
"themeColor" = '0078D7'
"title" = "Inactive Devices ($($report.count))"
"text" = "<h1>Inactive Devices for $inactiveDays+ days</h1>
<pre>$($report | Format-Table DeviceName, LastSyncDateTime | Out-String)</pre>"
} | 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

0 comments on commit 5950c27

Please sign in to comment.