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

Update to Get-PnPTenandId #2512

Merged
merged 3 commits into from
Oct 31, 2022
Merged
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
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