Skip to content

Commit ebe0d76

Browse files
authored
Merge pull request #90 from kylemar/main
Address issues in Invoke-MsIdAgentIdInteractive
2 parents 07e31b1 + 2b9d4c9 commit ebe0d76

6 files changed

+50
-27
lines changed

src/agentid/Add-MsIdClientSecretToAgentIdentityBlueprint.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function Add-MsIdClientSecretToAgentIdentityBlueprint {
6161

6262
# Store the secret in module-level variables for use by other functions
6363
$script:CurrentAgentBlueprintSecret = $secretResult
64-
$script:LastClientSecret = $secretResult.SecretText
64+
$script:LastClientSecret = ConvertTo-SecureString $secretResult.SecretText -AsPlainText -Force
6565

6666
return $secretResult
6767
}

src/agentid/Connect-MsIdEntraAsUser.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Connect-MsIdEntraAsUser {
3636
}
3737

3838
Write-Host "Connecting to Microsoft Graph as user..." -ForegroundColor Yellow
39-
connect-mggraph -contextscope process -scopes $Scopes
39+
connect-mggraph -contextscope process -scopes $Scopes -NoWelcome
4040

4141
# Get the tenant ID and current user
4242
$context = Get-MgContext

src/agentid/ConnectAsAgentIdentityBlueprint.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ function ConnectAsAgentIdentityBlueprint {
4242
Write-Host "Connecting to Microsoft Graph using Agent Identity Blueprint credentials..." -ForegroundColor Yellow
4343

4444
# Convert the stored client secret to a secure credential
45-
$SecureClientSecret = ConvertTo-SecureString $script:LastClientSecret -AsPlainText -Force
46-
$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $script:CurrentAgentBlueprintId, $SecureClientSecret
45+
$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $script:CurrentAgentBlueprintId, $script:LastClientSecret
4746

4847
# Connect to Microsoft Graph using the blueprint's credentials
4948
connect-mggraph -tenantId $script:CurrentTenantId -ClientSecretCredential $ClientSecretCredential -ContextScope Process -NoWelcome

src/agentid/EnsureRequiredModules.ps1

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ function EnsureRequiredModules {
1212
$requiredModules = @(
1313
'Microsoft.Graph.Authentication',
1414
'Microsoft.Graph.Applications',
15-
'Microsoft.Graph.Identity.SignIns'
15+
'Microsoft.Graph.Identity.SignIns',
16+
'Microsoft.Graph.Users',
17+
'Microsoft.Graph.Identity.DirectoryManagement'
1618
)
1719

1820
foreach ($module in $requiredModules) {
19-
Write-Host "Checking module: $module" -ForegroundColor Yellow
20-
2121
if (!(Get-Module -ListAvailable -Name $module)) {
22-
Write-Host "Module $module not found. Installing..." -ForegroundColor Red
22+
Write-Host "Module $module not found. Installing..." -ForegroundColor Yellow
2323
try {
2424
Install-Module -Name $module -Scope CurrentUser -Force -AllowClobber
2525
Write-Host "Successfully installed $module" -ForegroundColor Green
@@ -29,15 +29,11 @@ function EnsureRequiredModules {
2929
return $false
3030
}
3131
}
32-
else {
33-
Write-Host "Module $module is already installed" -ForegroundColor Green
34-
}
3532

3633
# Import the module if not already imported
3734
if (!(Get-Module -Name $module)) {
3835
try {
3936
Import-Module -Name $module -Force
40-
Write-Host "Successfully imported $module" -ForegroundColor Green
4137
}
4238
catch {
4339
Write-Error "Failed to import module $module`: $_"

src/agentid/Invoke-MsIdAgentIdInteractive.ps1

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/agentid/New-MsIdAgentIdentityBlueprint.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ function New-MsIdAgentIdentityBlueprint {
9898
try {
9999
$BlueprintRes = Invoke-MgGraphRequest -Method Post -Uri "https://graph.microsoft.com/beta/applications/graph.agentIdentityBlueprint" -Body $JsonBody
100100

101+
# Display the full response from the Graph API call
102+
Write-Host "Graph API Response:" -ForegroundColor Cyan
103+
$BlueprintRes | ConvertTo-Json -Depth 5 | Write-Host -ForegroundColor Gray
104+
101105
# Extract and store the blueprint ID
102106
$AgentBlueprintId = $BlueprintRes.id
103107
Write-Host "Successfully created Agent Identity Blueprint" -ForegroundColor Green
@@ -106,10 +110,8 @@ function New-MsIdAgentIdentityBlueprint {
106110
# Store the ID in module-level variable for use by other functions
107111
$script:CurrentAgentBlueprintId = $AgentBlueprintId
108112

109-
# Add the ID to the response object for easy access
110-
$BlueprintRes | Add-Member -MemberType NoteProperty -Name "AgentBlueprintId" -Value $AgentBlueprintId -Force
111-
112-
return $BlueprintRes
113+
# Return only the AgentBlueprintId instead of the full response
114+
return $AgentBlueprintId
113115
}
114116
catch {
115117
Write-Error "Failed to create Agent Identity Blueprint: $_"

0 commit comments

Comments
 (0)