Forum Discussion

My-atix's avatar
My-atix
Copper Contributor
Dec 16, 2022

Help with PowerShell in AzureFunction

Hi All,

 

I have the following code which I am using in an Azure Provisioning solution but for some reason I just can't get 2 things to work as I would expect them too...

 

First when I try to download a file from a SharePoint Document Library it fails with an error

 

Errors:

ERROR: File Not Found.

 

 

 Get-PnPFile -Url $templateFileUrl -Path $downloadFilePath -FileName $templateFileName -AsFile -Force -Connection $tplSiteConnect 

 

 

I have tried writing out the variables and checking them and they are correct

 

[Information] INFORMATION: https://xxxx.sharepoint.com/sites/SiteProvisioning
[Information] INFORMATION: TemplateFileURL /sites/SiteProvisioning/Shared%20Documents/site-template.xml
[Information] INFORMATION: downloadFilePath C:\home\site\wwwroot\SiteProvisioning
[Information] INFORMATION: TemplateFileName site-template.xml

 

Secondly when I try to write a value back to a SharePoint list I get the following error:

 

Error: Cannot validate argument on parameter 'List'. The argument is null. Provide a valid value for the argument, and then try running the command again.

 

 

 

$siteDirList = "Lists/SiteDirectory"
    # Log Site creation in Site Directory list
    write-host -f Cyan "Logging Site information in Site Directory for" $webUrl
    $Web = Get-PnPWeb -Connection $webConnect
    $list = Get-PnPList -identity $siteDirList -Connection $tplSiteConnect
    write-host $list
    write-host $web.Title
    write-host $web.URL
    write-host $web.Description
    $item = @{}
    $item = @{
        "Title" = $Web.Title;
        "URL" = $Web.URL;
        "SiteDescription" = $Web.Description;
    }
    write-host $list
    $Log = Add-PnPListItem -List $list -Values $item -Connection $tplSiteConnect

 

 

Here is the full code:

 

What am I doing wrong????

 

 

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 = "Lists/SiteDirectory"
$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 '/'
    write-host $templateSiteUrl
    $tplSiteConnect = Connect-PnPOnline -Url $templateSiteUrl -Tenant $tenant -ClientId $clientId -CertificateBase64Encoded $certificateBase64Encoded

    $webConnect = Connect-PnPOnline -Url $webUrl -Tenant $tenant -ClientId $clientId -CertificateBase64Encoded $certificateBase64Encoded

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

  #Temify the Group connected site
  #Get the site collection with ID property
  $Site = Get-PnPTenantSite -Identity $webUrl -Connection $webConnect
  $AddTeam = New-PnPTeamsTeam -GroupId $Site.GroupId.Guid -Connection $webConnect

# Apply PnP Template
    $templateRelUrl = ($templateLibUrl -Split '/' | Select-Object -Skip 3) -Join '/'
    $templateFileUrl = "/" + $templateRelUrl + "/" + $templateFileName

    write-host "TemplateFileURL" $templateFileUrl
    write-host "downloadFilePath" $downloadFilePath
    write-host "TemplateFileName" $templateFileName
    Get-PnPFile -Url $templateFileUrl -Path $downloadFilePath -FileName $templateFileName -AsFile -Force -Connection $tplSiteConnect
    
    $templateFilePath = $downloadFilePath + "\" + $templateFileName
    Invoke-PnPSiteTemplate -Path $templateFilePath -Parameters @{"SiteUrl"=$webUrl} -Connection $webConnect

    # Log Site creation in Site Directory list
    write-host -f Cyan "Logging Site information in Site Directory for" $webUrl
    $Web = Get-PnPWeb -Connection $webConnect
    $list = Get-PnPList -identity $siteDirList -Connection $tplSiteConnect
    write-host $list
    write-host $web.Title
    write-host $web.URL
    write-host $web.Description
    $item = @{}
    $item = @{
        "Title" = $Web.Title;
        "URL" = $Web.URL;
        "SiteDescription" = $Web.Description;
    }
    write-host $list
    $Log = Add-PnPListItem -List $list -Values $item -Connection $tplSiteConnect
}
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
Finally {
    #Rest Error Preference to Default
    $ErrorActionPreference = "Continue"
    
    #Disconnect
    $webConnect = $null
    $tplSiteConnect = $null
}

 

No RepliesBe the first to reply

Resources