Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions Segment/Segment/MFA Push/getSecretMicrosoftAuth.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
param(
[Parameter(Mandatory = $false)]
[string]$TenantId
)

# Install only the necessary Microsoft.Graph submodules
$requiredModules = @(
"Microsoft.Graph.Authentication",
Expand All @@ -12,11 +17,34 @@ foreach ($module in $requiredModules) {
}
# Connect to Microsoft Graph with application permissions
# Note: This requires app registration with appropriate permissions
Connect-MgGraph -Scopes "Application.ReadWrite.All" -NoWelcome
$connectParams = @{
Scopes = "Application.ReadWrite.All"
NoWelcome = $true
}
if ($TenantId) {
$connectParams.TenantId = $TenantId
}
try {
Connect-MgGraph @connectParams -ErrorAction Stop
}
catch {
Write-Error "Failed to connect to Microsoft Graph: $($_.Exception.Message)"
exit 1
}
# Define the Azure Multi-Factor Authentication (MFA) App ID
$AzureMFAAppID = "981f26a1-7f43-403b-a875-f8b09b8cd720"
# Get the service principal ObjectId for the MFA App ID
$AzureMFAServicePrincipal = Get-MgServicePrincipal -Filter "appId eq '$AzureMFAAppID'"
# Create the App if missing
if ($null -eq $AzureMFAServicePrincipal) {
write-verbose "AppId not found, creating."
$AzureMFAServicePrincipal = New-MgServicePrincipal -AppId "$AzureMFAAppID"
}
# Fail if not found!
if ($null -eq $AzureMFAServicePrincipal) {
write-error "Application not found and could not be created!"
exit 1
}
$AzureMFAObjID = $AzureMFAServicePrincipal.Id
# Set the end date for the ClientSecret (2 years from now)
$endDate = (Get-Date).AddYears(2)
Expand All @@ -32,4 +60,4 @@ $ClientSecret = $newPassword.SecretText
# Print the ClientSecret
Write-Host "ClientSecret: $ClientSecret"
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Disconnect-MgGraph
Loading