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

[New Feature] - Get-PnPTenantInfo - to get tenant info of any tenant #3414

Merged
merged 13 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
110 changes: 110 additions & 0 deletions documentation/Get-PnPTenantInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
Module Name: PnP.PowerShell
title: Get-PnPTenantInfo
schema: 2.0.0
applicable: SharePoint Online
external help file: PnP.PowerShell.dll-Help.xml
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPTenantInfo.html
---

# Get-PnPTenantInfo

## SYNOPSIS
Gets information about any tenant

## SYNTAX

### By TenantId
```powershell
Get-PnPTenantInfo -TenantId <String>
```

### By DomainName
```powershell
Get-PnPTenantInfo -DomainName <String>
```

## DESCRIPTION

Gets information about any tenant. If no Domain name or Tenant id is specified, it returns an error message to specify atleast one of TenantId or DomainName

## EXAMPLES

### EXAMPLE 1
```powershell
Get-PnPTenantInfo -TenantId "e65b162c-6f87-4eb1-a24e-1b37d3504663"

tenantId federationBrandName displayName defaultDomainName
-------- ------------------- ----------- -----------------
e65b162c-6f87-4eb1-a24e-1b37d3504663 contoso contoso.onmicrosoft.com
```

Returns the tenant information of the specified TenantId as shown. A valid connection with Connect-PnPOnline is required either


### EXAMPLE 2
```powershell
Get-PnPTenantInfo -DomainName "contoso.com"

tenantId federationBrandName displayName defaultDomainName
-------- ------------------- ----------- -----------------
e65b162c-6f87-4eb1-a24e-1b37d3504663 contoso contoso.onmicrosoft.com
```

Returns the Tenant Information for the tenant contoso.sharepoint.com as shown. A valid connection with Connect-PnPOnline is required either


### EXAMPLE 3
```powershell
Get-PnPTenantInfo -DomainName "contoso.com" -TenantId "e65b162c-6f87-4eb1-a24e-1b37d3504663"

Error:
Get-PnPTenantInfo: Specify atleast one, either DomainName or TenantId, but not both
```

Returns error message as shown

### EXAMPLE 4
```powershell
Get-PnPTenantInfo

Error:
Get-PnPTenantInfo: Please specify either DomainName or TenantId, but not both
```

Returns error message as shown

## PARAMETERS

### -TenantId
The id of the tenant to retrieve the information. You can use either TenantId or DomainName to fetch the Tenant information of any tenant.

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

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

### -DomainName
The Domain name of the tenant to lookup. You can use either TenantId or DomainName to fetch the Tenant information of any tenant. Use the full domain name as "contoso.onmicrosoft.com"

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

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


## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
53 changes: 53 additions & 0 deletions src/Commands/Admin/GetTenantInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using PnP.Core.Transformation;
using PnP.PowerShell.Commands.Base;
using PnP.PowerShell.Commands.Model;
using PnP.PowerShell.Commands.Utilities.REST;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Admin
{
[Cmdlet(VerbsCommon.Get, "PnPTenantInfo")]
public class GetTenantInfo : PnPAdminCmdlet
{
[Parameter(Mandatory = false)]
public string DomainName;

[Parameter(Mandatory = false)]
public string TenantId;

protected override void ExecuteCmdlet()
{
EnsureValidInput();

WriteVerbose("Acquiring access token for Microsoft Graph to look up Tenant");
var graphAccessToken = TokenHandler.GetAccessToken(this, $"https://{Connection.GraphEndPoint}/.default", Connection);
var requestUrl = BuildRequestUrl();

var results = RestHelper.GetAsync<Model.TenantInfo>(Connection.HttpClient, requestUrl, graphAccessToken).GetAwaiter().GetResult();
WriteObject(results, true);
}

private void EnsureValidInput()
{
if (string.IsNullOrEmpty(TenantId) && string.IsNullOrEmpty(DomainName))
NishkalankBezawada marked this conversation as resolved.
Show resolved Hide resolved
{
throw new PSArgumentException("Specify atleast one, either DomainName or TenantId, but not both");
}

if (TenantId != null && DomainName != null)
{
throw new PSArgumentException("Please specify either DomainName or TenantId, but not both");
}
}

private string BuildRequestUrl()
{
var baseUrl = $"https://{Connection.GraphEndPoint}/v1.0/tenantRelationships/";
var query = TenantId != null
? $"microsoft.graph.findTenantInformationByTenantId(tenantId='{TenantId}')"
: $"microsoft.graph.findTenantInformationByDomainName(domainName='{DomainName}')";

return baseUrl + query;
}
}
}
11 changes: 11 additions & 0 deletions src/Commands/Model/TenantInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

namespace PnP.PowerShell.Commands.Model
{
public class TenantInfo
{
public string tenantId { get; set; }
NishkalankBezawada marked this conversation as resolved.
Show resolved Hide resolved
public string federationBrandName { get; set; }
public string displayName { get; set; }
public string defaultDomainName { get; set; }
}
}