Skip to content

Commit

Permalink
Merge pull request #2512 from KoenZomers/GetTenantIdAzureEnvironment
Browse files Browse the repository at this point in the history
Update to `Get-PnPTenandId`
  • Loading branch information
gautamdsheth committed Oct 31, 2022
2 parents 06900d8 + 71f4690 commit 1b43d9d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added additional parameters to `Set-PnPContentType` cmdlet to support SPFx form customizer related properties.[#2456](https://github.com/pnp/powershell/pull/2456)
- Added `-Filter` parameter to `Get-PnPAzureADApp` cmdlet to retrieve specific Azure AD apps based on filter conditions. It suppports simple and advanced queries. [#2477](https://github.com/pnp/powershell/pull/2477)
- Added `Get-PnPDeletedTeam` cmdlet to retrieve all deleted Microsoft Teams teams [#2487](https://github.com/pnp/powershell/pull/2487)
- Added support for sovereign tenants in `Get-PnPTenandId` by utilizing the `-AzureEnvironment` parameter. [#2512](https://github.com/pnp/powershell/pull/2512)

### Changed

Expand Down
40 changes: 31 additions & 9 deletions documentation/Get-PnPTenantId.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ Returns the Tenant ID

### By TenantUrl
```powershell
Get-PnPTenantId -TenantUrl <String> [<CommonParameters>]
Get-PnPTenantId -TenantUrl <String> [-AzureEnvironment <AzureEnvironment>]
```

### By connection
```powershell
Get-PnPTenantId [-Connection <PnPConnection>] [<CommonParameters>]
Get-PnPTenantId [-Connection <PnPConnection>]
```

## DESCRIPTION

Allows to retrieve id of tenant.
Allows to retrieve id of tenant. This does not require an active connection to that tenant.

## EXAMPLES

Expand All @@ -39,36 +39,58 @@ Returns the current Tenant Id. A valid connection with Connect-PnPOnline is requ

### EXAMPLE 2
```powershell
Get-PnPTenantId -TenantUrl "https://contoso.sharepoint.com"
Get-PnPTenantId -TenantUrl contoso.sharepoint.com
```

Returns the Tenant ID for the specified tenant. Can be executed without an active PnP Connection.

### EXAMPLE 3
```powershell
Get-PnPTenantId -TenantUrl contoso.sharepoint.us -AzureEnvironment USGovernment
```

Returns the Tenant ID for the specified US Government tenant. Can be executed without an active PnP Connection.

## PARAMETERS

### -TenantUrl

```yaml
Type: String
Parameter Sets: (All)
Parameter Sets: By URL

Required: False
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -AzureEnvironment
The Azure environment to use for the tenant lookup. It defaults to 'Production' which is the main Azure environment.

```yaml
Type: AzureEnvironment
Parameter Sets: By URL
Accepted values: Production, PPE, China, Germany, USGovernment, USGovernmentHigh, USGovernmentDoD

Required: False
Position: Named
Default value: Production
Accept pipeline input: False
Accept wildcard characters: False
```

### -Connection
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection. If not specified, the current connection will be used.

```yaml
Type: PnPConnection
Parameter Sets: (All)
Parameter Sets: From connection

Required: False
Position: Named
Default value: None
Default value: Current connection
Accept pipeline input: False
Accept wildcard characters: False
```
Expand Down
17 changes: 15 additions & 2 deletions src/Commands/Admin/GetTenantId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.SharePoint.Client;
using PnP.Framework;
using PnP.PowerShell.Commands.Base;
using System;
using System.Management.Automation;
Expand All @@ -16,6 +17,9 @@ public class GetTenantId : BasePSCmdlet
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYURL)]
public string TenantUrl;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_BYURL)]
public AzureEnvironment AzureEnvironment = AzureEnvironment.Production;

[Parameter(Mandatory = false, ParameterSetName = ParameterSet_FROMCONNECTION, HelpMessage = "Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.")]
public PnPConnection Connection = null;

Expand All @@ -31,11 +35,20 @@ protected override void ProcessRecord()
{
if (string.IsNullOrEmpty(TenantUrl) && Connection != null)
{
WriteObject(TenantExtensions.GetTenantIdByUrl(Connection.Url));
WriteObject(TenantExtensions.GetTenantIdByUrl(Connection.Url, Connection.AzureEnvironment));
}
else if (!string.IsNullOrEmpty(TenantUrl))
{
WriteObject(TenantExtensions.GetTenantIdByUrl(TenantUrl));
if(!TenantUrl.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
{
TenantUrl = $"https://{TenantUrl}";
}
if(TenantUrl.IndexOf(".sharepoint.", StringComparison.InvariantCultureIgnoreCase) == -1)
{
throw new InvalidOperationException($"Please provide the sharepoint domain, e.g. https://contoso.sharepoint.com");
}

WriteObject(TenantExtensions.GetTenantIdByUrl(TenantUrl, AzureEnvironment));
}
else
{
Expand Down

0 comments on commit 1b43d9d

Please sign in to comment.