Skip to content

Commit 2f6acf1

Browse files
authored
Merge pull request #1919 from KoenZomers/ConnectionReferenceFix
Various fixes when using specific Connections
2 parents e7f3776 + f251487 commit 2f6acf1

15 files changed

+140
-157
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
3737
- Added capability to Debug the module in Visual Studio. [#1880](https://github.com/pnp/powershell/pull/1880)
3838
- Added `Set-PnPTeamsChannelUser` cmdlet to update the role of user in a private channel. [#1865](https://github.com/pnp/powershell/pull/1865)
3939
- Added `Restart-PnPFlowRun` which allows for a failed Power Automate flow run to be retried [#1915](https://github.com/pnp/powershell/pull/1915)
40+
- 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)
4041
- Added `-IncludeDeprecated` parameter to `Get-PnPTerm` cmdlet to fetch deprecated terms if specified [#1903](https://github.com/pnp/powershell/pull/1903)
4142
- 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)
4243

@@ -63,6 +64,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6364
- Fixed `Invoke-PnPSPRestMethod` invalid parsing for SharePoint number columns. [#1877](https://github.com/pnp/powershell/pull/1879)
6465
- Fix issue with `Add/Set-PnPListItem` not throwing correct exception for invalid taxonomy values. [#1870](https://github.com/pnp/powershell/pull/1870)
6566
- 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)
67+
- Fixed `Connect-PnPOnline -ReturnConnection` also setting the current connection instead of just the returned connection [#1919](https://github.com/pnp/powershell/pull/1919)
68+
- Fixed `Disconnect-PnPOnline -Connection` also disconnecting other connections next to the provided connection [#1919](https://github.com/pnp/powershell/pull/1919)
69+
- Fixed `Set-PnPContext` not properly applying the provided context [#1919](https://github.com/pnp/powershell/pull/1919)
6670

6771
### Removed
6872
- Removed `Get-PnPAvailableClientSideComponents`. Use `Get-PnPPageComponent -Page -ListAvailable` instead. [#1833](https://github.com/pnp/powershell/pull/1833)

documentation/Get-PnPConnection.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ title: Get-PnPConnection
1010
# Get-PnPConnection
1111

1212
## SYNOPSIS
13-
Returns the current context
13+
Returns the current connection
1414

1515
## SYNTAX
1616

1717
```powershell
18-
Get-PnPConnection [<CommonParameters>]
18+
Get-PnPConnection [-Connection <PnPConnection>] [<CommonParameters>]
1919
```
2020

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

3333
## PARAMETERS
3434

35-
## RELATED LINKS
35+
### -Connection
36+
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.
37+
38+
```yaml
39+
Type: PnPConnection
40+
Parameter Sets: (All)
3641

37-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
42+
Required: False
43+
Position: Named
44+
Default value: None
45+
Accept pipeline input: False
46+
Accept wildcard characters: False
47+
```
3848
49+
## RELATED LINKS
3950
51+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Get-PnPContext.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ title: Get-PnPContext
1010
# Get-PnPContext
1111

1212
## SYNOPSIS
13-
Returns the current context
13+
Returns the current SharePoint Online CSOM context
1414

1515
## SYNTAX
1616

1717
```powershell
18-
Get-PnPContext [<CommonParameters>]
18+
Get-PnPContext [-Connection <PnPConnection>] [<CommonParameters>]
1919
```
2020

2121
## DESCRIPTION
22-
Returns a Client Side Object Model context
22+
Returns a SharePoint Online Client Side Object Model (CSOM) context
2323

2424
## EXAMPLES
2525

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

4444
## PARAMETERS
4545

46-
## RELATED LINKS
46+
### -Connection
47+
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.
48+
49+
```yaml
50+
Type: PnPConnection
51+
Parameter Sets: (All)
4752

48-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
53+
Required: False
54+
Position: Named
55+
Default value: None
56+
Accept pipeline input: False
57+
Accept wildcard characters: False
58+
```
4959
60+
## RELATED LINKS
5061
62+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Get-PnPList.md

+13-17
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ This examples shows how to do wildcard searches on the list URL. It returns all
5353

5454
## PARAMETERS
5555

56-
### -Connection
57-
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.
58-
59-
```yaml
60-
Type: PnPConnection
61-
Parameter Sets: (All)
62-
63-
Required: False
64-
Position: Named
65-
Default value: None
66-
Accept pipeline input: False
67-
Accept wildcard characters: False
68-
```
69-
7056
### -Identity
7157
The ID, name or Url (Lists/MyList) of the list
7258

@@ -95,10 +81,20 @@ Accept pipeline input: False
9581
Accept wildcard characters: False
9682
```
9783
84+
### -Connection
85+
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.
9886
87+
```yaml
88+
Type: PnPConnection
89+
Parameter Sets: (All)
9990

100-
## RELATED LINKS
101-
102-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
91+
Required: False
92+
Position: Named
93+
Default value: None
94+
Accept pipeline input: False
95+
Accept wildcard characters: False
96+
```
10397
98+
## RELATED LINKS
10499
100+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Set-PnPContext.md

+17-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Set the ClientContext
1515
## SYNTAX
1616

1717
```powershell
18-
Set-PnPContext [-Context] <ClientContext> [<CommonParameters>]
18+
Set-PnPContext -Context <ClientContext> [-Connection <PnPConnection>] [<CommonParameters>]
1919
```
2020

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

2424
## EXAMPLES
2525

@@ -50,7 +50,20 @@ Accept pipeline input: True (ByValue)
5050
Accept wildcard characters: False
5151
```
5252
53-
## RELATED LINKS
53+
### -Connection
54+
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.
5455
55-
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
56+
```yaml
57+
Type: PnPConnection
58+
Parameter Sets: (All)
59+
60+
Required: False
61+
Position: Named
62+
Default value: None
63+
Accept pipeline input: False
64+
Accept wildcard characters: False
65+
```
66+
67+
## RELATED LINKS
5668
69+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Base/BasePSCmdlet.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Linq.Expressions;
62
using System.Management.Automation;
7-
using System.Reflection;
83
using PnP.PowerShell.Commands.Attributes;
94

105
namespace PnP.PowerShell.Commands.Base

src/Commands/Base/ConnectOnline.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ protected void Connect(ref CancellationToken cancellationToken)
290290
#else
291291
WriteVerbose($"PnP PowerShell Cmdlets ({Assembly.GetExecutingAssembly().GetName().Version})");
292292
#endif
293-
PnPConnection.Current = connection;
294-
if (CreateDrive && PnPConnection.Current.Context != null)
293+
294+
if (CreateDrive && connection.Context != null)
295295
{
296296
var provider = SessionState.Provider.GetAll().FirstOrDefault(p => p.Name.Equals(SPOProvider.PSProviderName, StringComparison.InvariantCultureIgnoreCase));
297297
if (provider != null)
@@ -306,9 +306,9 @@ protected void Connect(ref CancellationToken cancellationToken)
306306
}
307307
}
308308

309-
if (PnPConnection.Current.Url != null)
309+
if (connection.Url != null)
310310
{
311-
var hostUri = new Uri(PnPConnection.Current.Url);
311+
var hostUri = new Uri(connection.Url);
312312
Environment.SetEnvironmentVariable("PNPPSHOST", hostUri.Host);
313313
Environment.SetEnvironmentVariable("PNPPSSITE", hostUri.LocalPath);
314314
}
@@ -340,6 +340,10 @@ protected void Connect(ref CancellationToken cancellationToken)
340340
{
341341
WriteObject(connection);
342342
}
343+
else
344+
{
345+
PnPConnection.Current = connection;
346+
}
343347
}
344348

345349
#region Connect Types

src/Commands/Base/DisconnectOnline.cs

+11-40
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Management.Automation;
55
using System.Reflection;
66
using PnP.PowerShell.Commands.Provider;
7-
using PnP.PowerShell.Commands.Model;
87

98
namespace PnP.PowerShell.Commands.Base
109
{
@@ -18,35 +17,24 @@ public class DisconnectOnline : PSCmdlet
1817
protected override void ProcessRecord()
1918
{
2019
// If no specific connection has been passed in, take the connection from the current context
21-
if (Connection == null)
22-
{
23-
Connection = PnPConnection.Current;
24-
}
25-
if (Connection?.Certificate != null)
20+
var connection = Connection ?? PnPConnection.Current;
21+
22+
if (connection?.Certificate != null)
2623
{
27-
if (Connection != null && Connection.DeleteCertificateFromCacheOnDisconnect)
24+
if (connection != null && connection.DeleteCertificateFromCacheOnDisconnect)
2825
{
29-
PnPConnection.CleanupCryptoMachineKey(Connection.Certificate);
26+
PnPConnection.CleanupCryptoMachineKey(connection.Certificate);
3027
}
31-
Connection.Certificate = null;
32-
}
33-
var success = false;
34-
if (Connection != null)
35-
{
36-
success = DisconnectProvidedService(Connection);
37-
}
38-
else
39-
{
40-
success = DisconnectCurrentService();
28+
connection.Certificate = null;
4129
}
30+
31+
var success = DisconnectProvidedService(ref connection);
32+
4233
if (!success)
4334
{
4435
throw new InvalidOperationException(Properties.Resources.NoConnectionToDisconnect);
4536
}
4637

47-
// clear credentials
48-
PnPConnection.Current = null;
49-
5038
var provider = SessionState.Provider.GetAll().FirstOrDefault(p => p.Name.Equals(SPOProvider.PSProviderName, StringComparison.InvariantCultureIgnoreCase));
5139
if (provider != null)
5240
{
@@ -59,7 +47,7 @@ protected override void ProcessRecord()
5947
}
6048
}
6149

62-
internal static bool DisconnectProvidedService(PnPConnection connection)
50+
internal static bool DisconnectProvidedService(ref PnPConnection connection)
6351
{
6452
Environment.SetEnvironmentVariable("PNPPSHOST", string.Empty);
6553
Environment.SetEnvironmentVariable("PNPPSSITE", string.Empty);
@@ -71,22 +59,5 @@ internal static bool DisconnectProvidedService(PnPConnection connection)
7159
connection = null;
7260
return true;
7361
}
74-
75-
internal static bool DisconnectCurrentService()
76-
{
77-
Environment.SetEnvironmentVariable("PNPPSHOST", string.Empty);
78-
Environment.SetEnvironmentVariable("PNPPSSITE", string.Empty);
79-
80-
if (PnPConnection.Current == null)
81-
{
82-
return false;
83-
}
84-
else
85-
{
86-
PnPConnection.Current.Context = null;
87-
PnPConnection.Current = null;
88-
return true;
89-
}
90-
}
9162
}
92-
}
63+
}

src/Commands/Base/GetConnection.cs

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
using System.Management.Automation;
22

3-
using System;
4-
using PnP.PowerShell.Commands.Properties;
5-
63
namespace PnP.PowerShell.Commands.Base
74
{
85
[Cmdlet(VerbsCommon.Get, "PnPConnection")]
96
[OutputType(typeof(PnPConnection))]
10-
public class GetPnPConnection : PSCmdlet
7+
public class GetPnPConnection : PnPSharePointCmdlet
118
{
12-
13-
protected override void BeginProcessing()
14-
{
15-
base.BeginProcessing();
16-
17-
if (PnPConnection.Current == null)
18-
{
19-
throw new InvalidOperationException(Resources.NoSharePointConnection);
20-
}
21-
if (PnPConnection.Current.Context == null)
22-
{
23-
throw new InvalidOperationException(Resources.NoSharePointConnection);
24-
}
25-
}
26-
279
protected override void ProcessRecord()
2810
{
29-
WriteObject(PnPConnection.Current);
11+
WriteObject(Connection);
3012
}
3113
}
32-
}
14+
}

src/Commands/Base/GetContext.cs

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
using System.Management.Automation;
22

3-
using System;
4-
using PnP.PowerShell.Commands.Properties;
5-
63
namespace PnP.PowerShell.Commands.Base
74
{
85
[Cmdlet(VerbsCommon.Get, "PnPContext")]
96
[OutputType(typeof(Microsoft.SharePoint.Client.ClientContext))]
10-
public class GetSPOContext : PSCmdlet
7+
public class GetSPOContext : PnPSharePointCmdlet
118
{
12-
13-
protected override void BeginProcessing()
14-
{
15-
base.BeginProcessing();
16-
17-
if (PnPConnection.Current == null)
18-
{
19-
throw new InvalidOperationException(Resources.NoSharePointConnection);
20-
}
21-
if (PnPConnection.Current.Context == null)
22-
{
23-
throw new InvalidOperationException(Resources.NoSharePointConnection);
24-
}
25-
}
26-
279
protected override void ProcessRecord()
2810
{
29-
WriteObject(PnPConnection.Current.Context);
11+
WriteObject(Connection.Context);
3012
}
3113
}
32-
}
14+
}

0 commit comments

Comments
 (0)