-
Notifications
You must be signed in to change notification settings - Fork 10
Add Agent365ToolsServicePrincipalProdPublic script #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
scripts/cli/Auth/New-Agent365ToolsServicePrincipalProdPublic.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| #requires -Version 7.0 | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Creates the Agent 365 Tools Service Principal in your tenant (Admin only). | ||
|
|
||
| .DESCRIPTION | ||
| This script creates the Service Principal for Agent 365 Tools in your Microsoft Entra ID tenant. | ||
| This is a ONE-TIME operation per tenant that requires admin permissions. | ||
|
|
||
| After the Service Principal is created, regular users can create their own app | ||
| registrations without needing admin rights. | ||
|
|
||
| .EXAMPLE | ||
| .\New-Agent365ToolsServicePrincipalProdPublic.ps1 | ||
|
|
||
| .NOTES | ||
| Requires: Admin permissions to create Service Principals | ||
| This script only needs to be run ONCE per tenant. | ||
| #> | ||
|
|
||
| $resourceId = "ea9ffc3e-8a23-4a7d-836d-234d7c7565c1" | ||
|
|
||
| Write-Host "========================================" -ForegroundColor Cyan | ||
| Write-Host "Service Principal Creation for the 'Agent 365 Tools' application (Admin Only)" -ForegroundColor Cyan | ||
| Write-Host "========================================" -ForegroundColor Cyan | ||
| Write-Host "" | ||
| Write-Host "⚠ IMPORTANT: This requires admin permissions!" -ForegroundColor Yellow | ||
| Write-Host "⚠ This only needs to be run ONCE per tenant!" -ForegroundColor Yellow | ||
| Write-Host "" | ||
|
|
||
| # Check if Microsoft.Graph module is installed | ||
| Write-Host "Checking for Microsoft.Graph module..." -ForegroundColor Cyan | ||
| if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Applications)) { | ||
| Write-Host "Microsoft.Graph.Applications module not found. Installing..." -ForegroundColor Yellow | ||
| Install-Module Microsoft.Graph.Applications -Scope CurrentUser -Force | ||
| } | ||
|
|
||
| if (-not (Get-Module -ListAvailable -Name Microsoft.Graph.Authentication)) { | ||
| Write-Host "Microsoft.Graph.Authentication module not found. Installing..." -ForegroundColor Yellow | ||
| Install-Module Microsoft.Graph.Authentication -Scope CurrentUser -Force | ||
| } | ||
|
|
||
| # Import required modules | ||
| Import-Module Microsoft.Graph.Applications | ||
| Import-Module Microsoft.Graph.Authentication | ||
|
|
||
| # Connect to Microsoft Graph | ||
| Write-Host "" | ||
| Write-Host "Connecting to Microsoft Graph..." -ForegroundColor Cyan | ||
| Write-Host "⚠ You need admin permissions for this operation." -ForegroundColor Yellow | ||
| Write-Host "" | ||
|
|
||
| try { | ||
| # Request admin scope for creating service principals | ||
| Connect-MgGraph -Scopes "AppRoleAssignment.ReadWrite.All" -NoWelcome | ||
mrunalhirve128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $context = Get-MgContext | ||
| Write-Host "✓ Connected to tenant: $($context.TenantId)" -ForegroundColor Green | ||
| $tenantId = $context.TenantId | ||
| Write-Host "" | ||
| } | ||
| catch { | ||
| Write-Host "✗ Failed to connect to Microsoft Graph" -ForegroundColor Red | ||
| Write-Host $_.Exception.Message -ForegroundColor Red | ||
| exit 1 | ||
| } | ||
|
|
||
| Write-Host "Checking if Service Principal already exists..." -ForegroundColor Cyan | ||
|
|
||
| try { | ||
| $existingSp = Get-MgServicePrincipal -Filter "appId eq '$resourceId'" -ErrorAction SilentlyContinue | ||
|
|
||
| if ($existingSp) { | ||
| Write-Host "" | ||
| Write-Host "========================================" -ForegroundColor Green | ||
| Write-Host "✓ SERVICE PRINCIPAL ALREADY EXISTS" -ForegroundColor Green | ||
| Write-Host "========================================" -ForegroundColor Green | ||
| Write-Host "" | ||
| Write-Host "Details:" -ForegroundColor Cyan | ||
| Write-Host " Display Name: $($existingSp.DisplayName)" -ForegroundColor White | ||
| Write-Host " App ID: $($existingSp.AppId)" -ForegroundColor White | ||
| Write-Host " Service Principal ID: $($existingSp.Id)" -ForegroundColor White | ||
| Write-Host "" | ||
| Write-Host "✓ No action needed - Service Principal is already configured!" -ForegroundColor Green | ||
| Write-Host "" | ||
|
|
||
| Disconnect-MgGraph | Out-Null | ||
| exit 0 | ||
| } | ||
|
|
||
| Write-Host "✓ Service Principal does not exist - proceeding with creation..." -ForegroundColor Yellow | ||
| Write-Host "" | ||
| } | ||
| catch { | ||
| Write-Host "⚠ Warning: Could not check for existing Service Principal" -ForegroundColor Yellow | ||
| Write-Host " Proceeding with creation attempt..." -ForegroundColor Yellow | ||
| Write-Host "" | ||
| } | ||
|
|
||
| Write-Host "Creating Service Principal..." -ForegroundColor Cyan | ||
|
|
||
| try { | ||
| # Create service principal for the resource app | ||
| $spParams = @{ | ||
| AppId = $resourceId | ||
| } | ||
|
|
||
| $resourceSp = New-MgServicePrincipal -BodyParameter $spParams | ||
|
|
||
| Write-Host "" | ||
| Write-Host "========================================" -ForegroundColor Green | ||
| Write-Host "✓ SERVICE PRINCIPAL CREATED SUCCESSFULLY!" -ForegroundColor Green | ||
| Write-Host "========================================" -ForegroundColor Green | ||
| Write-Host "" | ||
| Write-Host "Details:" -ForegroundColor Cyan | ||
| Write-Host " Display Name: $($resourceSp.DisplayName)" -ForegroundColor White | ||
| Write-Host " App ID: $($resourceSp.AppId)" -ForegroundColor White | ||
| Write-Host " Service Principal ID: $($resourceSp.Id)" -ForegroundColor White | ||
| Write-Host " Tenant ID: $tenantId" -ForegroundColor White | ||
| Write-Host "" | ||
| } | ||
| catch { | ||
| Write-Host "" | ||
| Write-Host "✗ Failed to create Service Principal" -ForegroundColor Red | ||
| Write-Host $_.Exception.Message -ForegroundColor Red | ||
| Write-Host "" | ||
|
|
||
| if ($_.Exception.Message -like "*Insufficient privileges*" -or $_.Exception.Message -like "*Authorization*") { | ||
| Write-Host "⚠ This error usually means you don't have admin permissions." -ForegroundColor Yellow | ||
| Write-Host "" | ||
| Write-Host "Required Permissions:" -ForegroundColor Cyan | ||
| Write-Host " - AppRoleAssignment.ReadWrite.All" -ForegroundColor White | ||
mrunalhirve128 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Write-Host " - Or Global Administrator / Application Administrator role" -ForegroundColor White | ||
| Write-Host "" | ||
| Write-Host "Please contact your Microsoft Entra ID administrator to run this script." -ForegroundColor Yellow | ||
| } | ||
|
|
||
| Disconnect-MgGraph | Out-Null | ||
| exit 1 | ||
| } | ||
|
|
||
| Disconnect-MgGraph | Out-Null | ||
|
|
||
| Write-Host "========================================" -ForegroundColor Cyan | ||
| Write-Host "Setup Complete!" -ForegroundColor Cyan | ||
| Write-Host "========================================" -ForegroundColor Cyan | ||
| Write-Host "" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.