This guide provides step-by-step workflows for common Service Principal management tasks in ScubaGear.
The benefits of using the ScubaGear Service Principal module include:
- Automated configuration of all required ScubaGear permissions
- Built-in validation and auditing
- Reduced risk of misconfiguration
New-ScubaGearServicePrincipal- Creates a new service principal with all required permissions based on products being assessed
Get-ScubaGearAppPermission- Audits an existing service principal's permissionsSet-ScubaGearAppPermission- Fixes missing or incorrect permissions
Get-ScubaGearAppCert- Lists certificates for a service principalNew-ScubaGearAppCert- Adds a new certificate to a service principalRemove-ScubaGearAppCert- Removes a certificate from a service principal
Important
This module is designed exclusively for ScubaGear.
- ✅ Creates a service principal for ScubaGear assessments
- ✅ Manages ScubaGear product permissions (Entra, Exchange, SharePoint, Teams, Defender, Power Platform)
- ✅ Handles certificate management
- ✅ Audits and fixes ScubaGear Service Principal permission issues
- ❌ NOT for general service principal management
- ❌ NOT for other applications or custom permission sets
Before using the Service Principal module, ensure you have:
-
ScubaGear module installed (see Installation Guide)
-
Permissions Required (Entra ID roles and Microsoft Graph API delegated permissions)
[!NOTE]
- Entra ID roles: You need only ONE of the listed roles. The bold role is the least privileged option.
- Graph API scopes: Functions automatically request these permissions when you run them. You must consent when prompted.
Function Entra ID Role (any one of) Microsoft Graph API Scopes New-ScubaGearServicePrincipalApplication Administrator
Global Admin, Security Admin, or Cloud App Admin also workApplication.ReadWrite.AllRoleManagement.ReadWrite.DirectoryUser.ReadGet-ScubaGearAppPermissionGlobal Reader
Global Admin, Directory Writers, Hybrid Identity Admin, Security Admin, Cloud App Admin, or App Admin also workApplication.Read.AllRoleManagement.Read.DirectorySet-ScubaGearAppPermissionPrivileged Role Administrator
Global Admin, Directory Writers, Hybrid Identity Admin, Identity Governance Admin, User Admin, Cloud App Admin, or App Admin also workApplication.ReadWrite.AllAppRoleAssignment.ReadWrite.AllRoleManagement.ReadWrite.DirectoryGet-ScubaGearAppCertGlobal Reader
Global Admin, Cloud App Admin, or App Admin also workApplication.Read.AllNew-ScubaGearAppCertRemove-ScubaGearAppCertApplication Administrator
Global Admin, Cloud App Admin, or App Developer* also workApplication.ReadWrite.All*Application Developer can only manage applications they own
Goal: Create a new service principal with all necessary permissions for ScubaGear automation.
Before creating the service principal, determine:
- Products to assess: Choose from aad, exo, sharepoint, teams, defender, powerPlatform, or use
*for all products - Environment: commercial, gcc, gcchigh, or dod
- Certificate validity: Default 6 months, customizable up to 12 months maximum
Test the creation without making changes:
New-ScubaGearServicePrincipal `
-M365Environment commercial `
-ProductNames 'aad', 'exo', 'sharepoint', 'teams', 'defender', 'powerplatform' `
-WhatIfWhat to look for:
- App registration will be created
- Permissions to be granted
- Roles to be assigned
- Certificate details
Create the Service Principal:
$NewSP = New-ScubaGearServicePrincipal `
-M365Environment commercial `
-ProductNames 'aad', 'exo', 'sharepoint', 'teams', 'defender', 'powerplatform' `
-ServicePrincipalName "ScubaGear-Production"Save the output: The $NewSP variable contains critical information:
AppID- Application (client) IDCertThumbprint- Certificate thumbprint for authentication
Confirm everything was set up correctly:
Get-ScubaGearAppPermission `
-AppID $NewSP.AppID `
-M365Environment $NewSP.M365Environment `
-ProductNames $NewSP.ProductNamesExpected result: Status should show "No action needed - service principal is correctly configured."
- Application ID:
$NewSP.AppID - Tenant ID: Your Microsoft 365 tenant ID
- Certificate Thumbprint:
$NewSP.CertThumbprint - Certificate Location:
Cert:\CurrentUser\My\$($NewSP.CertThumbprint)
Verify the service principal can authenticate:
Invoke-Scuba `
-AppID $NewSP.AppID `
-CertificateThumbprint $NewSP.CertThumbprint `
-M365Environment $NewSP.M365Environment `
-ProductNames $NewSP.ProductNames `
-Organization 'example.onmicrosoft.com'Goal: Check if an existing service principal has correct permissions for ScubaGear.
$Audit = Get-ScubaGearAppPermission `
-AppID "your-app-id-here" `
-M365Environment commercial `
-ProductNames 'aad', 'exo', 'sharepoint', 'teams', 'defender', 'powerPlatform'Check the output properties:
$AuditLook for:
MissingPermissions- Permissions that need to be addedExtraPermissions- Permissions that should be removedMissingRoles- Directory roles that need assignmentDelegatedPermissions- Delegated permissions to remove (ScubaGear requires application permissions only for non-interactive execution)PowerPlatformRegistered- Whether Power Platform is properly registeredFixPermissionIssues- Command to fix issuesStatus- Summary of issues found
If issues are found, check the suggested fix:
$Audit.FixPermissionIssuesThis shows you the exact command to remediate all issues.
Goal: Automatically remediate permission problems identified during audit.
See what changes will be made:
# Pipeline mode (recommended)
$Audit | Set-ScubaGearAppPermission -WhatIf
# Standalone mode
Set-ScubaGearAppPermission -AppID $Audit.AppID -M365Environment $Audit.M365Environment -ProductNames $Audit.ProductNames -WhatIfReview the output:
- Permissions to be added
- Permissions to be removed
- Roles to be assigned
- Power Platform registration changes
Execute the fixes:
$Audit | Set-ScubaGearAppPermissionConfirm all issues were resolved:
Get-ScubaGearAppPermission `
-AppID $Audit.AppID `
-M365Environment $Audit.M365Environment `
-ProductNames 'aad', 'exo', 'sharepoint', 'teams', 'defender', 'powerPlatform'Expected result: Status should now show "No action needed."
Goal: Replace an expiring or expired certificate with a new one.
Review current certificates:
$CertStatus = Get-ScubaGearAppCert `
-AppID "your-app-id-here" `
-M365Environment commercial
# View summary
$CertStatus.CertificatesSummaryLook for:
HasExpiredCerts: True- Expired certificates presentHasExpiringSoon: True- Certificates expiring within 30 days- Individual certificate expiration dates
Add a new certificate before removing the old one (zero-downtime rotation):
$NewCert = New-ScubaGearAppCert `
-AppID "your-app-id-here" `
-M365Environment commercial `
-CertValidityMonths 6Save the new thumbprint: $NewCert.Thumbprint
Test ScubaGear execution with the new certificate:
# Test with new certificate
Invoke-ScubaGear `
-AppID "your-app-id-here" `
-CertificateThumbprint $NewCert.Thumbprint `
-M365Environment commercial `
-ProductNames 'aad'Once verified, remove the old certificate:
# Identify the old certificate thumbprint
$OldThumbprint = 'OldCertificateThumbprintHere'
# Remove it
Remove-ScubaGearAppCert `
-AppID "your-app-id-here" `
-M365Environment commercial `
-CertThumbprint $OldThumbprintConfirm only the new certificate remains:
Get-ScubaGearAppCert `
-AppID "your-app-id-here" `
-M365Environment commercialGoal: Add or remove product permissions from an existing service principal.
Example: You initially set up for AAD only, now need to add Exchange and SharePoint.
$Audit = Get-ScubaGearAppPermission `
-AppID "your-app-id-here" `
-M365Environment commercial `
-ProductNames 'aad', 'exo', 'sharepoint'Expected result: Shows missing permissions for Exchange and SharePoint.
$Audit | Set-ScubaGearAppPermissionExample: You no longer need Teams and Defender permissions.
$Audit = Get-ScubaGearAppPermission `
-AppID "your-app-id-here" `
-M365Environment commercial `
-ProductNames 'aad', 'exo', 'sharepoint'Expected result: Shows extra permissions for Teams and Defender.
$Audit | Set-ScubaGearAppPermissionResult: Removes unnecessary permissions.
| Task | Command |
|---|---|
| Create service principal | New-ScubaGearServicePrincipal |
| Audit permissions | Get-ScubaGearAppPermission |
| Fix issues | Set-ScubaGearAppPermission |
| Check certificates | Get-ScubaGearAppCert |
| Add certificate | New-ScubaGearAppCert |
| Remove certificate | Remove-ScubaGearAppCert |
# Audit and fix in one step
Get-ScubaGearAppPermission -AppID $AppID -M365Environment commercial -ProductNames '*' |
Set-ScubaGearAppPermission
# Find expired certificates
(Get-ScubaGearAppCert -AppID $AppID -M365Environment commercial).CertificatesSummary | Where-Object{$_.Status -eq 'Expired'}
# Export audit results, Can view with [Import-Clixml]
Get-ScubaGearAppPermission -AppID $AppID -M365Environment commercial -ProductNames '*' |
Export-Clixml "audit-results.xml"- Troubleshooting: See Service Principal Troubleshooting for common issues
- Configuration: See Parameters Reference for Invoke-ScubaGear parameters