@@ -23,6 +23,13 @@ function Invoke-MsIdAgentIdInteractive {
2323 $october1_2025 = [DateTime ]::new(2025 , 10 , 1 , 0 , 0 , 0 )
2424 $blueprintNumber = [int ]((Get-Date ) - $october1_2025 ).TotalSeconds
2525
26+ Write-Host " Connecting to Microsoft Graph with all the permissions needed to create and manage" - ForegroundColor Yellow
27+ Write-Host " Agent Identity Blueprints and Agent Users" - ForegroundColor Yellow
28+
29+ # Ensure required modules are available and connect as admin
30+ Connect-MsIdEntraAsUser - Scopes @ (' AgentIdentityBlueprint.Create' , ' AgentIdentityBlueprintPrincipal.Create' , ' AppRoleAssignment.ReadWrite.All' , ' Application.ReadWrite.All' , ' User.ReadWrite.All' )
31+
32+
2633 $bluePrintDisplayName = Read-Host " Enter a display name for the Agent Identity Blueprint (or press Enter for default)"
2734 if (-not $bluePrintDisplayName -or $bluePrintDisplayName.Trim () -eq " " ) {
2835 $bluePrintDisplayName = " Agent Identity Blueprint Example $blueprintNumber "
@@ -32,24 +39,47 @@ function Invoke-MsIdAgentIdInteractive {
3239 # Get current user as sponsor
3340 try {
3441 $currentUserUpn = (Get-MgContext ).Account
35- $currentUserId = (Get-MgUser - UserId $currentUserUpn ).Id
42+ # Get user's OID directly using their UPN
43+ $currentUser = Get-MgUser - Filter " userPrincipalName eq '$currentUserUpn '" - Property Id
44+ $currentUserId = $currentUser.Id
3645 }
3746 catch {
3847 $currentUserUpn = $null
3948 $currentUserId = $null
4049 }
50+
4151 if ($currentUserUpn ) {
4252 $useCurrentUserId = Read-Host " Use current user ($currentUserUpn ) as sponsor? (y/n)"
4353 if ($null -eq $useCurrentUserId -or $useCurrentUserId -eq " y" ) {
4454 Write-Host " Using current user as default sponsor: $currentUserUpn " - ForegroundColor Gray
4555 $SponsorUserIds = @ ($currentUserId )
56+ $useSponsor = $true
57+ } else {
58+ $useSponsor = $false
4659 }
60+ } else {
61+ $useSponsor = $false
4762 }
4863
4964 # Step 1: Create Agent Identity Blueprint with all parameters (no prompting)
50- $blueprint1 = New-MsIdAgentIdentityBlueprint - DisplayName $bluePrintDisplayName - SponsorUserIds $SponsorUserIds
65+ try {
66+ if ($useSponsor ) {
67+ $blueprint1 = New-MsIdAgentIdentityBlueprint - DisplayName $bluePrintDisplayName - SponsorUserIds $SponsorUserIds
68+ } else {
69+ $blueprint1 = New-MsIdAgentIdentityBlueprint - DisplayName $bluePrintDisplayName
70+ }
5171
52- Write-Host " Created Blueprint ID: $ ( $blueprint1.AgentBlueprintId ) " - ForegroundColor Green
72+ if ($blueprint1 ) {
73+ Write-Host " Created Blueprint ID: $blueprint1 " - ForegroundColor Green
74+ } else {
75+ Write-Error " Failed to create Agent Identity Blueprint - no ID returned"
76+ return
77+ }
78+ }
79+ catch {
80+ Write-Error " Failed to create Agent Identity Blueprint: $_ "
81+ return
82+ }
5383 Write-Host " "
5484
5585 # ===================================================================
@@ -112,7 +142,7 @@ function Invoke-MsIdAgentIdInteractive {
112142
113143 # Step 4: Configure inheritable permissions (what permissions agent users will get)
114144 $inheritablePerms = Add-MsIdInheritablePermissionsToAgentIdentityBlueprint - Scopes @ (" user.read" , " mail.read" , " calendars.read" )
115- Write-Host " Configured inheritable permissions: $ ( $inheritablePerms.Scopes -join ' , ' ) " - ForegroundColor Cyan
145+ Write-Host " Configured inheritable permissions: $ ( $inheritablePerms.InheritableScopes -join ' , ' ) " - ForegroundColor Cyan
116146 }
117147 else {
118148 Write-Host " Skipping inheritable permissions configuration." - ForegroundColor Gray
@@ -156,7 +186,7 @@ function Invoke-MsIdAgentIdInteractive {
156186
157187 # Step 6: Create the service principal for the blueprint
158188 $principal1 = New-MsIdAgentIdentityBlueprintPrincipal
159- Write-Host " Created Service Principal ID: $ ( $principal1.ServicePrincipalId ) " - ForegroundColor Green
189+ Write-Host " Created Service Principal ID: $ ( $principal1.id ) " - ForegroundColor Green
160190
161191 # Step 7: Grant permission to create agent users (only if user chose to have Agent ID users)
162192 if ($hasAgentIDUsers ) {
@@ -322,15 +352,11 @@ function Invoke-MsIdAgentIdInteractive {
322352 else {
323353 Write-Host " - 9-10. Agent Identity and User creation (not completed)" - ForegroundColor Gray
324354 }
325- Write-Host " "
326-
327- Write-Host " Available functions:" - ForegroundColor Yellow
328- Get-Command - Module MSIdentityTools | Where-Object { $_.Name -like " *Agent*" } | Format-Table Name, CommandType - AutoSize
329355
330356 Write-Host " "
331357 Write-Host " Module state:" - ForegroundColor Yellow
332- Write-Host " Current Blueprint ID: $ ( $ blueprint1.AgentBlueprintId ) " - ForegroundColor White
333- Write-Host " Current Service Principal ID: $ ( $principal1.ServicePrincipalId ) " - ForegroundColor White
358+ Write-Host " Current Blueprint ID: $blueprint1 " - ForegroundColor White
359+ Write-Host " Current Service Principal ID: $ ( $principal1.id ) " - ForegroundColor White
334360 Write-Host " Total Agent Identities created: $ ( $allAgentIdentities.Count ) " - ForegroundColor White
335361 Write-Host " Total Agent Users created: $ ( $allAgentUsers.Count ) " - ForegroundColor White
336362 Write-Host " Last Agent Identity ID: $ ( if ($agentIdentity ) { $agentIdentity.id } else { ' None created' }) " - ForegroundColor White
0 commit comments