-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into enganga/Update-EntraInvitedUserSponsorsFromI…
…nvitedBy
- Loading branch information
Showing
9 changed files
with
940 additions
and
2 deletions.
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
module/Entra/Microsoft.Entra/DirectoryManagement/Resolve-EntraTenant.ps1
This file contains 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,139 @@ | ||
# ------------------------------------------------------------------------------ | ||
# Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
# Licensed under the MIT License. See License in the project root for license information. | ||
# ------------------------------------------------------------------------------ | ||
function Resolve-EntraTenant { | ||
[CmdletBinding( | ||
DefaultParameterSetName = 'TenantId', | ||
SupportsShouldProcess = $false, | ||
PositionalBinding = $false, | ||
HelpUri = 'https://learn.microsoft.com/', | ||
ConfirmImpact = 'Medium' | ||
)] | ||
[Alias()] | ||
[OutputType([PSCustomObject])] | ||
Param ( | ||
# The TenantId in GUID format (supports multiple values) | ||
[Parameter( | ||
ParameterSetName = 'TenantId', | ||
Mandatory = $true, | ||
Position = 0, | ||
ValueFromPipeline = $true, | ||
ValueFromPipelineByPropertyName = $true, | ||
HelpMessage = "Unique Id(s) of the Tenant(s) in GUID format." | ||
)] | ||
[ValidateScript({ $_ -match "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$" })] | ||
[string[]] | ||
$TenantId, | ||
|
||
# The TenantDomainName in DNS Name format (supports multiple values) | ||
[Parameter( | ||
ParameterSetName = 'DomainName', | ||
Mandatory = $true, | ||
Position = 0, | ||
ValueFromPipeline = $true, | ||
ValueFromPipelineByPropertyName = $true, | ||
HelpMessage = "Unique Domain Name(s) of the Tenant(s) (e.g., contoso.com)." | ||
)] | ||
[ValidateScript({ $_ -match "^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z]{2,})+$" })] | ||
[string[]] | ||
$DomainName, | ||
|
||
# Environment to resolve Azure AD Tenant | ||
[Parameter( | ||
Mandatory = $false, | ||
Position = 1, | ||
ValueFromPipeline = $true, | ||
ValueFromPipelineByPropertyName = $true, | ||
HelpMessage = "Tenant Environment Name (Global, USGov, China, USGovDoD, Germany)." | ||
)] | ||
[ValidateSet("Global", "USGov", "China", "USGovDoD", "Germany")] | ||
[string] | ||
$Environment = "Global", | ||
|
||
# Skip resolving via the OIDC Metadata endpoint | ||
[Parameter(Mandatory=$false, HelpMessage="Specify whether to skip resolving via the OIDC metadata endpoint.")] | ||
[switch] | ||
$SkipOidcMetadataEndpoint | ||
) | ||
|
||
begin { | ||
# Retrieve endpoint information based on the environment | ||
$graphEndpoint = (Get-EntraEnvironment -Name $Environment).GraphEndpoint | ||
$azureAdEndpoint = (Get-EntraEnvironment -Name $Environment).AzureAdEndpoint | ||
|
||
Write-Verbose ("Using $Environment login endpoint: $azureAdEndpoint") | ||
Write-Verbose ("Using $Environment Graph endpoint: $graphEndpoint") | ||
} | ||
|
||
process { | ||
$itemsToProcess = if ($TenantId) { $TenantId } else { $DomainName } | ||
|
||
foreach ($item in $itemsToProcess) { | ||
# Initialize headers and result object | ||
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand | ||
$resolveUri = $null | ||
$resolvedTenant = [ordered]@{ | ||
Environment = $Environment | ||
} | ||
|
||
# Set URI based on parameter set | ||
if ($PSCmdlet.ParameterSetName -eq 'TenantId') { | ||
Write-Verbose ("Resolving Azure AD Tenant by TenantId: $item") | ||
$resolveUri = "$graphEndpoint/v1.0/tenantRelationships/findTenantInformationByTenantId(tenantId='$item')" | ||
$resolvedTenant.ValueFormat = "TenantId" | ||
} | ||
elseif ($PSCmdlet.ParameterSetName -eq 'DomainName') { | ||
Write-Verbose ("Resolving Azure AD Tenant by DomainName: $item") | ||
$resolveUri = "$graphEndpoint/v1.0/tenantRelationships/findTenantInformationByDomainName(domainName='$item')" | ||
$resolvedTenant.ValueFormat = "DomainName" | ||
} | ||
|
||
if ($resolveUri) { | ||
try { | ||
Write-Verbose ("Resolving Tenant Information using MS Graph API.") | ||
$resolve = Invoke-MgGraphRequest -Method Get -Uri $resolveUri -ErrorAction Stop -Headers $customHeaders | | ||
Select-Object tenantId, displayName, defaultDomainName, federationBrandName | ||
|
||
# Populate resolved tenant details | ||
$resolvedTenant.Result = "Resolved" | ||
$resolvedTenant.ResultMessage = "Tenant resolved successfully." | ||
$resolvedTenant.TenantId = $resolve.tenantId | ||
$resolvedTenant.DisplayName = $resolve.displayName | ||
$resolvedTenant.DefaultDomainName = $resolve.defaultDomainName | ||
$resolvedTenant.FederationBrandName = $resolve.federationBrandName | ||
} | ||
catch { | ||
$resolvedTenant.Result = "Error" | ||
$resolvedTenant.ResultMessage = $_.Exception.Message | ||
$resolvedTenant.TenantId = $null | ||
$resolvedTenant.DisplayName = $null | ||
$resolvedTenant.DefaultDomainName = $null | ||
$resolvedTenant.FederationBrandName = $null | ||
} | ||
} | ||
|
||
# Handle OIDC Metadata endpoint resolution | ||
if (-not $SkipOidcMetadataEndpoint) { | ||
$oidcMetadataUri = "$azureAdEndpoint/$item/v2.0/.well-known/openid-configuration" | ||
|
||
try { | ||
$oidcMetadata = Invoke-RestMethod -Method Get -Uri $oidcMetadataUri -ErrorAction Stop -Headers $customHeaders | ||
$resolvedTenant.OidcMetadataResult = "Resolved" | ||
$resolvedTenant.OidcMetadataTenantId = $oidcMetadata.issuer.split("/")[3] | ||
$resolvedTenant.OidcMetadataTenantRegionScope = $oidcMetadata.tenant_region_scope | ||
} | ||
catch { | ||
$resolvedTenant.OidcMetadataResult = "Not Found" | ||
$resolvedTenant.OidcMetadataTenantId = $null | ||
$resolvedTenant.OidcMetadataTenantRegionScope = $null | ||
} | ||
} | ||
else { | ||
$resolvedTenant.OidcMetadataResult = "Skipped" | ||
} | ||
|
||
Write-Output ([PSCustomObject]$resolvedTenant) | ||
} | ||
} | ||
} |
This file contains 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
68 changes: 68 additions & 0 deletions
68
module/EntraBeta/Microsoft.Entra.Beta/Authentication/Get-EntraEnvironment.ps1
This file contains 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,68 @@ | ||
# ------------------------------------------------------------------------------ | ||
# Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
# Licensed under the MIT License. See License in the project root for license information. | ||
# ------------------------------------------------------------------------------ | ||
function Get-EntraEnvironment{ | ||
[CmdletBinding(DefaultParameterSetName = 'GetQuery')] | ||
param ( | ||
[Parameter(ParameterSetName = "GetQuery", Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] | ||
[System.String] $Name) | ||
PROCESS{ | ||
$params = @{} | ||
if ($PSBoundParameters.ContainsKey("Verbose")) { | ||
$params["Verbose"] = $PSBoundParameters["Verbose"] | ||
} | ||
if ($PSBoundParameters.ContainsKey("Debug")) { | ||
$params["Debug"] = $PSBoundParameters["Debug"] | ||
} | ||
if($null -ne $PSBoundParameters["WarningVariable"]) | ||
{ | ||
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"] | ||
} | ||
if($null -ne $PSBoundParameters["InformationVariable"]) | ||
{ | ||
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"] | ||
} | ||
if($null -ne $PSBoundParameters["InformationAction"]) | ||
{ | ||
$params["InformationAction"] = $PSBoundParameters["InformationAction"] | ||
} | ||
if($null -ne $PSBoundParameters["OutVariable"]) | ||
{ | ||
$params["OutVariable"] = $PSBoundParameters["OutVariable"] | ||
} | ||
if($null -ne $PSBoundParameters["OutBuffer"]) | ||
{ | ||
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"] | ||
} | ||
if($null -ne $PSBoundParameters["ErrorVariable"]) | ||
{ | ||
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"] | ||
} | ||
if($null -ne $PSBoundParameters["PipelineVariable"]) | ||
{ | ||
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"] | ||
} | ||
if($null -ne $PSBoundParameters["ErrorAction"]) | ||
{ | ||
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"] | ||
} | ||
if($null -ne $PSBoundParameters["WarningAction"]) | ||
{ | ||
$params["WarningAction"] = $PSBoundParameters["WarningAction"] | ||
} | ||
|
||
if ($null -ne $PSBoundParameters["Name"]) { | ||
$params["Name"] = $PSBoundParameters["Name"] | ||
} | ||
|
||
Write-Debug("============================ TRANSFORMATIONS ============================") | ||
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug | ||
Write-Debug("=========================================================================`n") | ||
|
||
Get-MgEnvironment @params | ||
} | ||
} | ||
|
||
|
||
|
139 changes: 139 additions & 0 deletions
139
module/EntraBeta/Microsoft.Entra.Beta/DirectoryManagement/Resolve-EntraBetaTenant.ps1
This file contains 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,139 @@ | ||
# ------------------------------------------------------------------------------ | ||
# Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
# Licensed under the MIT License. See License in the project root for license information. | ||
# ------------------------------------------------------------------------------ | ||
function Resolve-EntraBetaTenant { | ||
[CmdletBinding( | ||
DefaultParameterSetName = 'TenantId', | ||
SupportsShouldProcess = $false, | ||
PositionalBinding = $false, | ||
HelpUri = 'https://learn.microsoft.com/', | ||
ConfirmImpact = 'Medium' | ||
)] | ||
[Alias()] | ||
[OutputType([PSCustomObject])] | ||
Param ( | ||
# The TenantId in GUID format (supports multiple values) | ||
[Parameter( | ||
ParameterSetName = 'TenantId', | ||
Mandatory = $true, | ||
Position = 0, | ||
ValueFromPipeline = $true, | ||
ValueFromPipelineByPropertyName = $true, | ||
HelpMessage = "Unique Id(s) of the Tenant(s) in GUID format." | ||
)] | ||
[ValidateScript({ $_ -match "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$" })] | ||
[string[]] | ||
$TenantId, | ||
|
||
# The TenantDomainName in DNS Name format (supports multiple values) | ||
[Parameter( | ||
ParameterSetName = 'DomainName', | ||
Mandatory = $true, | ||
Position = 0, | ||
ValueFromPipeline = $true, | ||
ValueFromPipelineByPropertyName = $true, | ||
HelpMessage = "Unique Domain Name(s) of the Tenant(s) (e.g., contoso.com)." | ||
)] | ||
[ValidateScript({ $_ -match "^(?!-)[A-Za-z0-9-]{1,63}(?<!-)(\.[A-Za-z]{2,})+$" })] | ||
[string[]] | ||
$DomainName, | ||
|
||
# Environment to resolve Azure AD Tenant | ||
[Parameter( | ||
Mandatory = $false, | ||
Position = 1, | ||
ValueFromPipeline = $true, | ||
ValueFromPipelineByPropertyName = $true, | ||
HelpMessage = "Tenant Environment Name (Global, USGov, China, USGovDoD, Germany)." | ||
)] | ||
[ValidateSet("Global", "USGov", "China", "USGovDoD", "Germany")] | ||
[string] | ||
$Environment = "Global", | ||
|
||
# Skip resolving via the OIDC Metadata endpoint | ||
[Parameter(Mandatory=$false, HelpMessage="Specify whether to skip resolving via the OIDC metadata endpoint.")] | ||
[switch] | ||
$SkipOidcMetadataEndpoint | ||
) | ||
|
||
begin { | ||
# Retrieve endpoint information based on the environment | ||
$graphEndpoint = (Get-EntraEnvironment -Name $Environment).GraphEndpoint | ||
$azureAdEndpoint = (Get-EntraEnvironment -Name $Environment).AzureAdEndpoint | ||
|
||
Write-Verbose ("Using $Environment login endpoint: $azureAdEndpoint") | ||
Write-Verbose ("Using $Environment Graph endpoint: $graphEndpoint") | ||
} | ||
|
||
process { | ||
$itemsToProcess = if ($TenantId) { $TenantId } else { $DomainName } | ||
|
||
foreach ($item in $itemsToProcess) { | ||
# Initialize headers and result object | ||
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand | ||
$resolveUri = $null | ||
$resolvedTenant = [ordered]@{ | ||
Environment = $Environment | ||
} | ||
|
||
# Set URI based on parameter set | ||
if ($PSCmdlet.ParameterSetName -eq 'TenantId') { | ||
Write-Verbose ("Resolving Azure AD Tenant by TenantId: $item") | ||
$resolveUri = "$graphEndpoint/beta/tenantRelationships/findTenantInformationByTenantId(tenantId='$item')" | ||
$resolvedTenant.ValueFormat = "TenantId" | ||
} | ||
elseif ($PSCmdlet.ParameterSetName -eq 'DomainName') { | ||
Write-Verbose ("Resolving Azure AD Tenant by DomainName: $item") | ||
$resolveUri = "$graphEndpoint/beta/tenantRelationships/findTenantInformationByDomainName(domainName='$item')" | ||
$resolvedTenant.ValueFormat = "DomainName" | ||
} | ||
|
||
if ($resolveUri) { | ||
try { | ||
Write-Verbose ("Resolving Tenant Information using MS Graph API.") | ||
$resolve = Invoke-MgGraphRequest -Method Get -Uri $resolveUri -ErrorAction Stop -Headers $customHeaders | | ||
Select-Object tenantId, displayName, defaultDomainName, federationBrandName | ||
|
||
# Populate resolved tenant details | ||
$resolvedTenant.Result = "Resolved" | ||
$resolvedTenant.ResultMessage = "Tenant resolved successfully." | ||
$resolvedTenant.TenantId = $resolve.tenantId | ||
$resolvedTenant.DisplayName = $resolve.displayName | ||
$resolvedTenant.DefaultDomainName = $resolve.defaultDomainName | ||
$resolvedTenant.FederationBrandName = $resolve.federationBrandName | ||
} | ||
catch { | ||
$resolvedTenant.Result = "Error" | ||
$resolvedTenant.ResultMessage = $_.Exception.Message | ||
$resolvedTenant.TenantId = $null | ||
$resolvedTenant.DisplayName = $null | ||
$resolvedTenant.DefaultDomainName = $null | ||
$resolvedTenant.FederationBrandName = $null | ||
} | ||
} | ||
|
||
# Handle OIDC Metadata endpoint resolution | ||
if (-not $SkipOidcMetadataEndpoint) { | ||
$oidcMetadataUri = "$azureAdEndpoint/$item/v2.0/.well-known/openid-configuration" | ||
|
||
try { | ||
$oidcMetadata = Invoke-RestMethod -Method Get -Uri $oidcMetadataUri -ErrorAction Stop -Headers $customHeaders | ||
$resolvedTenant.OidcMetadataResult = "Resolved" | ||
$resolvedTenant.OidcMetadataTenantId = $oidcMetadata.issuer.split("/")[3] | ||
$resolvedTenant.OidcMetadataTenantRegionScope = $oidcMetadata.tenant_region_scope | ||
} | ||
catch { | ||
$resolvedTenant.OidcMetadataResult = "Not Found" | ||
$resolvedTenant.OidcMetadataTenantId = $null | ||
$resolvedTenant.OidcMetadataTenantRegionScope = $null | ||
} | ||
} | ||
else { | ||
$resolvedTenant.OidcMetadataResult = "Skipped" | ||
} | ||
|
||
Write-Output ([PSCustomObject]$resolvedTenant) | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.