Intune-Aufgaben mit Runbooks automatisieren

Erfahre, wie du Intune mit Azure Automation Runbooks, Managed Identity, Microsoft Graph, Zeitplänen und Source Control automatisierst.

Intune-Aufgaben mit Runbooks automatisieren

Wie ihr alle wisst, bin ich ein sehr, sehr großer Fan von Automatisierung. Das Gute daran ist, dass es in fast allen Microsoft-Produkten Schnittstellen gibt, um genau das zu tun. Das gilt auch für Intune. In einem meiner letzten Blogs habe ich darüber geschrieben, wie man PowerShell nutzt, um Aufgaben in Intune zu automatisieren. Ich habe dabei auch Azure Automation erwähnt. In diesem Blog möchte ich tiefer in das Thema einsteigen und erklären, wie du Azure Automation nutzen kannst, um wiederkehrende Prozesse zu automatisieren.

Einrichtung eines Azure Automation Runbooks für Intune-Aufgaben

Inhalt

  1. Inhalt
  2. Was ist Azure Automation?
  3. Wie erstellt man ein Automation Account und ein Runbook?
    1. Automation Account erstellen
    2. Das Runbook erstellen
  4. Wie authentifiziert man sich mit einer Managed Identity an Graph?
  5. Wie authentifiziert man sich an einem Storage Account?
  6. Wie authentifiziert man sich an einem Key Vault?
  7. Wie schreibe ich ein PowerShell-Skript?
    1. Skript-Beispiel
  8. Wie testet man ein Skript
  9. Wie veröffentlicht man ein Skript und erstellt einen Zeitplan
  10. Wie aktiviert man Source Control
  11. Weitere Quellen

Was ist Azure Automation?

Azure Automation ist ein leistungsstarker Azure-Dienst, mit dem du sich wiederholende und zeitaufwändige Aufgaben in deinen Azure- und On-Premises-Umgebungen automatisieren kannst. Azure Automation Runbooks sind eine wesentliche Komponente dieses Dienstes. Sie ermöglichen es dir, Skripte zu erstellen, auszuführen und zu verwalten, die Prozesse automatisieren – etwa das Verwalten von Intune-Geräten, Bereinigungen, das Erstellen von Gruppen und vieles mehr – unter Verwendung der Microsoft Graph API. Runbooks können in verschiedenen Skriptsprachen geschrieben werden, darunter PowerShell, Python und Bash, wodurch sich problemlos automatisierte Lösungen erstellen lassen, die genau auf deine Bedürfnisse zugeschnitten sind.

Wie erstellt man ein Automation Account und ein Runbook?

Automation Account erstellen

  • Suche nach Automation Accounts

Einrichtung eines Azure Automation Runbooks zur Automatisierung von Intune-Aufgaben

  • Klicke auf + Create

Azure Automation Runbook zur Automatisierung von Intune-Aufgaben

  • Wähle ein Subscriptionund eine Resource group
  • Gib einen Account-Namen ein und wähle eine Region
  • Klicke auf Next

Azure Automation Runbook zur Automatisierung von Intune-Aufgaben

  • Klicke auf Next

Einrichtung eines Azure Automation Runbooks zur Automatisierung von Intune-Aufgaben

  • Klicke auf Next -> Next -> Create

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

Das Runbook erstellen

  • Öffne das Automation Account
  • Wähle Runbooks
  • Klicke auf + Create a runbook

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Gib einen Namen ein
  • Wähle PowerShell als Runbook-Typ
  • Wähle die Runtime-Version
  • Klicke auf Create

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

Wie authentifiziert man sich mit einer Managed Identity an Graph?

  • Zunächst musst du die Managed Identity für dein Automation Account aktivieren

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Öffne eine Azure PowerShell oder eine PowerShell lokal auf deinem Gerät

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Führe den folgenden Code aus:
Install-Module Microsoft.Graph -Scope CurrentUser

Connect-MgGraph -Scopes Application.Read.All, AppRoleAssignment.ReadWrite.All, RoleManagement.ReadWrite.Directory

$managedIdentityId = "Managed Identity Object ID"
$roleName = "DeviceManagementApps.Read.All"

$msgraph = Get-MgServicePrincipal -Filter "AppId eq '00000003-0000-0000-c000-000000000000'"
$role = $Msgraph.AppRoles| Where-Object {$_.Value -eq $roleName}

New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $managedIdentityId -PrincipalId $managedIdentityId -ResourceId $msgraph.Id -AppRoleId $role.Id

Disconnect-MgGraph

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Gehe zurück und wähle Modules
  • Klicke auf + Add a module

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Wähle PowerShell Galary

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Suche und installiere das Graph.Authentication-Modul

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Jetzt kannst du die Managed Identity nutzen, um dich auf sehr sichere Weise an Graph zu authentifizieren
  • Mit diesem Code kannst du dich nun an Graph authentifizieren
Connect-AzAccount -Identity
$token = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com"
Connect-MgGraph -AccessToken $token.Token

Wie authentifiziert man sich an einem Storage Account?

  • Die oben beschriebenen Schritte zur Aktivierung der system-assigned Identity sind Voraussetzung
  • Stelle sicher, dass du ein Storage Account hast. Falls nicht, erstelle eines
  • Öffne eine Azure PowerShell oder eine PowerShell lokal auf deinem Gerät

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Führe den folgenden Code aus:
# Define variables for your resources
$managedIdentityId = "Managed Identity Object ID"
$resourceGroupName = "<Your-Resource-Group-Name>"
$storageAccountName = "<Your-Storage-Account-Name>"

$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName

New-AzRoleAssignment -ObjectId $managedIdentityId -RoleDefinitionName "Storage Blob Data Contributor" -Scope $storageAccount.Id
  • Jetzt kannst du mit diesem Befehl einen Storage-Account-Kontext abrufen:
$Context = New-AzStorageContext -StorageAccountName $storageAccountName

Wie authentifiziert man sich an einem Key Vault?

  • Die oben beschriebenen Schritte zur Aktivierung der system-assigned Identity sind Voraussetzung
  • Stelle sicher, dass du ein Storage Account hast. Falls nicht, erstelle eines
  • Öffne eine Azure PowerShell oder eine PowerShell lokal auf deinem Gerät

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Führe den folgenden Code aus:
# Define variables for your resources
$managedIdentityId = "Managed Identity Object ID"
$resourceGroupName = "<Your-Resource-Group-Name>"
$keyVaultName = "<Your-Storage-Account-Name>"

$keyVault = Get-AzKeyVault -ResourceGroupName $resourceGroupName -Name $keyVaultName

New-AzRoleAssignment -ObjectId $managedIdentityId -RoleDefinitionName "Reader" -Scope $keyVault.ResourceId

Set-AzKeyVaultAccessPolicy -VaultName $keyVaultName -ResourceGroupName $resourceGroupName -ObjectId $managedIdentityId -PermissionsToSecrets get
  • Jetzt kannst du das Secret mit dem folgenden Befehl auslesen:
Connect-AzAccount -Identity
$secret = Get-AzKeyVaultSecret -VaultName "VaultName" -Name "SecretName" -AsPlainText

Wie schreibe ich ein PowerShell-Skript?

Schau dir diesen Blogbeitrag an, in dem ich im Detail erkläre, wie man PowerShell nutzt, um Aufgaben in Intune zu automatisieren

https://jannikreinhard.com/how-to-create-powershell-script-to-automate-tasks-in-intune/

Skript-Beispiel

Hier ist ein Beispielskript, wie man alle erkannten Apps aus Intune mithilfe von Azure Automation Runbooks meldet und sich mit einer Managed Identity authentifiziert.

  • Installiere die Module Microsoft.Graph.Reports und Microsoft.PowerShell.Archive
  • Erstelle ein Runbook mit der Powershell-7.2-Runtime
<#
Version: 1.0
Author: Jannik Reinhard (jannikreinhard.com)
Script: Get-GraphExportApiReport
Description:
Get a CSV Report from the Graph API
Release notes:
Version 1.0: Init
#>

# Authenticate and connect to Microsoft Graph
Connect-AzAccount -Identity
$token = Get-AzAccessToken -ResourceUrl "https://graph.microsoft.com"
Connect-MgGraph -AccessToken $token.Token

$reportName = 'DetectedAppsRawData'
$fileName = "intuneExport.csv"
$storageAccountName = ""
$containerName = ""

$body = @"
{
    "reportName": "$reportName",
    "localizationType": "LocalizedValuesAsAdditionalColumn"
}
"@


$id = (Invoke-MgGraphRequest -Method POST -Body $body -Uri https://graph.microsoft.com/beta/deviceManagement/reports/exportJobs).id
$status = (Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/reports/exportJobs('$id')" -Method GET).status

while (-not ($status -eq 'completed')) {
    $response = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/reports/exportJobs('$id')" -Method Get
    $status = ($response).status
    Start-Sleep -Seconds 2
}

$localFilePath = "./$fileName"
$localFilePathZip = "./$fileName.zip"
Invoke-WebRequest -Uri $response.url -OutFile $localFilePathZip
Expand-Archive $localFilePathZip -DestinationPath $localFilePath

$Context = New-AzStorageContext -StorageAccountName $storageAccountName

$file = Get-ChildItem -Path $localFilePath -Force -Recurse -File | Select-Object -First 1
Set-AzStorageBlobContent -Force -File $file -Container $containerName -Blob $localFilePath -Context $Context -StandardBlobTier 'Hot'

Wie testet man ein Skript

  • Erstelle ein neues Runbook wie oben beschrieben
  • Füge das Skript in das Runbook ein
  • Klicke auf Test pane

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Klicke auf Start

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Prüfe, ob die Ausführung erfolgreich ist

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

Wie veröffentlicht man ein Skript und erstellt einen Zeitplan

  • Klicke auf Publish

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Navigiere zu Schedules und klicke auf + Add a schedule

Intune-Aufgaben mit Azure Automation Runbooks automatisieren Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Klicke auf Link to schedule und füge den erstellten Zeitplan hinzu

Intune-Aufgaben mit Azure Automation Runbooks automatisieren Intune-Aufgaben mit Azure Automation Runbooks automatisieren

Wie aktiviert man Source Control

Der erste Schritt besteht darin, der Managed Identity des Automation Accounts Contributor-Rechte auf sich selbst zuzuweisen

  • Öffne eine Azure PowerShell oder eine PowerShell lokal auf deinem Gerät

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Führe den folgenden Code aus:
# Define variables for your resources
$managedIdentityId = "Managed Identity Object ID"
$resourceGroupName = "<Your-Resource-Group-Name>"
$automationAccountName = "<Your-Automation-Account-Name>"

$automationAccount = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.Automation/automationAccounts" -Name $automationAccountName

New-AzRoleAssignment -ObjectId $managedIdentityId -RoleDefinitionName "Contributor" -Scope $automationAccountName.Id
  • Erstelle ein Azure DevOps Projekt und ein Repository

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Navigiere zu Source control und klicke auf +Add

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Wähle deine Organization und authentifiziere dich
  • Wähle das Repository und den Branch
  • Klicke auf Save

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

  • Lege ein Powershell-Skript im Repository ab
  • Klicke auf den Source-control-Eintrag und klicke auf Start sync
  • Prüfe den Status

Intune-Aufgaben mit Azure Automation Runbooks automatisieren

Weitere Quellen

Kommentare

Shashi Devadiga

nice one! Thank you REINHARD.

Tracking Windows 11 Upgrades with Azure Automation and Intune &#8211; Modern Device Management

[…] have already written a very detailed blog on how to get started with Azure Automamtion and Runbooks. It’s best to read this blog first. You have to make sure that the Graph.Authentication […]

Mastering Intune Reporting and Analytics &#8211; Modern Device Management

[…] posts where I explain how to start with PowerShell to export data from Intune and how to start with azure automation to create and schedule […]

Kommentar schreiben

Kommentare werden vor der Veröffentlichung geprüft. Deine E-Mail-Adresse bleibt privat.

Mit deiner Zustimmung erfasst Microsoft Clarity maskierte Interaktionen zur Verbesserung der Website. Eine Ablehnung hat keinen Einfluss auf den Zugang.

Datenschutzdetails