Skip to content
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

Rewrite for Powershell and pwsh Core support #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
91 changes: 74 additions & 17 deletions Segment/MFA Push/getSecretMicrosoftAuth.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,82 @@
# Install the AzureAD module if not already installed
if (-not (Get-Module -ListAvailable -Name AzureAD)) {
Install-Module -Name AzureAD -Force
}
[CmdletBinding()]
param (
[Parameter()]
[string] $TenantId
)

# Import the AzureAD module
Import-Module AzureAD
$IsElevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

# Connect to AzureAD
Connect-AzureAD
# Set the end date for the ClientSecret
$endDate = (Get-Date).AddYears(2)

# Define the Azure Multi-Factor Authentication (MFA) App ID
$AzureMFAAppID = "981f26a1-7f43-403b-a875-f8b09b8cd720"
# Pwsh Core flavor because the module is different
if ($PSVersionTable.PSEdition -eq "Core") {

# Get the AzureAD service principal ObjectId for the MFA App ID
$AzureMFAObjID = Get-AzureADServicePrincipal -Filter "AppId eq '$AzureMFAAppID'" | Select-Object -ExpandProperty ObjectId
if (-Not (Get-Module -ListAvailable -Name Az)) {
if ($IsElevated) {
Install-Module -Name Az -AllowClobber -Force
} else {
Write-Host -ForegroundColor Red "AzureAD Module is not installed and you are not running in an elevated session."
Write-Host -ForegroundColor Red "Please run Powershell as an Administrator and try again."
return
}

}

# Set the end date for the ClientSecret
$endDate = (Get-Date).AddYears(2)
if (-Not (Get-AzContext).Account | Out-Null) {
$login = "Connect-AzAccount"
if ($TenantId) { $login += " -Tenant $($TenantId)" }
else { write-warning "skipping Tenant specificity on connecting to AzureAD"}
Invoke-Expression $login | Out-Null
}

# Get the Azure Multi-Factor Authentication (MFA) App ID from Zero Networks Enterprise App (Gallery Entity)
$AzureMFAAppID = (Get-AzADServicePrincipal -DisplayNameBeginsWith 'zero networks').AppId

# Get the AzureAD service principal ObjectId for the MFA App ID
$AzureMFAObj = Get-AzADServicePrincipal -ApplicationId $AzureMFAAppID

# Generate a new secret and set its expiry based on $endDate variable
$ClientSecret = New-AzADSpCredential -ObjectId $AzureMFAObj.Id -EndDate $endDate | Select-Object -ExpandProperty secretText

# Generate a new ClientSecret and retrieve the value
$ClientSecret = New-AzureADServicePrincipalPasswordCredential -ObjectId $AzureMFAObjID -EndDate $endDate | Select-Object -ExpandProperty Value

} else {
if (-not (Get-Module -ListAvailable -Name AzureAD)) {
if ($IsElevated) {
Install-Module -Name AzureAD -Repository PSGallery -AllowClobber -Force
} else {
Write-Host -ForegroundColor Red "AzureAD Module is not installed and you are not running in an elevated session."
Write-Host -ForegroundColor Red "Please run Powershell as an Administrator and try again."
return
}
}

# Connect to AzureAD
try {Get-AzureADTenantDetail | Out-Null}
catch {
$login = "Connect-AzureAD"

if ($TenantId) { $login += " -TenantId $($TenantId)" }
else { write-warning "skipping Tenant specificity on connecting to AzureAD"}

Invoke-Expression $login | Out-Null
}

# Get the Azure Multi-Factor Authentication (MFA) App ID from Zero Networks Enterprise App (Gallery Entity)
$AzureMFAAppID = (Get-AzureADServicePrincipal -Filter "DisplayName eq 'Zero Networks'").AppId

# Get the AzureAD service principal ObjectId for the MFA App ID
$AzureMFAObjID = Get-AzureADServicePrincipal -Filter "AppId eq '$AzureMFAAppID'" | Select-Object -ExpandProperty ObjectId

# Generate a new secret and set its expiry based on $endDate variable
$ClientSecret = New-AzureADServicePrincipalPasswordCredential -ObjectId $AzureMFAObjID -EndDate $endDate | Select-Object -ExpandProperty Value
}

# Print the ClientSecret
Write-Host "ClientSecret: $ClientSecret"
if ($ClientSecret) {
Write-Host "ClientSecret: $($ClientSecret)" -ForegroundColor Green
Set-Clipboard -Value $ClientSecret
Write-Host -ForegroundColor DarkYellow "Copied to the clipboard if your OS supports it!"
} else {
Write-Warning "No secret was found. Could be an authorization issue! Check that you have the proper permissions and that you are connected to the intended Tenant context. You can change these using -TenantId."
}