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

My two code samples #757

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
171 changes: 171 additions & 0 deletions scripts/aad-get-tenantid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
---
plugin: add-to-gallery-preparation
---

# AAD-Get-TenantID

## Summary

These are 2 practical scripts I have to get Tenant ID from either a domain name or from a Subscriptionis ID.

These are simple, but very useful to be combined in other scripts.


# [PnP PowerShell](#tab/pnpps)

```powershell


function Get-TenantIdFromDomain {
<#
.SYNOPSIS
Get the tenant ID for any Domain.

.DESCRIPTION
Will check and return the tenant ID for any domin, or return $false if no ID is found.

.PARAMETER domain
Any domain name, ex. domain.com

.INPUTS
domain name: domain.com

.OUTPUTS
String or boolean False.

.EXAMPLE
Get-TenantIdFromDomain domain.com
Get-TenantIdFromDomain -domain domain.com
"domain.com" | Get-TenantIdFromDomain

.NOTES
FileName: Get-TenantIdFromDomain.psm1
Author: Daniel Kåven
Contact: @dkaaven
Created: 2022-03-25
Updated: 2024-10-13
Version History:
1.0.0 - (2022-03-25) Script created
1.1.0 - (2024-06-20) Added check for missing domain
1.2.0 - (2024-08-13) Added ability to get data from Pipeline

#>

param(
[CmdletBinding()]
[parameter(
Mandatory = $true,
Position = 0,
HelpMessage = "The domain name of the target tenant.",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[String]$domain
)

# Check if tenant exists
try {
$request = Invoke-WebRequest -Uri https://login.windows.net/$domain/.well-known/openid-configuration
}
catch {
if ($null -eq $request) {
return $false
} else {
Write-Error $_
}
}

# Return tenant ID
$data = ConvertFrom-Json $request.Content
$result = $data.token_endpoint.split('/')[3]
return $result
}

```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]


# [PnP PowerShell](#tab/pnpps)

```powershell

function Get-TenantIdFromSubscriptionId {
<#
.SYNOPSIS
Get the tenant ID from an Azure Subscription ID.
.DESCRIPTION
Will check and return the tenant ID for an Azure Subscription ID or return $false if no ID is found.
Inspired from [Jos Lieben @ lieben.nu](https://www.lieben.nu/liebensraum/2020/08/get-tenant-id-using-azure-subscription-id/)

.PARAMETER subscriptionId
The Azure Subscription ID to check.

.INPUTS
Azure Subscription Id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

.OUTPUTS
String or boolean False.

.EXAMPLE
Get-TenantIdFromSubscriptionId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Get-TenantIdFromSubscriptionId -subscriptionId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | Get-TenantIdFromSubscriptionId

.NOTES
FileName: Get-TenantIdFromSubscriptionId.psm1
Author: Daniel Kåven
Contact: @DKaaven
Created: 2024-08-06
Updated: 2024-08-06
Version history:
1.0.0 - (2024-08-06) Script created
#>
param (
[CmdletBinding()]
[Parameter(
Mandatory = $true,
Position = 0,
HelpMessage = "The Azure Subscription ID",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[Alias("subId")]
[String]$subscriptionId
)
# Check Subscription ID format
$guidPattern = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
if ($subscriptionId -notmatch $guidPattern) {
Write-Error "$subscriptionId is not a valid Azure Subscription ID."
return $false
}
$response = try {(Invoke-WebRequest -UseBasicParsing -Uri "https://management.azure.com/subscriptions/$($subscriptionId)?api-version=2015-01-01" -ErrorAction Stop).BaseResponse} catch { $_.Exception.Response }
$stringHeader = $response.Headers.ToString()
$tenantId = $stringHeader.SubString($stringHeader.IndexOf("login.windows.net")+18,36)

# Check if it exist or return false
if ($tenantId -match $guidPattern) {
return $tenantId
}
else {
return $false
}

}

```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]


## Source Credit

Sample first appeared on [https://github.com/dkaaven/M365-Scripts](https://github.com/dkaaven/M365-Scripts)

## Contributors

| Author(s) |
|-----------|
| [Daniel Kåven](https://github.com/dkaaven)|


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/template-script-submission" aria-hidden="true" />
Binary file added scripts/aad-get-tenantid/assets/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/aad-get-tenantid/assets/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions scripts/aad-get-tenantid/assets/template.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
[
{
"name": "<foldername>",
"source": "pnp",
"title": "<title>",
"shortDescription": "",
"url": "https://pnp.github.io/script-samples/<foldername>/README.html",
"longDescription": [
""
],
"creationDateTime": "2023-01-20",
"updateDateTime": "2023-01-20",
"products": [
"SharePoint",
"Office",
"Graph",
"PowerApps",
"Teams",
"Power Automate",
"Microsoft Todo",
"Azure",
"Microsoft 365 Copilot"
],
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "1.11.0"
},
{
"key": "CLI-FOR-MICROSOFT365",
"value": "5.6.0"
},
{
"key": "GRAPH-POWERSHELL",
"value": "1.0.0"
},
{
"key": "SPO-MANAGEMENT-SHELL",
"value": "16.0.21116.12000"
},
{
"key": "AZURE-CLI",
"value": "2.27.0"
},
{
"key": "MICROSOFTTEAMS-POWERSHELL",
"value": "3.0.0"
},
{
"key": "POWERAPPS-POWERSHELL",
"value": "2.0.0"
},
{
"key": "POWERSHELL",
"value": "7.2.0"
},
{
"key": "MICROSOFTWHITEBOARDADMIN",
"value": "1.5.0"
}
],
"categories": [
"Modernize",
"Data",
"Deploy",
"Provision",
"Configure",
"Report",
"Security",
"AI",
"Microsoft 365 Copilot"
],
"tags": [
"<Cmdlets-Used>"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/<foldername>/assets/preview.png",
"alt": "Preview of the sample <title>"
}
],
"authors": [
{
"gitHubAccount": "<your-github-username>",
"company": "",
"pictureUrl": "https://github.com/<github-username>.png",
"name": "<Full Name>"
}
],
"references": [
{
"name": "Want to learn more about PnP PowerShell and the cmdlets",
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
"url": "https://aka.ms/pnp/powershell"
},
{
"name": "Want to learn more about CLI for Microsoft 365 and the commands",
"description": "Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.",
"url": "https://aka.ms/cli-m365"
},
{
"name": "Want to learn more about Microsoft Graph PowerShell SDK and the cmdlets",
"description": "Check out the Microsoft Graph PowerShell SDK documentation site to get started and for the reference to the cmdlets.",
"url": "https://learn.microsoft.com/graph/powershell/get-started"
},
{
"name": "Want to learn more about Power Apps PowerShell and the cmdlets",
"description": "Check out the Power Apps PowerShell documentation site to get started and for the reference to the cmdlets.",
"url": "https://learn.microsoft.com/power-platform/admin/powerapps-powershell"
},
{
"name": "Introduction to the SharePoint Online Management Shell",
"description": "Check out the SPO Management Shell documentation site to get started and for the reference to the cmdlets.",
"url": "https://learn.microsoft.com/powershell/sharepoint/sharepoint-online/introduction-sharepoint-online-management-shell"
},
{
"name": "Want to learn more about Microsoft Teams PowerShell and the cmdlets",
"description": "Check out the Microsoft Teams PowerShell documentation site to get started and for the reference to the cmdlets.",
"url": "https://learn.microsoft.com/microsoftteams/teams-powershell-overview"
},
{
"name": "Want to learn more about Azure CLI and the commands",
"description": "Check out the Azure CLI documentation site to get started and for the reference to the commands.",
"url": "https://learn.microsoft.com/cli/azure/"
},
{
"name":"Want to learn more about Whiteboard Admin cmdlets",
"description":"Check out the Whiteboard Admin cmdlets documentation site to get started and for the reference to the commands.",
"url": "https://learn.microsoft.com/powershell/module/whiteboard"
}
]
}
]
65 changes: 65 additions & 0 deletions scripts/aad-get-tenantid/modules/Get-TenantIdFromDomain.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

function Get-TenantIdFromDomain {
<#
.SYNOPSIS
Get the tenant ID for any Domain.

.DESCRIPTION
Will check and return the tenant ID for any domin, or return $false if no ID is found.

.PARAMETER domain
Any domain name, ex. domain.com

.INPUTS
domain name: domain.com

.OUTPUTS
String or boolean False.

.EXAMPLE
Get-TenantIdFromDomain domain.com
Get-TenantIdFromDomain -domain domain.com
"domain.com" | Get-TenantIdFromDomain

.NOTES
FileName: Get-TenantIdFromDomain.psm1
Author: Daniel Kåven
Contact: @dkaaven
Created: 2022-03-25
Updated: 2024-10-13
Version History:
1.0.0 - (2022-03-25) Script created
1.1.0 - (2024-06-20) Added check for missing domain
1.2.0 - (2024-08-13) Added ability to get data from Pipeline

#>

param(
[CmdletBinding()]
[parameter(
Mandatory = $true,
Position = 0,
HelpMessage = "The domain name of the target tenant.",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[String]$domain
)

# Check if tenant exists
try {
$request = Invoke-WebRequest -Uri https://login.windows.net/$domain/.well-known/openid-configuration
}
catch {
if ($null -eq $request) {
return $false
} else {
Write-Error $_
}
}

# Return tenant ID
$data = ConvertFrom-Json $request.Content
$result = $data.token_endpoint.split('/')[3]
return $result
}
Loading