Skip to content

Commit 7dc754d

Browse files
committed
Removed module dependencies
1 parent fa89eda commit 7dc754d

12 files changed

+238
-215
lines changed

src/MSIdentityTools.psd1

Lines changed: 166 additions & 167 deletions
Large diffs are not rendered by default.

src/agentid/Add-MsIdClientSecretToAgentIdentityBlueprint.ps1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,20 @@ function Add-MsIdClientSecretToAgentIdentityBlueprint {
5555
$secretResult = $null
5656
$success = $false
5757

58+
$body = @{
59+
passwordCredential = $passwordCredential
60+
}
61+
5862
while ($retryCount -lt $maxRetries -and -not $success) {
5963
try {
60-
$secretResult = Add-MgApplicationPassword -ApplicationId $AgentBlueprintId -PasswordCredential $passwordCredential -ErrorAction Stop
64+
$secretResult = Invoke-MgGraphRequest -Method POST -Uri "v1.0/applications/$AgentBlueprintId/addPassword" -Body ($body | ConvertTo-Json -Depth 10) -ErrorAction Stop
6165
$success = $true
6266
}
6367
catch {
6468
$retryCount++
6569
if ($retryCount -lt $maxRetries) {
66-
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
70+
Write-Host "Waiting for propagation..." -ForegroundColor Yellow
71+
Write-Verbose "Attempt $retryCount failed. Waiting 10 seconds before retry..."
6772
Start-Sleep -Seconds 10
6873
}
6974
else {

src/agentid/Add-MsIdInheritablePermissionsToAgentIdentityBlueprint.ps1

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
4040
$resourceInput = Read-Host "Resource App ID (press Enter for Microsoft Graph default)"
4141
if ($resourceInput -and $resourceInput.Trim() -ne "") {
4242
$ResourceAppId = $resourceInput.Trim()
43-
} else {
43+
}
44+
else {
4445
$ResourceAppId = "00000003-0000-0000-c000-000000000000"
4546
Write-Host "Using default: Microsoft Graph" -ForegroundColor Cyan
4647
}
@@ -93,10 +94,10 @@ function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
9394

9495
# Build the request body
9596
$Body = [PSCustomObject]@{
96-
resourceAppId = $ResourceAppId
97+
resourceAppId = $ResourceAppId
9798
inheritableScopes = [PSCustomObject]@{
9899
"@odata.type" = "microsoft.graph.enumeratedScopes"
99-
scopes = $Scopes
100+
scopes = $Scopes
100101
}
101102
}
102103

@@ -106,7 +107,7 @@ function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
106107
# Use Invoke-MgRestMethod to make the API call with the stored Agent Blueprint ID with retry logic
107108
$apiUrl = "https://graph.microsoft.com/beta/applications/microsoft.graph.agentIdentityBlueprint/$($script:CurrentAgentBlueprintId)/inheritablePermissions"
108109
Write-Debug "API URL: $apiUrl"
109-
110+
110111
$retryCount = 0
111112
$maxRetries = 10
112113
$result = $null
@@ -120,7 +121,8 @@ function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
120121
catch {
121122
$retryCount++
122123
if ($retryCount -lt $maxRetries) {
123-
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
124+
Write-Host "Waiting for propagation..." -ForegroundColor Yellow
125+
Write-Verbose "Attempt $retryCount failed. Waiting 10 seconds before retry..."
124126
Start-Sleep -Seconds 10
125127
}
126128
else {
@@ -138,13 +140,13 @@ function Add-MsIdInheritablePermissionsToAgentIdentityBlueprint {
138140

139141
# Create a result object with permission information
140142
$permissionResult = [PSCustomObject]@{
141-
AgentBlueprintId = $script:CurrentAgentBlueprintId
142-
ResourceAppId = $ResourceAppId
143-
ResourceAppName = $resourceName
143+
AgentBlueprintId = $script:CurrentAgentBlueprintId
144+
ResourceAppId = $ResourceAppId
145+
ResourceAppName = $resourceName
144146
InheritableScopes = $Scopes
145-
ScopeCount = $Scopes.Count
146-
ConfiguredAt = Get-Date
147-
ApiResponse = $result
147+
ScopeCount = $Scopes.Count
148+
ConfiguredAt = Get-Date
149+
ApiResponse = $result
148150
}
149151

150152
return $permissionResult

src/agentid/Add-MsIdPermissionToCreateAgentUsersToAgentIdentityBlueprintPrincipal.ps1

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,23 @@ function Add-MsIdPermissionToCreateAgentUsersToAgentIdentityBlueprintPrincipal {
5050
else {
5151
Write-Host "Connected to Microsoft Graph as: $($context.Account)" -ForegroundColor Green
5252
}
53-
53+
5454
try {
5555
Write-Host "Adding permission to create Agent Users to Agent Identity Blueprint Principal..." -ForegroundColor Green
5656
Write-Verbose "Retrieving Blueprint Service Principal ID from tenant..."
57-
$blueprintServicePrincipal = Get-MgServicePrincipal -Filter "appId eq '$AgentBlueprintId'" -Select "id,appId,displayName"
57+
$blueprintServicePrincipalResponse = Invoke-MgGraphRequest -Method GET -Uri "v1.0/servicePrincipals?`$filter=appId eq '$AgentBlueprintId'&`$select=id,appId,displayName"
5858

59-
if (-not $blueprintServicePrincipal) {
59+
if (-not $blueprintServicePrincipalResponse.value -or $blueprintServicePrincipalResponse.value.Count -eq 0) {
6060
throw "Blueprint Service Principal not found in tenant"
6161
}
6262

63+
$blueprintServicePrincipal = $blueprintServicePrincipalResponse.value[0]
64+
6365
# Cache the result
64-
$script:CurrentAgentBlueprintServicePrincipalId = $blueprintServicePrincipal.Id
66+
$script:CurrentAgentBlueprintServicePrincipalId = $blueprintServicePrincipal.id
67+
68+
Write-Verbose "Blueprint Service Principal found - ID: $script:CurrentAgentBlueprintServicePrincipalId, Display Name: $($blueprintServicePrincipal.displayName)"
6569

66-
Write-Verbose "Blueprint Service Principal found - ID: $script:CurrentAgentBlueprintServicePrincipalId, Display Name: $($blueprintServicePrincipal.DisplayName)"
67-
6870
$servicePrincipalId = $script:CurrentAgentBlueprintServicePrincipalId
6971
Write-Host "Using stored Agent Identity Blueprint Service Principal ID: $servicePrincipalId" -ForegroundColor Yellow
7072

src/agentid/Add-MsIdRedirectURIToAgentIdentityBlueprint.ps1

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ 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-
60+
6161
$retryCount = 0
6262
$maxRetries = 10
6363
$currentApp = $null
@@ -71,7 +71,8 @@ function Add-MsIdRedirectURIToAgentIdentityBlueprint {
7171
catch {
7272
$retryCount++
7373
if ($retryCount -lt $maxRetries) {
74-
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
74+
Write-Host "Waiting for propagation..." -ForegroundColor Yellow
75+
Write-Verbose "Attempt $retryCount failed. Waiting 10 seconds before retry..."
7576
Start-Sleep -Seconds 10
7677
}
7778
else {
@@ -93,10 +94,10 @@ function Add-MsIdRedirectURIToAgentIdentityBlueprint {
9394

9495
$result = [PSCustomObject]@{
9596
AgentBlueprintId = $AgentBlueprintId
96-
RedirectUri = $RedirectUri
97-
Action = "Already Exists"
98-
AllRedirectUris = $existingRedirectUris
99-
ConfiguredAt = Get-Date
97+
RedirectUri = $RedirectUri
98+
Action = "Already Exists"
99+
AllRedirectUris = $existingRedirectUris
100+
ConfiguredAt = Get-Date
100101
}
101102

102103
return $result
@@ -129,7 +130,8 @@ function Add-MsIdRedirectURIToAgentIdentityBlueprint {
129130
catch {
130131
$retryCount++
131132
if ($retryCount -lt $maxRetries) {
132-
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
133+
Write-Host "Waiting for propagation..." -ForegroundColor Yellow
134+
Write-Verbose "Attempt $retryCount failed. Waiting 10 seconds before retry..."
133135
Start-Sleep -Seconds 10
134136
}
135137
else {
@@ -145,11 +147,11 @@ function Add-MsIdRedirectURIToAgentIdentityBlueprint {
145147
# Create a result object with redirect URI information
146148
$result = [PSCustomObject]@{
147149
AgentBlueprintId = $AgentBlueprintId
148-
RedirectUri = $RedirectUri
149-
Action = "Added"
150-
AllRedirectUris = $updatedRedirectUris
151-
ConfiguredAt = Get-Date
152-
ApiResponse = $updateResult
150+
RedirectUri = $RedirectUri
151+
Action = "Added"
152+
AllRedirectUris = $updatedRedirectUris
153+
ConfiguredAt = Get-Date
154+
ApiResponse = $updateResult
153155
}
154156

155157
return $result

src/agentid/Add-MsIdScopeToAgentIdentityBlueprint.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ function Add-MsIdScopeToAgentIdentityBlueprint {
140140
catch {
141141
$retryCount++
142142
if ($retryCount -lt $maxRetries) {
143-
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
143+
Write-Host "Waiting for propagation..." -ForegroundColor Yellow
144+
Write-Verbose "Attempt $retryCount failed. Waiting 10 seconds before retry..."
144145
Start-Sleep -Seconds 10
145146
}
146147
else {

src/agentid/Connect-MsIdEntraAsUser.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ function Connect-MsIdEntraAsUser {
2222
[string[]]$Scopes = @('AgentIdentityBlueprint.Create', 'AgentIdentityBlueprintPrincipal.Create', 'AppRoleAssignment.ReadWrite.All', 'Application.ReadWrite.All', 'User.ReadWrite.All', 'AgentIdentityBlueprint.ReadWrite.All', 'AgentIdentityBlueprint.AddRemoveCreds.All')
2323
)
2424

25-
# Ensure required modules are available
26-
if (!(EnsureRequiredModules)) {
27-
Write-Error "Failed to ensure required modules are available."
28-
return
29-
}
25+
# # Ensure required modules are available
26+
# if (!(EnsureRequiredModules)) {
27+
# Write-Error "Failed to ensure required modules are available."
28+
# return
29+
# }
3030

3131
try {
3232
# Check if we need to disconnect from a different connection type

src/agentid/Get-MSGraphServicePrincipalId.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ function Get-MSGraphServicePrincipalId {
2626
$msGraphAppId = "00000003-0000-0000-c000-000000000000"
2727

2828
# Get the service principal for Microsoft Graph
29-
$msGraphServicePrincipal = Get-MgServicePrincipal -Filter "appId eq '$msGraphAppId'" -Select "id,appId,displayName"
29+
$msGraphServicePrincipalResponse = Invoke-MgGraphRequest -Method GET -Uri "v1.0/servicePrincipals?`$filter=appId eq '$msGraphAppId'&`$select=id,appId,displayName"
3030

31-
if (-not $msGraphServicePrincipal) {
31+
if (-not $msGraphServicePrincipalResponse.value -or $msGraphServicePrincipalResponse.value.Count -eq 0) {
3232
throw "Microsoft Graph Service Principal not found in tenant"
3333
}
3434

35+
$msGraphServicePrincipal = $msGraphServicePrincipalResponse.value[0]
36+
3537
# Cache the result
36-
$script:MSGraphServicePrincipalId = $msGraphServicePrincipal.Id
38+
$script:MSGraphServicePrincipalId = $msGraphServicePrincipal.id
3739

38-
Write-Verbose "Microsoft Graph Service Principal found - ID: $script:MSGraphServicePrincipalId, Display Name: $($msGraphServicePrincipal.DisplayName)"
40+
Write-Verbose "Microsoft Graph Service Principal found - ID: $script:MSGraphServicePrincipalId, Display Name: $($msGraphServicePrincipal.displayName)"
3941

4042
return $script:MSGraphServicePrincipalId
4143
}

src/agentid/Invoke-MsIdAgentIdInteractive.ps1

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ function Invoke-MsIdAgentIdInteractive {
7272
try {
7373
$currentUserUpn = (Get-MgContext).Account
7474
# Get user's OID directly using their UPN
75-
$currentUser = Get-MgUser -Filter "userPrincipalName eq '$currentUserUpn'" -Property Id
76-
$currentUserId = $currentUser.Id
75+
$currentUserResponse = Invoke-MgGraphRequest -Method GET -Uri "v1.0/users?`$filter=userPrincipalName eq '$currentUserUpn'&`$select=id"
76+
if ($currentUserResponse.value -and $currentUserResponse.value.Count -gt 0) {
77+
$currentUserId = $currentUserResponse.value[0].id
78+
} else {
79+
$currentUserId = $null
80+
}
7781
}
7882
catch {
7983
$currentUserUpn = $null
@@ -216,7 +220,7 @@ function Invoke-MsIdAgentIdInteractive {
216220

217221
while ($elapsedSeconds -lt $maxWaitSeconds) {
218222
try {
219-
$sp = Get-MgServicePrincipal -ServicePrincipalId $principal1.id -ErrorAction Stop
223+
$sp = Invoke-MgGraphRequest -Method GET -Uri "v1.0/servicePrincipals/$($principal1.id)" -ErrorAction Stop
220224
if ($sp) {
221225
$spAvailable = $true
222226
Write-Host "Service principal is now available" -ForegroundColor Green
@@ -337,7 +341,8 @@ function Invoke-MsIdAgentIdInteractive {
337341
if ($agentIDNeedsUser) {
338342
Write-Host "Creating Agent Users as requested..." -ForegroundColor Yellow
339343
# Get current tenant's domain for UPN
340-
$tenantDomain = (Get-MgOrganization).VerifiedDomains | Where-Object { $_.IsDefault -eq $true } | Select-Object -First 1 -ExpandProperty Name
344+
$orgResponse = Invoke-MgGraphRequest -Method GET -Uri "v1.0/organization?`$select=verifiedDomains"
345+
$tenantDomain = $orgResponse.value[0].verifiedDomains | Where-Object { $_.isDefault -eq $true } | Select-Object -First 1 -ExpandProperty name
341346

342347
# Determine names for the Agent User
343348
if ($useExampleNames) {

src/agentid/New-MsIdAgentIDForAgentIdentityBlueprint.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ function New-MsIdAgentIDForAgentIdentityBlueprint {
125125
catch {
126126
$retryCount++
127127
if ($retryCount -lt $maxRetries) {
128-
Write-Host "Attempt $retryCount failed. Waiting 10 seconds before retry..." -ForegroundColor Yellow
128+
Write-Host "Waiting for propagation..." -ForegroundColor Yellow
129+
Write-Verbose "Attempt $retryCount failed. Waiting 10 seconds before retry..."
130+
129131
Start-Sleep -Seconds 10
130132
}
131133
else {

0 commit comments

Comments
 (0)