Maintaining Multiple Connections to PnPONline

%3CLINGO-SUB%20id%3D%22lingo-sub-186407%22%20slang%3D%22en-US%22%3EMaintaining%20Multiple%20Connections%20to%20PnPONline%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-186407%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20trying%20to%20create%20a%20script%20that%20will%20copy%20users%20from%20SPO%20Groups%20to%20Office%2FUnified%20groups.%20I%20think%20that%20I%20should%20be%20able%20to%20do%20this%20by%20using%26nbsp%3B%20Connect-pnpOnline%20-graph%26nbsp%3B%20and%20Connect-PnPOnline%20-Url%2C%20but%20I'm%20not%20sure%20how%20to%20maintain%202%20separate%20connections%20in%20the%20same%20script.%20Can%20someone%20point%20in%20the%20right%20direction%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-186407%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-187803%22%20slang%3D%22en-US%22%3ERe%3A%20Maintaining%20Multiple%20Connections%20to%20PnPONline%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-187803%22%20slang%3D%22en-US%22%3E%3CP%3EI%20think%20you%20need%20to%20use%20the%26nbsp%3BReturnConnection%20parameter%20of%20the%26nbsp%3BConnect-PnPOnline%20command.%3CBR%20%2F%3E%3CBR%20%2F%3ESample%20code%20would%20be%20something%20like%20%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%24connection1%20%3D%20Connect-PnPOnline%20-Url%20%22someurl%22%20-ReturnConnection%3C%2FP%3E%3CP%3E%24connection2%20%3D%20Connect-PnPOnline%20-Graph%20-ReturnConnection%3C%2FP%3E%3CP%3EGet-PnPUser%20-Identity%20i%3A0%23.f%7Cmembership%7Cuser%40tenant.onmicrosoft.com%20-Connection%20%24connection1%3C%2FP%3E%3CP%3ENew-PnPUser%20-LoginName%20%22%3CLOGINNAME%3E%22%20-Connection%20%24connection2%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20title%3D%22Connect-PnPOnline%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fconnect-pnponline%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EConnect-PnPOnline%3C%2FA%3E%3C%2FLOGINNAME%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Respected Contributor

I'm trying to create a script that will copy users from SPO Groups to Office/Unified groups. I think that I should be able to do this by using  Connect-pnpOnline -graph  and Connect-PnPOnline -Url, but I'm not sure how to maintain 2 separate connections in the same script. Can someone point in the right direction?

 

@Erwin van Hunen

1 Reply

I think you need to use the ReturnConnection parameter of the Connect-PnPOnline command.

Sample code would be something like :

$connection1 = Connect-PnPOnline -Url "someurl" -ReturnConnection

$connection2 = Connect-PnPOnline -Graph -ReturnConnection

Get-PnPUser -Identity i:0#.f|membership|user@tenant.onmicrosoft.com -Connection $connection1

New-PnPUser -LoginName "<loginName>" -Connection $connection2

Connect-PnPOnline