Forum Discussion

My-atix's avatar
My-atix
Copper Contributor
Jan 04, 2023

PnP PowerShell Azure Function App issues

Hi Everyone,

 

I am having issues getting a PnP.PowerShell provisioning solution to work successfully. I can understand that there have been a number of challenges with PnP.PowerShell running on Function Apps since the beginning of December when PowerShell Core 7.0 was deprecated.
I have managed to role back my Function App to use PowerShell Core 7.0 but I am still having issues with the following code.

 

 

 

using namespace System.Net

param($Request)

$env:PNPPOWERSHELL_UPDATECHECK="false"
$tenant = $env:Tenant
$clientId = $env:ClientId
$certificateBase64Encoded = $env:Certificate
$templateLibUrl = $env:TemplateLibUrl
$templateFileName = $env:TemplateFileName
#$teamSiteLogo = "logo.png"
$siteDirList = "Site Directory"
$downloadFilePath = $PSScriptRoot
$webUrl = $Request.Body.webUrl

write-output "Starting configuration of Modern Teams Site:"
write-output $webUrl
#Applies PnP Template to newly created site
Try {

    #Set Error Preference variable to Stop
    $ErrorActionPreference = "Stop"

if ($webUrl -ne $null) {
    $templateSiteUrl = ($templateLibUrl -Split '/')[0..4] -Join '/'
    $tplSiteConnect = Connect-PnPOnline -Url $templateSiteUrl -Tenant $tenant -ClientId $clientId -CertificateBase64Encoded $certificateBase64Encoded
    #$tplSiteConnect = Connect-PnPOnline -ManagedIdentity

    $templateRelUrl = ($templateLibUrl -Split '/' | Select-Object -Skip 3) -Join '/'
    $templateFileUrl = "/" + $templateRelUrl + "/" + $templateFileName
    #Get-PnPFile -Url $templateFileUrl -Path $downloadFilePath -FileName $templateFileName -AsFile -Force -Connection $tplSiteConnect

    $webConnect = Connect-PnPOnline -Url $webUrl -Tenant $tenant -ClientId $clientId -CertificateBase64Encoded $certificateBase64Encoded
    #$webConnect = Connect-PnPOnline -ManagedIdentity
    $templateFilePath = $downloadFilePath + "\" + $templateFileName
    #Invoke-PnPSiteTemplate -Path $templateFilePath -Parameters @{"SiteUrl"=$webUrl}


    # Log Site creation in Site Directory list
    write-host -f Cyan "Logging Site information in Site Directory for" $webUrl
    $Web = Get-PnPWeb -Connection $webConnect
    write-host "Web Details:" $web.Title
    $list = Get-PnPList -identity $siteDirList -Connection $tplSiteConnect -ThrowExceptionIfListNotFound -ErrorAction Stop
    write-host "List details:" $list
if ($webUrl -ne $null) {
    $item = @{}
    $item = @{
        "Title" = $Web.Title;
        "URL" = $Web.URL;
        "SiteDescription" = $Web.Description;
    }
    $addItem = Add-PnPListItem -List $list -Values $item -Connection $tplSiteConnect
}

 #    Set-PnPWebHeader -SiteLogoUrl $templateFilePath + "\" + $teamSiteLogo

}
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
Finally {
    #Rest Error Preference to Default
    $ErrorActionPreference = "Continue"
}


  #Temify the Group connected site
Try {
  
    $Site = Get-PnPTenantSite -Identity $webUrl
    New-PnPTeamsTeam -GroupId $Site.GroupId.Guid -Connection $webConnect
    
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

# Diables Template Gallery dialog and Next Steps dialog for newly created sites in SharePoint
 $disableTemp = Get-PnPWeb -Includes NextStepsFirstRunEnabled, WebTemplatesGalleryFirstRunEnabled -Connection $webConnect
 $disableTemp.NextStepsFirstRunEnabled = $false
 $disableTemp.WebTemplatesGalleryFirstRunEnabled = $false
 $disableTemp.Update()
 Invoke-PnPQuery
$webConnect = $null
$tplSiteConnect = $null

 

 

 

Or more specifically with the line 43 Get-PnPList method. If I run this code snippet locally I have no issue with the Get-PnPList but as soon as I add it to my Function App the list is not returned?

 

    # Log Site creation in Site Directory list
    write-host -f Cyan "Logging Site information in Site Directory for" $webUrl
    $Web = Get-PnPWeb -Connection $webConnect
    write-host "Web Details:" $web.Title
    $list = Get-PnPList -identity $siteDirList -Connection $tplSiteConnect -ThrowExceptionIfListNotFound -ErrorAction Stop
    write-host "List details:" $list
if ($webUrl -ne $null) {
    $item = @{}
    $item = @{
        "Title" = $Web.Title;
        "URL" = $Web.URL;
        "SiteDescription" = $Web.Description;
    }
    $addItem = Add-PnPListItem -List $list -Values $item -Connection $tplSiteConnect
}

 

I have checked the App Registration permissions and this is all configured correctly so I am a little unsure why the List isn't returned.

 

I hope someone can help?

Resources