Close Menu
Peter Klapwijk – In The Cloud 24-7Peter Klapwijk – In The Cloud 24-7
    Facebook X (Twitter) Instagram
    Peter Klapwijk – In The Cloud 24-7Peter Klapwijk – In The Cloud 24-7
    • Home
    • Intune
    • Windows
      • Modern Workplace
    • macOS
    • Android
    • iOS
    • Automation
      • Logic Apps
      • Intune Monitoring
      • GitHub
    • Security
      • Passwordless
      • Security
    • Speaking
    • About me
    Peter Klapwijk – In The Cloud 24-7Peter Klapwijk – In The Cloud 24-7
    Home»Automation»Windows Autopilot lifecycle automation with Logic Apps – Part2
    Automation

    Windows Autopilot lifecycle automation with Logic Apps – Part2

    Peter KlapwijkBy Peter KlapwijkMay 18, 2022Updated:February 14, 202515 Mins Read

    About a year ago I wrote this article regarding a Logic Apps flow that I created to delegate some permissions to local IT to perform lifecycle management on Autopilot registered devices.

    By using a SharePoint List, local IT can delete Autopilot device registrations (in case of a repair or retirement of the device) or change the Group TAG (which changes the deployment profile based on a dynamic AAD group). For the retire part of the flow, it removes the device object in Intune and after that, it deletes the Autopilot registration. What it did not do, was clean up Azure AD (AAD) by also removing the device object from AAD. I did not implement that part as a delete action via Graph API does not support application permissions and application permissions are what my HTTP actions use to perform several Graph queries. But since that time, I learned how to use Managed Identities for my HTTP actions (which still uses applications permissions), but thanks to some great community members #CommunityRocks I also learned that we can assign (custom) Azure AD roles to a Managed Identity. And if that is possible, we can use a Graph call to delete the device object in Azure AD with a Logic Apps flow!

    Solution and requirements

    On the solution and requirements, we can be short, these are the same as described in part 1, with one additional permission (Azure AD role), to delete Azure AD Devices.

    The biggest difference is that I switched from an Azure App registration to a Managed Identity (yes, still need to describe that further). So if you have implemented the first part of the flow, but didn’t change authentication to a Managed Identity yet, you should first do that.

    Assigning an Azure AD role to a Managed Identity works the exact same way as we assign the permission to a normal user account (not using PIM).
    We can assign a built-in role, such as the Cloud device administrator role, but that role also holds other permissions.
    We can also create a custom Azure AD role, which only holds the microsoft.directory/devices/delete permission.

    As you can see we can just search on the name of our Managed Identity to assign the role to the Managed Identity.

    Expand our Logic Apps flow

    Sign in to the Azure portal and open the Logic App service. I suggest cloning your existing Logic App, so you can leave that up and running, while you implement the new actions.

    If you followed my previous blog post, you have a case for the retirement of devices. Under this case, we have a case for (Intune) enrolled devices and devices which are not enrolled (not contacted). Under both of these cases, we will add additional actions to query via Graph if a device exists in Azure AD or not. If we don’t first determine if a device exists (sometimes an Autopilot registration shows no associated device object), but immediately perform a delete action, the flow will fail when the object does not exist.

    We start with the enrolled case. We add a new HTTP Action between the second HTTP Delete and SharePoint Get Item actions.

    Choose GET as Method.
    As URI enter:

    https://graph.microsoft.com/v1.0/devices?$search="deviceId:[azureAdDeviceId]"&$select=displayName,id

    Replace [azureAdDeviceId] with the azureAdDeviceId from the Dynamic content list.

    As Header enter:
    ConsistencyLevel eventual

    Add the Managed Identity under Authentication.

    Next, we add a Parse JSON action.
    As content add Body from the HTTP Action. As Schema add:

    {
        "properties": {
            "@@odata.context": {
                "type": "string"
            },
            "value": {
                "items": {
                    "properties": {
                        "displayName": {
                            "type": "string"
                        },
                        "id": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "displayName",
                        "id"
                    ],
                    "type": "object"
                },
                "type": "array"
            }
        },
        "type": "object"
    }

    We use a Condition action with an expression that checks the length of the value. If no AAD device object is found, the value returned is empty (and length is 0) and the condition is false. If an AAD device object is found, the condition is true, under which we perform the HTTP Delete action.

    Add a Condition and in the left text box enter this value: as Expression

    length(body('Parse_JSON_GET_AAD_Device_enrolled')?['value'])

    Note Parse_JSON_GET_AAD_Device_enrolled in the expression is the name of the previous Parse JSON action, with underscores between the words.

    Choose is not equal to from the drop-down list and enter 0 in the right text box.

    Under True, we add an HTTP action.
    Choose DELETE as Method.
    As URI enter:

    https://graph.microsoft.com/v1.0/devices/[ID]

    Replace [ID] with the ID from the Dynamic content list (from the last Parse JSON action).
    This will add the HTTP action in a For each action.

    This is what our enrolled case now looks like with the additional actions to delete the device object.

    We need to add the exact same actions in the not contacted case.
    Add the HTTP action between the HTTP delete and SharePoint Get Item action.

    The HTTP Action is followed by a Parse JSON action and Condition.

    As Expression add:

    length(body('Parse_JSON_GET_AAD_Device_notContacted')?['value'])

    And under True we add the HTTP action to delete de AAD Device object.

    And that is all for expanding our existing Logic App.

    Thanks for reading and if you have any questions, leave a comment below.

    Autopilot Flow Graph Graph API Intune Logic Apps MEM Microsoft Endpoint Manager Power Automate PowerApps Windows Autopilot
    Share. Facebook Twitter LinkedIn Email WhatsApp
    Peter Klapwijk
    • Website
    • X (Twitter)
    • LinkedIn

    Peter is a Security (Intune) MVP since 2020 and is working as Modern Workplace Engineer at Wortell in The Netherlands. He has more than 15 years of experience in IT, with a strong focus on Microsoft technologies like Microsoft Intune, Windows, and (low-code) automation.

    Related Posts

    Export Endpoint Analytics Proactive remediation data with Logic Apps

    April 19, 2021

    MEM Monitoring: Get your Windows Autopilot deployment events in a Teams channel with Logic Apps – Part 1

    April 16, 2021

    Authenticate with a FIDO2 security key for Windows Autopilot enrollment

    February 27, 2021
    View 1 Comment

    1 Comment

    1. Jack on October 11, 2022 09:46

      nice setup

      Reply
    Leave A Reply Cancel Reply

    Peter Klapwijk

    Hi! Welcome to my blog post.
    I hope you enjoy reading my articles.

    Hit the About Me button to get in contact with me or leave a comment.

    Awards
    Sponsor
    Latest Posts

    Managing Windows 365 Link devices with Intune

    October 24, 2025

    Hard drive and partitions are not shown while installing Windows

    October 14, 2025

    Intune compliance for Windows 365 Cloud PCs

    September 12, 2025

    Intune connector for Active Directory configuration error

    August 29, 2025
    follow me
    • Twitter 4.8K
    • LinkedIn 6.1K
    • YouTube
    • Bluesky 1.5K
    Tags
    Administrative Templates Android Automation Autopilot Azure Azure AD Browser Conditional Access Edge EMS Exchange Online Feitian FIDO2 Flow Graph Graph API Identity Management Intune Intune Monitoring iOS KIOSK Logic Apps macOS MEM MEMMonitoring Microsoft 365 Microsoft Defender Microsoft Edge Microsoft Endpoint Manager Modern Workplace Office 365 OneDrive for Business Outlook Passwordless PowerApps Power Automate Security SharePoint Online Windows Windows 10 Windows10 Windows 11 Windows 365 Windows Autopilot Windows Update
    Awards
    Sponsor
    Follow me on Twitter
    Tweets by inthecloud_247
    Tags
    Administrative Templates Android Automation Autopilot Azure Azure AD Browser Conditional Access Edge EMS Exchange Online Feitian FIDO2 Flow Graph Graph API Identity Management Intune Intune Monitoring iOS KIOSK Logic Apps macOS MEM MEMMonitoring Microsoft 365 Microsoft Defender Microsoft Edge Microsoft Endpoint Manager Modern Workplace Office 365 OneDrive for Business Outlook Passwordless PowerApps Power Automate Security SharePoint Online Windows Windows 10 Windows10 Windows 11 Windows 365 Windows Autopilot Windows Update
    Archives
    Peter Klapwijk

    Hi! Welcome to my blog post.
    I hope you enjoy reading my articles.

    Hit the About Me button to get in contact with me or leave a comment.

    Copy right

    This information is provided “AS IS” with no warranties, confers no rights and is not supported by the authors, or In The Cloud 24-7.

     

    Copyright © 2025 by In The Cloud 24-7/ Peter Klapwijk. All rights reserved, No part of the information on this web site may be reproduced or posted in any form or by any means without the prior written permission of the publisher.

    Shorthand; Don’t pass off my work as yours, it’s not nice.

    Recent Comments
    • Luis on Application installation issues; Download pending
    • AndrewWak on Issues syncing SharePoint Online libraries with OneDrive for Mac
    • Peter Klapwijk on Deploy Microsoft Defender updates in deployment rings
    • Magnus on Deploy Microsoft Defender updates in deployment rings
    • Gertjan Jongeneel on Add an Azure AD group to the local administrators group with Microsoft Intune
    most popular

    Application installation issues; Download pending

    October 1, 2024

    How to change the Windows 11 language with Intune

    November 11, 2022

    Restrict which users can logon into a Windows 10 device with Microsoft Intune

    April 11, 2020

    How I solved a strange Kerberos issue

    December 12, 2024
    Recent Comments
    • Luis on Application installation issues; Download pending
    • AndrewWak on Issues syncing SharePoint Online libraries with OneDrive for Mac
    • Peter Klapwijk on Deploy Microsoft Defender updates in deployment rings
    • Magnus on Deploy Microsoft Defender updates in deployment rings
    • Gertjan Jongeneel on Add an Azure AD group to the local administrators group with Microsoft Intune
    Copy right

    This information is provided “AS IS” with no warranties, confers no rights and is not supported by the authors, or In The Cloud 24-7.

    Copyright © 2023 by In The Cloud 24-7/ Peter Klapwijk. All rights reserved. No part of the information on this web site may be reproduced or posted in any form or by any means without the prior written permission of the publisher.

    Shorthand: Don’t pass off my work as yours, it’s not nice.

    Peter Klapwijk – In The Cloud 24-7
    X (Twitter) LinkedIn YouTube RSS Bluesky
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.

    Manage Cookie Consent
    To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
    Functional Always active
    The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
    Preferences
    The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
    Statistics
    The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
    Marketing
    The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
    Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
    View preferences
    {title} {title} {title}