From 169c61aa7aaf1b289759a7b0645a51e540b21173 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sat, 5 Jul 2025 17:31:23 +0300 Subject: [PATCH] Add tenantId if not specified by fetching it from URL --- documentation/Connect-PnPOnline.md | 2 +- pages/articles/authentication.md | 2 +- src/Commands/Base/ConnectOnline.cs | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/documentation/Connect-PnPOnline.md b/documentation/Connect-PnPOnline.md index 024d302ea..452a7d277 100644 --- a/documentation/Connect-PnPOnline.md +++ b/documentation/Connect-PnPOnline.md @@ -56,7 +56,7 @@ Connect-PnPOnline [-ReturnConnection] [-Url] [-CreateDrive] [-DriveName ### DeviceLogin ```powershell -Connect-PnPOnline [-ReturnConnection] [-Url] [-PersistLogin] [-CreateDrive] [-DriveName ] [-DeviceLogin] +Connect-PnPOnline [-ReturnConnection] [-Url] [-PersistLogin] [-CreateDrive] [-DriveName ] [-DeviceLogin] -Tenant [-ClientId ] [-AzureEnvironment ] [-ValidateConnection] [-MicrosoftGraphEndPoint ] [-AzureADLoginEndPoint ] [-Connection ] diff --git a/pages/articles/authentication.md b/pages/articles/authentication.md index 96868bd3e..3114c1ce4 100644 --- a/pages/articles/authentication.md +++ b/pages/articles/authentication.md @@ -25,7 +25,7 @@ In some scenarios it can happen that you need to authenticate to PnP PowerShell Connecting can be done using: ```powershell -Connect-PnPOnline [yourtenant].sharepoint.com -DeviceLogin -Tenant .onmicrosoft.com -ClientId +Connect-PnPOnline [yourtenant].sharepoint.com -DeviceLogin -ClientId ``` When running this line, it will prompt in text to go to https://microsoft.com/devicelogin on any device and log in using a specific code which will be shown in the text. You can perform this step on any device. This does not have to be the same device as you're using PnP PowerShell on. After going to that website and entering the code shown in the text, you can complete the interactive login process as normal, including any multi factor authentication requirements that might have been set up for your account. Once the authentication completes, PnP PowerShell will automatically detect this and will be ready to be used. diff --git a/src/Commands/Base/ConnectOnline.cs b/src/Commands/Base/ConnectOnline.cs index 8a3df771c..22e5e31c1 100644 --- a/src/Commands/Base/ConnectOnline.cs +++ b/src/Commands/Base/ConnectOnline.cs @@ -567,6 +567,10 @@ private PnPConnection ConnectDeviceLogin() messageWriter.LogDebug("Using Managed AppId from secure store"); } } + if (string.IsNullOrWhiteSpace(Tenant)) + { + Tenant = TenantExtensions.GetTenantIdByUrl(Url, AzureEnvironment); + } var returnedConnection = PnPConnection.CreateWithDeviceLogin(clientId, Url, Tenant, messageWriter, AzureEnvironment, cancellationTokenSource, PersistLogin, Host); connection = returnedConnection;