Skip to content

Commit 588dfaa

Browse files
authored
Merge pull request #93 from kylemar/main
add retires for replication
2 parents a855480 + 967bf8e commit 588dfaa

11 files changed

+319
-78
lines changed

src/agentid/Add-MsIdClientSecretToAgentIdentityBlueprint.ps1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,29 @@ function Add-MsIdClientSecretToAgentIdentityBlueprint {
4949
endDateTime = (Get-Date).AddDays(90).ToString("yyyy-MM-ddTHH:mm:ssZ")
5050
}
5151

52-
# Add the secret to the application
53-
$secretResult = Add-MgApplicationPassword -ApplicationId $AgentBlueprintId -PasswordCredential $passwordCredential
52+
# Add the secret to the application with retry logic
53+
$retryCount = 0
54+
$maxRetries = 10
55+
$secretResult = $null
56+
$success = $false
57+
58+
while ($retryCount -lt $maxRetries -and -not $success) {
59+
try {
60+
$secretResult = Add-MgApplicationPassword -ApplicationId $AgentBlueprintId -PasswordCredential $passwordCredential -ErrorAction Stop
61+
$success = $true
62+
}
63+
catch {
64+
$retryCount++
65+
if ($retryCount -lt $maxRetries) {
66+
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
67+
Start-Sleep -Seconds 10
68+
}
69+
else {
70+
Write-Error "Failed to add secret to Agent Blueprint after $maxRetries attempts: $_"
71+
throw
72+
}
73+
}
74+
}
5475

5576
Write-Host "Successfully added secret to Agent Blueprint" -ForegroundColor Green
5677
#Write-Host "Secret Value: $($secretResult.SecretText)" -ForegroundColor Red

src/agentid/Add-MsIdInheritablePermissionsToAgentIdentityBlueprint.ps1

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,32 @@ function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
103103
$JsonBody = $Body | ConvertTo-Json -Depth 5
104104
Write-Debug "Request Body: $JsonBody"
105105

106-
# Use Invoke-MgRestMethod to make the API call with the stored Agent Blueprint ID
106+
# Use Invoke-MgRestMethod to make the API call with the stored Agent Blueprint ID with retry logic
107107
$apiUrl = "https://graph.microsoft.com/beta/applications/microsoft.graph.agentIdentityBlueprint/$($script:CurrentAgentBlueprintId)/inheritablePermissions"
108108
Write-Debug "API URL: $apiUrl"
109-
$result = Invoke-MgRestMethod -Method POST -Uri $apiUrl -Body $JsonBody -ContentType "application/json"
109+
110+
$retryCount = 0
111+
$maxRetries = 10
112+
$result = $null
113+
$success = $false
114+
115+
while ($retryCount -lt $maxRetries -and -not $success) {
116+
try {
117+
$result = Invoke-MgRestMethod -Method POST -Uri $apiUrl -Body $JsonBody -ContentType "application/json" -ErrorAction Stop
118+
$success = $true
119+
}
120+
catch {
121+
$retryCount++
122+
if ($retryCount -lt $maxRetries) {
123+
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
124+
Start-Sleep -Seconds 10
125+
}
126+
else {
127+
Write-Error "Failed to add inheritable permissions after $maxRetries attempts: $_"
128+
throw
129+
}
130+
}
131+
}
110132

111133
Write-Host "Successfully added inheritable permissions to Agent Identity Blueprints" -ForegroundColor Green
112134
Write-Host "Permissions are now available for inheritance by agent blueprints" -ForegroundColor Green

src/agentid/Add-MsIdRedirectURIToAgentIdentityBlueprint.ps1

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,29 @@ function Add-MsIdRedirectURIToAgentIdentityBlueprint {
5757

5858
# First, get the current application configuration to preserve existing redirect URIs
5959
Write-Host "Retrieving current application configuration..." -ForegroundColor Yellow
60-
$currentApp = Invoke-MgRestMethod -Method GET -Uri "https://graph.microsoft.com/v1.0/applications/$AgentBlueprintId" -ContentType "application/json"
60+
61+
$retryCount = 0
62+
$maxRetries = 10
63+
$currentApp = $null
64+
$success = $false
65+
66+
while ($retryCount -lt $maxRetries -and -not $success) {
67+
try {
68+
$currentApp = Invoke-MgRestMethod -Method GET -Uri "https://graph.microsoft.com/beta/applications/$AgentBlueprintId" -ContentType "application/json" -ErrorAction Stop
69+
$success = $true
70+
}
71+
catch {
72+
$retryCount++
73+
if ($retryCount -lt $maxRetries) {
74+
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
75+
Start-Sleep -Seconds 10
76+
}
77+
else {
78+
Write-Error "Failed to retrieve application configuration after $maxRetries attempts: $_"
79+
throw
80+
}
81+
}
82+
}
6183

6284
# Get existing redirect URIs or initialize empty array
6385
$existingRedirectUris = @()
@@ -93,8 +115,29 @@ function Add-MsIdRedirectURIToAgentIdentityBlueprint {
93115
$JsonBody = $Body | ConvertTo-Json -Depth 5
94116
Write-Debug "Request Body: $JsonBody"
95117

96-
# Use Invoke-MgRestMethod to update the application
97-
$updateResult = Invoke-MgRestMethod -Method PATCH -Uri "https://graph.microsoft.com/v1.0/applications/$AgentBlueprintId" -Body $JsonBody -ContentType "application/json"
118+
# Use Invoke-MgRestMethod to update the application with retry logic
119+
$retryCount = 0
120+
$maxRetries = 10
121+
$updateResult = $null
122+
$success = $false
123+
124+
while ($retryCount -lt $maxRetries -and -not $success) {
125+
try {
126+
$updateResult = Invoke-MgRestMethod -Method PATCH -Uri "https://graph.microsoft.com/beta/applications/$AgentBlueprintId" -Body $JsonBody -ContentType "application/json" -ErrorAction Stop
127+
$success = $true
128+
}
129+
catch {
130+
$retryCount++
131+
if ($retryCount -lt $maxRetries) {
132+
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
133+
Start-Sleep -Seconds 10
134+
}
135+
else {
136+
Write-Error "Failed to update redirect URI after $maxRetries attempts: $_"
137+
throw
138+
}
139+
}
140+
}
98141

99142
Write-Host "Successfully added web redirect URI to Agent Identity Blueprint" -ForegroundColor Green
100143
Write-Host "Total redirect URIs: $($updatedRedirectUris.Count)" -ForegroundColor Cyan

src/agentid/Add-MsIdScopeToAgentIdentityBlueprint.ps1

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,29 @@ function Add-MsIdScopeToAgentIdentityBlueprint {
126126
$JsonBody = $Body | ConvertTo-Json -Depth 5
127127
Write-Debug "Request Body: $JsonBody"
128128

129-
# Use Invoke-MgRestMethod to update the application
130-
$scopeResult = Invoke-MgRestMethod -Method PATCH -Uri "https://graph.microsoft.com/v1.0/applications/$AgentBlueprintId" -Body $JsonBody -ContentType "application/json"
129+
# Use Invoke-MgRestMethod to update the application with retry logic
130+
$retryCount = 0
131+
$maxRetries = 10
132+
$scopeResult = $null
133+
$success = $false
134+
135+
while ($retryCount -lt $maxRetries -and -not $success) {
136+
try {
137+
$scopeResult = Invoke-MgRestMethod -Method PATCH -Uri "https://graph.microsoft.com/beta/applications/$AgentBlueprintId" -Body $JsonBody -ContentType "application/json" -ErrorAction Stop
138+
$success = $true
139+
}
140+
catch {
141+
$retryCount++
142+
if ($retryCount -lt $maxRetries) {
143+
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
144+
Start-Sleep -Seconds 10
145+
}
146+
else {
147+
Write-Error "Failed to add OAuth2 permission scope after $maxRetries attempts: $_"
148+
throw
149+
}
150+
}
151+
}
131152

132153
Write-Host "Successfully added OAuth2 permission scope to Agent Blueprint" -ForegroundColor Green
133154
Write-Host "Scope ID: $scopeId" -ForegroundColor Cyan

src/agentid/Connect-MsIdEntraAsUser.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Connect-MsIdEntraAsUser {
1919
[CmdletBinding()]
2020
param (
2121
[Parameter(Mandatory = $false)]
22-
[string[]]$Scopes = @('AgentIdentityBlueprint.Create', 'AgentIdentityBlueprintPrincipal.Create', 'AppRoleAssignment.ReadWrite.All', 'Application.ReadWrite.All', 'User.ReadWrite.All')
22+
[string[]]$Scopes = @('AgentIdentityBlueprint.Create', 'AgentIdentityBlueprintPrincipal.Create', 'AppRoleAssignment.ReadWrite.All', 'Application.ReadWrite.All', 'User.ReadWrite.All', 'AgentIdentityBlueprint.ReadWrite.All', 'AgentIdentityBlueprint.AddRemoveCreds.All')
2323
)
2424

2525
# Ensure required modules are available

src/agentid/EnsureRequiredModules.ps1

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Ensures that required PowerShell modules are installed and imported
44
55
.DESCRIPTION
6-
Checks for required modules and installs them if they are not available
6+
Checks for required modules and installs them if they are not available.
7+
Handles version conflicts by checking if compatible versions are already loaded.
78
#>
89
function EnsureRequiredModules {
910
[CmdletBinding()]
@@ -16,7 +17,40 @@ function EnsureRequiredModules {
1617
'Microsoft.Graph.Identity.DirectoryManagement'
1718
)
1819

20+
# Check if there are version conflicts in loaded modules
21+
$loadedGraphModules = Get-Module -Name Microsoft.Graph.*
22+
$hasVersionConflict = $false
23+
24+
if ($loadedGraphModules) {
25+
$authModule = $loadedGraphModules | Where-Object { $_.Name -eq 'Microsoft.Graph.Authentication' }
26+
$otherModules = $loadedGraphModules | Where-Object { $_.Name -ne 'Microsoft.Graph.Authentication' }
27+
28+
# Check if loaded modules have different versions of dependencies
29+
foreach ($mod in $otherModules) {
30+
$authDep = $mod.RequiredModules | Where-Object { $_.Name -eq 'Microsoft.Graph.Authentication' }
31+
if ($authDep -and $authModule -and $authDep.Version -ne $authModule.Version) {
32+
Write-Verbose "Version conflict detected: $($mod.Name) requires Microsoft.Graph.Authentication $($authDep.Version) but $($authModule.Version) is loaded"
33+
$hasVersionConflict = $true
34+
break
35+
}
36+
}
37+
}
38+
39+
# If there's a version conflict, we need to start fresh
40+
if ($hasVersionConflict) {
41+
Write-Host "Detected module version conflicts. Removing all Microsoft.Graph modules from session..." -ForegroundColor Yellow
42+
Get-Module -Name Microsoft.Graph.* | Remove-Module -Force -ErrorAction SilentlyContinue
43+
}
44+
1945
foreach ($module in $requiredModules) {
46+
# Check if module is already loaded with compatible version
47+
$loadedModule = Get-Module -Name $module
48+
if ($loadedModule -and -not $hasVersionConflict) {
49+
Write-Verbose "Module $module is already loaded (version $($loadedModule.Version))"
50+
continue
51+
}
52+
53+
# Install if not available
2054
if (!(Get-Module -ListAvailable -Name $module)) {
2155
Write-Host "Module $module not found. Installing..." -ForegroundColor Yellow
2256
try {

src/agentid/Get-MsIdAgentIdentity.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function Get-MsIdAgentIdentity {
3939

4040
# Call the Graph API to get the agent identity
4141
$uri = "https://graph.microsoft.com/beta/servicePrincipals/microsoft.graph.agentIdentity/$AgentId"
42-
$result = Invoke-MgRestMethod -Method GET -Uri $uri
42+
$result = Invoke-MgRestMethod -Method GET -Uri $uri -ErrorAction Stop
4343

4444
Write-Verbose "Successfully retrieved Agent Identity"
4545
return $result

0 commit comments

Comments
 (0)