Skip to content

Commit eec95e5

Browse files
committed
Added agent id
1 parent a09caf1 commit eec95e5

20 files changed

+1976
-0
lines changed

src/MSIdentityTools.psd1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@
6868

6969
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
7070
NestedModules = @(
71+
'.\agentid\Add-MsIdClientSecretToAgentIdentityBlueprint.ps1'
72+
'.\agentid\Add-MsIdInheritablePermissionsToAgentIdentityBlueprint.ps1'
73+
'.\agentid\Add-MsIdPermissionToCreateAgentUsersToAgentIdentityBlueprintPrincipal.ps1'
74+
'.\agentid\Add-MsIdPermissionsToInheritToAgentIdentityBlueprintPrincipal.ps1'
75+
'.\agentid\Add-MsIdRedirectURIToAgentIdentityBlueprint.ps1'
76+
'.\agentid\Add-MsIdScopeToAgentIdentityBlueprint.ps1'
77+
'.\agentid\Connect-MsIdEntraAsUser.ps1'
78+
'.\agentid\ConnectAsAgentIdentityBlueprint.ps1'
79+
'.\agentid\Disconnect-MgGraphIfNeeded.ps1'
80+
'.\agentid\Disconnect-MsIdEntraAgentID.ps1'
81+
'.\agentid\Invoke-MsIdAgentIdInteractive.ps1'
82+
'.\agentid\EnsureRequiredModules.ps1'
83+
'.\agentid\Get-MSGraphServicePrincipalId.ps1'
84+
'.\agentid\Get-SponsorsAndOwners.ps1'
85+
'.\agentid\New-MsIdAgentIdentityBlueprint.ps1'
86+
'.\agentid\New-MsIdAgentIdentityBlueprintPrincipal.ps1'
87+
'.\agentid\New-MsIdAgentIDForAgentIdentityBlueprint.ps1'
88+
'.\agentid\New-MsIdAgentIDUserForAgentId.ps1'
7189
'.\internal\Compress-Data.ps1'
7290
'.\internal\Confirm-JsonWebSignature.ps1'
7391
'.\internal\ConvertFrom-Base64String.ps1'
@@ -164,13 +182,21 @@
164182

165183
# Functions to export from this module
166184
FunctionsToExport = @(
185+
'Add-MsIdClientSecretToAgentIdentityBlueprint'
186+
'Add-MsIdInheritablePermissionsToAgentIdentityBlueprint'
187+
'Add-MsIdPermissionToCreateAgentUsersToAgentIdentityBlueprintPrincipal'
188+
'Add-MsIdPermissionsToInheritToAgentIdentityBlueprintPrincipal'
189+
'Add-MsIdRedirectURIToAgentIdentityBlueprint'
190+
'Add-MsIdScopeToAgentIdentityBlueprint'
167191
'Add-MsIdServicePrincipal'
168192
'Confirm-MsIdJwtTokenSignature'
193+
'Connect-MsIdEntraAsUser'
169194
'ConvertFrom-MsIdAadcAadConnectorSpaceDn'
170195
'ConvertFrom-MsIdAadcSourceAnchor'
171196
'ConvertFrom-MsIdUniqueTokenIdentifier'
172197
'ConvertFrom-MsIdJwtToken'
173198
'ConvertFrom-MsIdSamlMessage'
199+
'Disconnect-MsIdEntraAgentID'
174200
'Expand-MsIdJwtTokenPayload'
175201
'Export-MsIdAppConsentGrantReport'
176202
'Export-MsIdAzureMfaReport'
@@ -193,6 +219,11 @@
193219
'Get-MsIdUnmanagedExternalUser'
194220
'Grant-MsIdMcpServerPermission'
195221
'Invoke-MsIdAzureAdSamlRequest'
222+
'Invoke-MsIdAgentIdInteractive'
223+
'New-MsIdAgentIdentityBlueprint'
224+
'New-MsIdAgentIdentityBlueprintPrincipal'
225+
'New-MsIdAgentIDForAgentIdentityBlueprint'
226+
'New-MsIdAgentIDUserForAgentId'
196227
'New-MsIdWsTrustRequest'
197228
'New-MsIdClientSecret'
198229
'New-MsIdSamlRequest'

src/MSIdentityTools.psm1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,37 @@ if ($PSVersionTable.PSVersion -lt [version]'7.0') {
99
#Write-Warning 'It is recommended to update Microsoft Graph PowerShell SDK modules frequently because many commands in this module depend on them.'
1010

1111
class SamlMessage : xml {}
12+
13+
#region AgentID
14+
15+
# Module-level variable to store the current Agent Blueprint ID
16+
$script:CurrentAgentBlueprintId = $null
17+
18+
# Module-level variable to store the current Agent Blueprint Secret
19+
$script:CurrentAgentBlueprintSecret = $null
20+
21+
# Module-level variable to store the current Agent Identity Blueprint Service Principal ID
22+
$script:CurrentAgentBlueprintServicePrincipalId = $null
23+
24+
# Module-level variable to cache the Microsoft Graph Service Principal ID
25+
$script:MSGraphServicePrincipalId = $null
26+
27+
# Module-level variable to store the last configured inheritable scopes
28+
$script:LastConfiguredInheritableScopes = $null
29+
30+
# Module-level variable to store the current Agent Identity ID
31+
$script:CurrentAgentIdentityId = $null
32+
33+
# Module-level variable to store the current tenant ID
34+
$script:CurrentTenantId = $null
35+
36+
# Module-level variable to store the last client secret
37+
$script:LastClientSecret = $null
38+
39+
# Module-level variable to track the last successful connection type
40+
$script:LastSuccessfulConnection = $null
41+
42+
# Module-level variable to store the current Agent User ID
43+
$script:CurrentAgentUserId = $null
44+
45+
#endregion AgentID
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<#
2+
.SYNOPSIS
3+
Adds a client secret to the current Agent Identity Blueprint
4+
5+
.DESCRIPTION
6+
Creates an application password for the most recently created Agent Identity Blueprint using New-MgApplicationPassword.
7+
Uses the stored AgentBlueprintId from the last New-AgentIdentityBlueprint call.
8+
9+
.PARAMETER AgentBlueprintId
10+
Optional. The ID of the Agent Identity Blueprint to add the secret to. If not provided, uses the stored ID from the last blueprint creation.
11+
12+
.EXAMPLE
13+
New-MsIdAgentIdentityBlueprint -DisplayName "My Blueprint" -SponsorUserIds @("user1")
14+
Add-MsIdClientSecretToAgentIdentityBlueprint # Uses the stored blueprint ID
15+
16+
.EXAMPLE
17+
Add-MsIdClientSecretToAgentIdentityBlueprint -AgentBlueprintId "12345678-1234-1234-1234-123456789012" # Uses specific ID
18+
#>
19+
function Add-MsIdClientSecretToAgentIdentityBlueprint {
20+
[CmdletBinding()]
21+
param (
22+
[Parameter(Mandatory = $false)]
23+
[string]$AgentBlueprintId
24+
)
25+
26+
# Use stored blueprint ID if not provided
27+
if (-not $AgentBlueprintId) {
28+
if (-not $script:CurrentAgentBlueprintId) {
29+
Write-Error "No Agent Blueprint ID available. Please create a blueprint first using New-MsIdAgentIdentityBlueprint or provide an explicit AgentBlueprintId parameter."
30+
return
31+
}
32+
$AgentBlueprintId = $script:CurrentAgentBlueprintId
33+
Write-Host "Using stored Agent Blueprint ID: $AgentBlueprintId" -ForegroundColor Gray
34+
}
35+
36+
# Ensure we're connected to Microsoft Graph
37+
$context = Get-MgContext
38+
if (-not $context) {
39+
Write-Error "Not connected to Microsoft Graph. Please run Connect-MgGraph first."
40+
return
41+
}
42+
43+
try {
44+
Write-Host "Adding secret to Agent Blueprint: $AgentBlueprintId" -ForegroundColor Yellow
45+
46+
# Create the password credential object
47+
$passwordCredential = @{
48+
displayName = "1st blueprint secret for dev/test. Not recommended for production use"
49+
endDateTime = (Get-Date).AddDays(90).ToString("yyyy-MM-ddTHH:mm:ssZ")
50+
}
51+
52+
# Add the secret to the application
53+
$secretResult = Add-MgApplicationPassword -ApplicationId $AgentBlueprintId -PasswordCredential $passwordCredential
54+
55+
Write-Host "Successfully added secret to Agent Blueprint" -ForegroundColor Green
56+
#Write-Host "Secret Value: $($secretResult.SecretText)" -ForegroundColor Red
57+
58+
# Add additional properties for easy access
59+
$secretResult | Add-Member -MemberType NoteProperty -Name "Description" -Value "Not recommended for production use" -Force
60+
$secretResult | Add-Member -MemberType NoteProperty -Name "AgentBlueprintId" -Value $AgentBlueprintId -Force
61+
62+
# Store the secret in module-level variables for use by other functions
63+
$script:CurrentAgentBlueprintSecret = $secretResult
64+
$script:LastClientSecret = $secretResult.SecretText
65+
66+
return $secretResult
67+
}
68+
catch {
69+
Write-Error "Failed to add secret to Agent Blueprint: $_"
70+
throw
71+
}
72+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<#
2+
.SYNOPSIS
3+
Adds inheritable permissions to Agent Identity Blueprints
4+
5+
.DESCRIPTION
6+
Configures inheritable Microsoft Graph permissions that can be granted to Agent Identity Blueprints.
7+
This allows agents created from the blueprint to inherit specific Microsoft Graph permissions.
8+
9+
.PARAMETER Scopes
10+
Optional. Array of Microsoft Graph permission scopes to make inheritable. If not provided, will prompt for input.
11+
Common scopes include: User.Read, Mail.Read, Calendars.Read, etc.
12+
13+
.PARAMETER ResourceAppId
14+
Optional. The resource application ID. Defaults to Microsoft Graph (00000003-0000-0000-c000-000000000000).
15+
16+
.EXAMPLE
17+
Add-MsIdInheritablePermissionsToAgentIdentityBlueprint # Will prompt for scopes
18+
19+
.EXAMPLE
20+
Add-MsIdInheritablePermissionsToAgentIdentityBlueprint -Scopes @("User.Read", "Mail.Read", "Calendars.Read")
21+
22+
.EXAMPLE
23+
Add-MsIdInheritablePermissionsToAgentIdentityBlueprint -Scopes @("User.Read") -ResourceAppId "00000003-0000-0000-c000-000000000000"
24+
#>
25+
function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
26+
[CmdletBinding()]
27+
param (
28+
[Parameter(Mandatory = $false)]
29+
[string[]]$Scopes,
30+
31+
[Parameter(Mandatory = $false)]
32+
[string]$ResourceAppId = "00000003-0000-0000-c000-000000000000"
33+
)
34+
35+
# Prompt for ResourceAppId if not provided
36+
if (-not $ResourceAppId -or $ResourceAppId.Trim() -eq "") {
37+
Write-Host "Enter the Resource Application ID for the permissions." -ForegroundColor Yellow
38+
Write-Host "Default: 00000003-0000-0000-c000-000000000000 (Microsoft Graph)" -ForegroundColor Gray
39+
40+
$resourceInput = Read-Host "Resource App ID (press Enter for Microsoft Graph default)"
41+
if ($resourceInput -and $resourceInput.Trim() -ne "") {
42+
$ResourceAppId = $resourceInput.Trim()
43+
} else {
44+
$ResourceAppId = "00000003-0000-0000-c000-000000000000"
45+
Write-Host "Using default: Microsoft Graph" -ForegroundColor Cyan
46+
}
47+
}
48+
49+
# Determine resource name for display
50+
$resourceName = switch ($ResourceAppId) {
51+
"00000003-0000-0000-c000-000000000000" { "Microsoft Graph" }
52+
"00000002-0000-0000-c000-000000000000" { "Azure Active Directory Graph" }
53+
default { "Custom Resource ($ResourceAppId)" }
54+
}
55+
56+
# Prompt for scopes if not provided
57+
if (-not $Scopes -or $Scopes.Count -eq 0) {
58+
Write-Host "Enter permission scopes to make inheritable for $resourceName." -ForegroundColor Yellow
59+
if ($ResourceAppId -eq "00000003-0000-0000-c000-000000000000") {
60+
Write-Host "Common Microsoft Graph scopes: User.Read, Mail.Read, Calendars.Read, Files.Read, etc." -ForegroundColor Gray
61+
}
62+
Write-Host "Enter multiple scopes separated by commas." -ForegroundColor Gray
63+
64+
do {
65+
$scopeInput = Read-Host "Enter permission scopes (comma-separated)"
66+
if ($scopeInput -and $scopeInput.Trim() -ne "") {
67+
$Scopes = $scopeInput.Split(',') | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
68+
}
69+
} while (-not $Scopes -or $Scopes.Count -eq 0)
70+
}
71+
72+
# Check if we have a stored Agent Blueprint ID
73+
if (-not $script:CurrentAgentBlueprintId) {
74+
Write-Error "No Agent Blueprint ID available. Please create a blueprint first using New-MsIdAgentIdentityBlueprint."
75+
return
76+
}
77+
78+
# Ensure we're connected to Microsoft Graph
79+
$context = Get-MgContext
80+
if (-not $context) {
81+
Write-Error "Not connected to Microsoft Graph. Please run Connect-MgGraph first."
82+
return
83+
}
84+
85+
try {
86+
Write-Host "Adding inheritable permissions to Agent Identity Blueprint..." -ForegroundColor Yellow
87+
Write-Host "Agent Blueprint ID: $($script:CurrentAgentBlueprintId)" -ForegroundColor Gray
88+
Write-Host "Resource App ID: $ResourceAppId ($resourceName)" -ForegroundColor Cyan
89+
Write-Host "Scopes to make inheritable:" -ForegroundColor Cyan
90+
foreach ($scope in $Scopes) {
91+
Write-Host " - $scope" -ForegroundColor White
92+
}
93+
94+
# Build the request body
95+
$Body = [PSCustomObject]@{
96+
resourceAppId = $ResourceAppId
97+
inheritableScopes = [PSCustomObject]@{
98+
"@odata.type" = "microsoft.graph.enumeratedScopes"
99+
scopes = $Scopes
100+
}
101+
}
102+
103+
$JsonBody = $Body | ConvertTo-Json -Depth 5
104+
Write-Debug "Request Body: $JsonBody"
105+
106+
# Use Invoke-MgRestMethod to make the API call with the stored Agent Blueprint ID
107+
$apiUrl = "https://graph.microsoft.com/beta/applications/microsoft.graph.agentIdentityBlueprint/$($script:CurrentAgentBlueprintId)/inheritablePermissions"
108+
Write-Debug "API URL: $apiUrl"
109+
$result = Invoke-MgRestMethod -Method POST -Uri $apiUrl -Body $JsonBody -ContentType "application/json"
110+
111+
Write-Host "Successfully added inheritable permissions to Agent Identity Blueprints" -ForegroundColor Green
112+
Write-Host "Permissions are now available for inheritance by agent blueprints" -ForegroundColor Green
113+
114+
# Store the scopes for use in other functions
115+
$script:LastConfiguredInheritableScopes = $Scopes
116+
117+
# Create a result object with permission information
118+
$permissionResult = [PSCustomObject]@{
119+
AgentBlueprintId = $script:CurrentAgentBlueprintId
120+
ResourceAppId = $ResourceAppId
121+
ResourceAppName = $resourceName
122+
InheritableScopes = $Scopes
123+
ScopeCount = $Scopes.Count
124+
ConfiguredAt = Get-Date
125+
ApiResponse = $result
126+
}
127+
128+
return $permissionResult
129+
}
130+
catch {
131+
Write-Error "Failed to add inheritable permissions: $_"
132+
if ($_.Exception.Response) {
133+
Write-Host "Response Status: $($_.Exception.Response.StatusCode)" -ForegroundColor Red
134+
if ($_.Exception.Response.Content) {
135+
Write-Host "Response Content: $($_.Exception.Response.Content)" -ForegroundColor Red
136+
}
137+
}
138+
throw
139+
}
140+
}

0 commit comments

Comments
 (0)