Skip to content

Commit

Permalink
Merge pull request #1919 from KoenZomers/ConnectionReferenceFix
Browse files Browse the repository at this point in the history
Various fixes when using specific Connections
  • Loading branch information
KoenZomers authored Jun 3, 2022
2 parents e7f3776 + f251487 commit 2f6acf1
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 157 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added capability to Debug the module in Visual Studio. [#1880](https://github.com/pnp/powershell/pull/1880)
- Added `Set-PnPTeamsChannelUser` cmdlet to update the role of user in a private channel. [#1865](https://github.com/pnp/powershell/pull/1865)
- Added `Restart-PnPFlowRun` which allows for a failed Power Automate flow run to be retried [#1915](https://github.com/pnp/powershell/pull/1915)
- Added optional `-Connection` parameter to `Get-PnPConnection`, `Get-PnPContext` and `Set-PnPContext` which allows for using any of these for a specific connection [#1919](https://github.com/pnp/powershell/pull/1919)
- Added `-IncludeDeprecated` parameter to `Get-PnPTerm` cmdlet to fetch deprecated terms if specified [#1903](https://github.com/pnp/powershell/pull/1903)
- Added optional `-ValidateConnection` to `Connect-PnPOnline` which will check if the site you are connecting to exists and if not, will throw an exception [#1924](https://github.com/pnp/powershell/pull/1924)

Expand All @@ -63,6 +64,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `Invoke-PnPSPRestMethod` invalid parsing for SharePoint number columns. [#1877](https://github.com/pnp/powershell/pull/1879)
- Fix issue with `Add/Set-PnPListItem` not throwing correct exception for invalid taxonomy values. [#1870](https://github.com/pnp/powershell/pull/1870)
- Fixed `Sync-PnPSharePointUserProfilesFromAzureActiveDirectory` throwing an "Object reference not set to an instance of an object" exception when providing an empty users collection or incorrect user mapping [#1896](https://github.com/pnp/powershell/pull/1896)
- Fixed `Connect-PnPOnline -ReturnConnection` also setting the current connection instead of just the returned connection [#1919](https://github.com/pnp/powershell/pull/1919)
- Fixed `Disconnect-PnPOnline -Connection` also disconnecting other connections next to the provided connection [#1919](https://github.com/pnp/powershell/pull/1919)
- Fixed `Set-PnPContext` not properly applying the provided context [#1919](https://github.com/pnp/powershell/pull/1919)

### Removed
- Removed `Get-PnPAvailableClientSideComponents`. Use `Get-PnPPageComponent -Page -ListAvailable` instead. [#1833](https://github.com/pnp/powershell/pull/1833)
Expand Down
20 changes: 16 additions & 4 deletions documentation/Get-PnPConnection.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ title: Get-PnPConnection
# Get-PnPConnection

## SYNOPSIS
Returns the current context
Returns the current connection

## SYNTAX

```powershell
Get-PnPConnection [<CommonParameters>]
Get-PnPConnection [-Connection <PnPConnection>] [<CommonParameters>]
```

## DESCRIPTION
Expand All @@ -32,8 +32,20 @@ This will put the current connection for use with the -Connection parameter on o

## PARAMETERS

## RELATED LINKS
### -Connection
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by specifying -ReturnConnection on Connect-PnPOnline. If not provided, the connection will be retrieved from the current context.

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

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
22 changes: 17 additions & 5 deletions documentation/Get-PnPContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ title: Get-PnPContext
# Get-PnPContext

## SYNOPSIS
Returns the current context
Returns the current SharePoint Online CSOM context

## SYNTAX

```powershell
Get-PnPContext [<CommonParameters>]
Get-PnPContext [-Connection <PnPConnection>] [<CommonParameters>]
```

## DESCRIPTION
Returns a Client Side Object Model context
Returns a SharePoint Online Client Side Object Model (CSOM) context

## EXAMPLES

Expand All @@ -43,8 +43,20 @@ Get-PnPList # returns the lists from site A

## PARAMETERS

## RELATED LINKS
### -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. If not provided, the context of the connection will be retrieved from the current connection.

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

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
30 changes: 13 additions & 17 deletions documentation/Get-PnPList.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@ This examples shows how to do wildcard searches on the list URL. It returns all

## PARAMETERS

### -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.

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

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
### -Identity
The ID, name or Url (Lists/MyList) of the list

Expand Down Expand Up @@ -95,10 +81,20 @@ 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.
```yaml
Type: PnPConnection
Parameter Sets: (All)

## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
21 changes: 17 additions & 4 deletions documentation/Set-PnPContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Set the ClientContext
## SYNTAX

```powershell
Set-PnPContext [-Context] <ClientContext> [<CommonParameters>]
Set-PnPContext -Context <ClientContext> [-Connection <PnPConnection>] [<CommonParameters>]
```

## DESCRIPTION
Sets the Client Context to use by the cmdlets, which allows easy context switching. See examples for details.
Sets the Client Context to be used by the cmdlets, which allows easy context switching. See examples for details.

## EXAMPLES

Expand Down Expand Up @@ -50,7 +50,20 @@ Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```
## RELATED LINKS
### -Connection
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by specifying -ReturnConnection on Connect-PnPOnline. If not provided, the connection will be retrieved from the current context.
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
```yaml
Type: PnPConnection
Parameter Sets: (All)

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```
## RELATED LINKS
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
5 changes: 0 additions & 5 deletions src/Commands/Base/BasePSCmdlet.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Management.Automation;
using System.Reflection;
using PnP.PowerShell.Commands.Attributes;

namespace PnP.PowerShell.Commands.Base
Expand Down
12 changes: 8 additions & 4 deletions src/Commands/Base/ConnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ protected void Connect(ref CancellationToken cancellationToken)
#else
WriteVerbose($"PnP PowerShell Cmdlets ({Assembly.GetExecutingAssembly().GetName().Version})");
#endif
PnPConnection.Current = connection;
if (CreateDrive && PnPConnection.Current.Context != null)

if (CreateDrive && connection.Context != null)
{
var provider = SessionState.Provider.GetAll().FirstOrDefault(p => p.Name.Equals(SPOProvider.PSProviderName, StringComparison.InvariantCultureIgnoreCase));
if (provider != null)
Expand All @@ -306,9 +306,9 @@ protected void Connect(ref CancellationToken cancellationToken)
}
}

if (PnPConnection.Current.Url != null)
if (connection.Url != null)
{
var hostUri = new Uri(PnPConnection.Current.Url);
var hostUri = new Uri(connection.Url);
Environment.SetEnvironmentVariable("PNPPSHOST", hostUri.Host);
Environment.SetEnvironmentVariable("PNPPSSITE", hostUri.LocalPath);
}
Expand Down Expand Up @@ -340,6 +340,10 @@ protected void Connect(ref CancellationToken cancellationToken)
{
WriteObject(connection);
}
else
{
PnPConnection.Current = connection;
}
}

#region Connect Types
Expand Down
51 changes: 11 additions & 40 deletions src/Commands/Base/DisconnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Management.Automation;
using System.Reflection;
using PnP.PowerShell.Commands.Provider;
using PnP.PowerShell.Commands.Model;

namespace PnP.PowerShell.Commands.Base
{
Expand All @@ -18,35 +17,24 @@ public class DisconnectOnline : PSCmdlet
protected override void ProcessRecord()
{
// If no specific connection has been passed in, take the connection from the current context
if (Connection == null)
{
Connection = PnPConnection.Current;
}
if (Connection?.Certificate != null)
var connection = Connection ?? PnPConnection.Current;

if (connection?.Certificate != null)
{
if (Connection != null && Connection.DeleteCertificateFromCacheOnDisconnect)
if (connection != null && connection.DeleteCertificateFromCacheOnDisconnect)
{
PnPConnection.CleanupCryptoMachineKey(Connection.Certificate);
PnPConnection.CleanupCryptoMachineKey(connection.Certificate);
}
Connection.Certificate = null;
}
var success = false;
if (Connection != null)
{
success = DisconnectProvidedService(Connection);
}
else
{
success = DisconnectCurrentService();
connection.Certificate = null;
}

var success = DisconnectProvidedService(ref connection);

if (!success)
{
throw new InvalidOperationException(Properties.Resources.NoConnectionToDisconnect);
}

// clear credentials
PnPConnection.Current = null;

var provider = SessionState.Provider.GetAll().FirstOrDefault(p => p.Name.Equals(SPOProvider.PSProviderName, StringComparison.InvariantCultureIgnoreCase));
if (provider != null)
{
Expand All @@ -59,7 +47,7 @@ protected override void ProcessRecord()
}
}

internal static bool DisconnectProvidedService(PnPConnection connection)
internal static bool DisconnectProvidedService(ref PnPConnection connection)
{
Environment.SetEnvironmentVariable("PNPPSHOST", string.Empty);
Environment.SetEnvironmentVariable("PNPPSSITE", string.Empty);
Expand All @@ -71,22 +59,5 @@ internal static bool DisconnectProvidedService(PnPConnection connection)
connection = null;
return true;
}

internal static bool DisconnectCurrentService()
{
Environment.SetEnvironmentVariable("PNPPSHOST", string.Empty);
Environment.SetEnvironmentVariable("PNPPSSITE", string.Empty);

if (PnPConnection.Current == null)
{
return false;
}
else
{
PnPConnection.Current.Context = null;
PnPConnection.Current = null;
return true;
}
}
}
}
}
24 changes: 3 additions & 21 deletions src/Commands/Base/GetConnection.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
using System.Management.Automation;

using System;
using PnP.PowerShell.Commands.Properties;

namespace PnP.PowerShell.Commands.Base
{
[Cmdlet(VerbsCommon.Get, "PnPConnection")]
[OutputType(typeof(PnPConnection))]
public class GetPnPConnection : PSCmdlet
public class GetPnPConnection : PnPSharePointCmdlet
{

protected override void BeginProcessing()
{
base.BeginProcessing();

if (PnPConnection.Current == null)
{
throw new InvalidOperationException(Resources.NoSharePointConnection);
}
if (PnPConnection.Current.Context == null)
{
throw new InvalidOperationException(Resources.NoSharePointConnection);
}
}

protected override void ProcessRecord()
{
WriteObject(PnPConnection.Current);
WriteObject(Connection);
}
}
}
}
24 changes: 3 additions & 21 deletions src/Commands/Base/GetContext.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
using System.Management.Automation;

using System;
using PnP.PowerShell.Commands.Properties;

namespace PnP.PowerShell.Commands.Base
{
[Cmdlet(VerbsCommon.Get, "PnPContext")]
[OutputType(typeof(Microsoft.SharePoint.Client.ClientContext))]
public class GetSPOContext : PSCmdlet
public class GetSPOContext : PnPSharePointCmdlet
{

protected override void BeginProcessing()
{
base.BeginProcessing();

if (PnPConnection.Current == null)
{
throw new InvalidOperationException(Resources.NoSharePointConnection);
}
if (PnPConnection.Current.Context == null)
{
throw new InvalidOperationException(Resources.NoSharePointConnection);
}
}

protected override void ProcessRecord()
{
WriteObject(PnPConnection.Current.Context);
WriteObject(Connection.Context);
}
}
}
}
Loading

0 comments on commit 2f6acf1

Please sign in to comment.