Skip to content

Commit

Permalink
Merge pull request #2959 from KoenZomers/MakeUrlOptionalForManagedIde…
Browse files Browse the repository at this point in the history
…ntity

Making URL optional when using a managed identity
  • Loading branch information
KoenZomers authored Mar 31, 2023
2 parents b0c12d5 + cb96fa5 commit 2c0c5ce
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
10 changes: 5 additions & 5 deletions documentation/Connect-PnPOnline.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ Connect-PnPOnline -Url <String> -AccessToken <String> [-AzureEnvironment <AzureE

### System Assigned Managed Identity
```
Connect-PnPOnline -Url <String> -ManagedIdentity [-ReturnConnection]
Connect-PnPOnline [-Url <String>] -ManagedIdentity [-ReturnConnection]
```

### User Assigned Managed Identity by Client Id
```
Connect-PnPOnline -Url <String> -ManagedIdentity -UserAssignedManagedIdentityClientId <String> [-ReturnConnection]
Connect-PnPOnline [-Url <String>] -ManagedIdentity -UserAssignedManagedIdentityClientId <String> [-ReturnConnection]
```

### User Assigned Managed Identity by Principal Id
```
Connect-PnPOnline -Url <String> -ManagedIdentity -UserAssignedManagedIdentityObjectId <String> [-ReturnConnection]
Connect-PnPOnline [-Url <String>] -ManagedIdentity -UserAssignedManagedIdentityObjectId <String> [-ReturnConnection]
```

### User Assigned Managed Identity by Azure Resource Id
```
Connect-PnPOnline -Url <String> -ManagedIdentity -UserAssignedManagedIdentityAzureResourceId <String> [-ReturnConnection]
Connect-PnPOnline [-Url <String>] -ManagedIdentity -UserAssignedManagedIdentityAzureResourceId <String> [-ReturnConnection]
```

### Environment Variable
Expand Down Expand Up @@ -576,7 +576,7 @@ Type: String
Parameter Sets: Credentials, SharePoint ACS (Legacy) App Only, App-Only with Azure Active Directory, App-Only with Azure Active Directory using a certificate from the Windows Certificate Management Store by thumbprint, SPO Management Shell Credentials, PnP Management Shell / DeviceLogin, Web Login for Multi Factor Authentication, Interactive for Multi Factor Authentication, Access Token, Environment Variable
Aliases:

Required: True
Required: True (Except when using -ManagedIdentity)
Position: 0
Default value: None
Accept pipeline input: True (ByValue)
Expand Down
4 changes: 2 additions & 2 deletions pages/articles/azureautomationrunbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ We're now ready to create a Runbook in which your PnP PowerShell script will run
1. On the Edit PowerShell Runbook page, enter your PnP PowerShell code in the large white area, i.e.:

```powershell
Connect-PnPOnline tenant.sharepoint.com -ManagedIdentity
Connect-PnPOnline -ManagedIdentity
Get-PnPWeb
Get-PnPMicrosoft365Group
```

Once done, click on **Save** at the top of the screen and then on **Test pane** to test your Runbook.
Expand Down
2 changes: 1 addition & 1 deletion pages/articles/azurefunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ using namespace System.Net
param($Request, $TriggerMetadata)
Connect-PnPOnline tenant.sharepoint.com -ManagedIdentity
Connect-PnPOnline -ManagedIdentity
Get-PnPMicrosoft365Group
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Base/ConnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public class ConnectOnline : BasePSCmdlet
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_DEVICELOGIN, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_WEBLOGIN, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_INTERACTIVE, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_SYSTEMASSIGNEDMANAGEDIDENTITY, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYCLIENTID, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYPRINCIPALID, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID, ValueFromPipeline = true)]
[Parameter(Mandatory = false, Position = 0, ParameterSetName = ParameterSet_SYSTEMASSIGNEDMANAGEDIDENTITY, ValueFromPipeline = true)]
[Parameter(Mandatory = false, Position = 0, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYCLIENTID, ValueFromPipeline = true)]
[Parameter(Mandatory = false, Position = 0, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYPRINCIPALID, ValueFromPipeline = true)]
[Parameter(Mandatory = false, Position = 0, ParameterSetName = ParameterSet_USERASSIGNEDMANAGEDIDENTITYBYAZURERESOURCEID, ValueFromPipeline = true)]
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ParameterSet_ENVIRONMENTVARIABLE, ValueFromPipeline = true)]
public string Url;

Expand Down
9 changes: 7 additions & 2 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,13 @@ internal static PnPConnection CreateWithCert(Uri url, string clientId, string te
internal static PnPConnection CreateWithManagedIdentity(Cmdlet cmdlet, string url, string tenantAdminUrl, string userAssignedManagedIdentityObjectId = null, string userAssignedManagedIdentityClientId = null, string userAssignedManagedIdentityAzureResourceId = null)
{
var httpClient = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient();
var resourceUri = new Uri(url);
var defaultResource = $"{resourceUri.Scheme}://{resourceUri.Authority}";
string defaultResource = "https://graph.microsoft.com";
if(url != null)
{
var resourceUri = new Uri(url);
defaultResource = $"{resourceUri.Scheme}://{resourceUri.Authority}";
}

cmdlet.WriteVerbose("Acquiring token for resource " + defaultResource);
var accessToken = TokenHandler.GetManagedIdentityTokenAsync(cmdlet, httpClient, defaultResource, userAssignedManagedIdentityObjectId, userAssignedManagedIdentityClientId, userAssignedManagedIdentityAzureResourceId).GetAwaiter().GetResult();

Expand Down

0 comments on commit 2c0c5ce

Please sign in to comment.