2

I am trying to get a user to run a powershell script through an Azure function. This script is supposed to create a site on SharePoint with some info provided by users.

I created an Azure Function App to run some PowerShell scripts. When I tried to run my code, it returned an error http 502 at Connect-PnPOnline If I remove Connect-PnPOnline, I get a 200 response. So I am sure that it has to do with Connect-PnPOnline in my script.

I followed the post by @Lee_MSFT Get PowerShell Script to Run SharePoint Commands on Azure and able to import modules.

using namespace System.Net

param (
    [string]$projectnumber,
    [string]$projectname
)

$site = "https://xxxxx.sharepoint.com/sites/projects"

$projectsite = -join($site, "/", $projectnumber)
$projecttitle = -join($projectnumber, " ", $projectname)

Connect-PnPOnline -url $site 
...

I got 500 from Connect-PnPOnline -url $site and a 502 from Connect-PnPOnline -url $site -UseWebLogin

Anyone know that why I have 5xx errors while using Connect-PnPOnline? Thanks a lot!

1
  • I don't think you can use an interactive way to connect in azure function. If your account is MFA-enabled, you may need to use an AD app to do that, like Connect-PnPOnline -AppId '<id>' -AppSecret '<secret>' -AADDomain 'contoso.onmicrosoft.com'. If your account is not MFA-enabled, I think the way provided by Lee_MSFT will work.
    – Joy Wang
    Aug 28, 2019 at 2:33

1 Answer 1

1

Make sure you have upload the PnP PowerShell module for your Azure Function, then use the PowerShell below to connect the SharePoint.

$siteURL = "https://tenant.sharepoint.com/sites/projects"    
$userId = "[email protected]"    
$plainText= "*****"  
$pwd = ConvertTo-SecureString $plainText -AsPlainText -Force    
$creds = New-Object System.Management.Automation.PSCredential($userId,$pwd)  
Connect-PnPOnline -Url $siteURL -Credentials $creds 

Or use this:

Connect-PnPOnline -AppId $env:SPO_AppId -AppSecret $env:SPO_AppSecret -Url "https://tenant.sharepoint.com/sites/projects" 

SPO_AppId - Set the value to the Client ID you copied in the first step when you created your app on your tenant.

SPO_AppSecret - Set the value to the Client Secret that you copied in the first step when you created your app on your tenant.

I suggest you check the steps in the article below.

Azure Functions For SharePoint Operations Using PnP PowerShell

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.