<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Graph API on Alfred van Ster</title><link>https://avanster.tech/tags/graph-api/</link><description>Recent content in Graph API on Alfred van Ster</description><generator>Hugo -- 0.161.1</generator><language>en-us</language><lastBuildDate>Sun, 03 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://avanster.tech/tags/graph-api/index.xml" rel="self" type="application/rss+xml"/><item><title>Cloud Identity: Entra ID Stale Guest Reaper</title><link>https://avanster.tech/library/script-entra-guest-reaper/</link><pubDate>Sun, 03 May 2026 00:00:00 +0000</pubDate><guid>https://avanster.tech/library/script-entra-guest-reaper/</guid><description>&lt;ol&gt;
&lt;li&gt;The Workflow&lt;/li&gt;
&lt;li&gt;The Implementation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;External guest accounts are frequently provisioned for vendors but rarely decommissioned, creating a lingering attack surface. This script uses the Microsoft Graph API to identify and remove guest accounts that have been inactive for over 90 days.&lt;/p&gt;
&lt;h3 id="1-the-workflow"&gt;1. The Workflow&lt;/h3&gt;
&lt;p&gt;The script performs the following steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Authentication:&lt;/strong&gt; Uses App Registration credentials to silently authenticate to MS Graph.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Filtering:&lt;/strong&gt; Queries all users where &lt;code&gt;userType&lt;/code&gt; equals &amp;ldquo;Guest&amp;rdquo;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Evaluation:&lt;/strong&gt; Checks the &lt;code&gt;signInActivity&lt;/code&gt; property against a 90-day threshold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Purge:&lt;/strong&gt; Identifies stale guest accounts to close the security loop.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="2-the-implementation"&gt;2. The Implementation&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-powershell" data-lang="powershell"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ThresholdDate = (Get-Date).AddDays(&lt;span style="color:#ae81ff"&gt;-90&lt;/span&gt;).ToString(&lt;span style="color:#e6db74"&gt;&amp;#34;yyyy-MM-ddTHH:mm:ssZ&amp;#34;&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$Url = &lt;span style="color:#e6db74"&gt;&amp;#34;[https://graph.microsoft.com/beta/users](https://graph.microsoft.com/beta/users)?&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;`$&lt;/span&gt;&lt;span style="color:#e6db74"&gt;filter=userType eq &amp;#39;Guest&amp;#39;&amp;amp;&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;`$&lt;/span&gt;&lt;span style="color:#e6db74"&gt;select=displayName,userPrincipalName,signInActivity&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$Guests = Invoke-RestMethod -Uri $Url -Method Get -Headers $Headers
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;foreach&lt;/span&gt; ($Guest &lt;span style="color:#66d9ef"&gt;in&lt;/span&gt; $Guests.value) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;if&lt;/span&gt; ([&lt;span style="color:#66d9ef"&gt;string&lt;/span&gt;]::IsNullOrEmpty($Guest.signInActivity.lastSignInDateTime) &lt;span style="color:#f92672"&gt;-or&lt;/span&gt; ($Guest.signInActivity.lastSignInDateTime &lt;span style="color:#f92672"&gt;-lt&lt;/span&gt; $ThresholdDate)) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Write-Host &lt;span style="color:#e6db74"&gt;&amp;#34;Stale Guest Found: &lt;/span&gt;$($Guest.displayName)&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item></channel></rss>