Forum Discussion

My-atix's avatar
My-atix
Copper Contributor
Jan 05, 2023
Solved

Connect-PnPOnline -Connection parameter???

Hi All,

 

I have a question with regards to the below code... I want add information from a newly created site to a "List" in another site collection.

 

After debugging the following code I can see that the -connection parameter is not working as I would expect it too and I am not sure why???

 

 

   $webUrl = "https://yc0wh.sharepoint.com/sites/Test2023"
   $templateSiteUrl = "https://yc0wh.sharepoint.com/sites/SPOSiteProvisioning"
   $siteDirList = "Site Directory"
    
   $webConnect = Connect-PnPOnline -Url $webUrl -Interactive
   $tplSiteConnect = Connect-PnPOnline -Url $templateSiteUrl -Interactive


   try {
    write-host -f Cyan "Logging Site information in Site Directory for" $webUrl
    $SPWeb = Get-PnPWeb -Connection $webConnect
    write-output $SPWeb
    $list = Get-PnPList -identity $siteDirList -Connection $tplSiteConnect -ThrowExceptionIfListNotFound -ErrorAction Stop
    write-output $list
    if ($webUrl -ne $null) {
      $item = @{}
      $item = @{
          "Title" = $SPWeb.Title;
          "URL" = $SPWeb.URL;
          "SiteDescription" = $SPWeb.Description
      }
      Add-PnPListItem -List $list -Values $item -Connection $tplSiteConnect
  }
   }
   catch {
    Write-Host "ERROR CREATING ITEM"
    Get-Error
   }

 

 

I have set to variables to use when I connect to the various sites but for some reason the above code always uses the last site I connected to initially.

 

EG:

If I connect to 2 sites it always connects to the last site despite my using the relevant variables with the -Connection parameter???

 

The Above code is always connected to:

 $tplSiteConnect = Connect-PnPOnline -Url $templateSiteUrl -Interactive

 

Despite me calling the $webConnect variable using -connection $webConnect

What am I doing wrong?

  • My-atix The Connect-PnPOnline cmdlet has a -ReturnConnection parameter which will return the connection object you are looking for.

     

    Change line 5 in your sample above to 

    $webConnect = Connect-PnPOnline -Url $webUrl -Interactive -ReturnConnection

     

    Good luck!

     

     

    Please click Mark as Best Response & Like if my post helped you to answer or resolve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

2 Replies

  • My-atix The Connect-PnPOnline cmdlet has a -ReturnConnection parameter which will return the connection object you are looking for.

     

    Change line 5 in your sample above to 

    $webConnect = Connect-PnPOnline -Url $webUrl -Interactive -ReturnConnection

     

    Good luck!

     

     

    Please click Mark as Best Response & Like if my post helped you to answer or resolve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

  • SvenSieverding's avatar
    SvenSieverding
    Bronze Contributor

    My-atix 

    "Connect-PnPOnline" does not return a connection object.
    Use "Get-PnPConnection" to get the current connection and store it as a variable

    Connect-PnPOnline -Url $webUrl -Interactive
    $webConnect = Get-PnPConnection
    Connect-PnPOnline -Url $templateSiteUrl -Interactive
    $tplSiteConnect = Get-PnPConnection
    
    Get-PnPWeb -Connection $webConnect
    Get-PnPWeb -Connection $tplSiteConnect


    Best Regards,
    Sven

Resources