0
votes

I have a Azure PowerShell Runbook where I use SharePoint-PnP Cmdlets, which is working totally fine. But I have a problem with a function call. To mention, the script is working fine locally, but not in the Azure Runbook.

$SPCredentials = Get-AutomationPSCredential -Name 'ServiceAccount'
Connect-PnPOnline -Url $SPUrl -Credentials $SPCredentials

$SPList = New-PnPList -Title "$($ObjektNr)-$($ObjektName)" -Template DocumentLibrary -OnQuickLaunch -EnableContentTypes
Write-Output "Bibliothek erstellt"
Add-PnPContentTypeToList -List $SPList -ContentType $SPCTName -DefaultContentType
Write-Output "Inhaltstyp hinzugefügt"
Remove-PnPContentTypeFromList -List $SPList -ContentType "Dokument"
Write-Output "Inhaltstyp entfernt"
Set-PnPList -Identity $SPList -EnableContentTypes $false -EnableVersioning $true -EnableMinorVersions $false
Write-Output "Listeneinstellungen vorgenommen"

$SPList = Get-PnPList -Identity "$($ObjektNr)-$($ObjektName)"
$SPRootFolder = "$($SPList.RootFolder.Name)/"
Write-Output "Neue Bibliothek abgerufen"

$SPTerms = Get-PnPTerm -TermSet $SPTermSet -TermGroup $SPTermGroup -IncludeChildTerms -Recursive
Write-Output "Terms abgerufen"

Function getTerms($Terms)
{
    Write-Output "Funktion ausgeführt"
    Foreach ($Term in $Terms)
    {
        If ($Term.PathOfTerm.Contains(";"))
        {
            Add-PnPFolder -Name $Term.Name -Folder "$($SPRootFolder)$($Term.PathOfTerm.Substring(0, $Term.PathOfTerm.LastIndexOf(";")).Replace(";","/"))"

            $values = @()
            $splitTerms = $Term.PathOfTerm.Split(";")
            $count = $splitTerms.Count - 1

            For ($i=0; $i -le $count; $i++)
            {
                If ($i -eq 0)
                {
                    $values += "$($SPTermGroup)|$($SPTermSet)|$($splitTerms[$i])"
                }
                Else
                {
                    $values += "$($values[-1])|$($splitTerms[$i])"
                }
            }

            Set-PnPDefaultColumnValues -List $SPList -Field "Metadaten" -Value $values -Folder "$($Term.PathOfTerm.Replace(";","/"))"
        }
        else
        {
            Add-PnPFolder -Name $Term.Name -Folder $SPRootFolder

            Set-PnPDefaultColumnValues -List $SPList -Field "Metadaten" -Value $Term.Id -Folder $Term.Name
        }

        If ($Term.Terms.Count -gt 0)
        {
            getTerms($Term.Terms)
        }
    }
}

getTerms($SPTerms)

The problem now is where I call the function getTerms within the function getTerms (the fourth line from the bottom). It seems after calling that function again, the whole script starts from the beginning, instead of only the function with the parameters.

Thanks for any help!

Regards, Mark

1

1 Answers

0
votes

Ideally it shouldn't start from the beginning! To double check, I would recommend you to add write-output lines just before and after you call getTerms function (i.e., near 4th line from the bottom) and also just before actual getTerms function. And then validate whether the content you provided in those write-output lines are printed in expected order or not.

Also, I would suggest you to try below approach to see if your issue gets resolved.

Function getTerms($Terms)
{
    Write-Output "Funktion ausgeführt"
    Foreach ($Term in $Terms)
    {
        If ($Term.PathOfTerm.Contains(";"))
        {
            Add-PnPFolder -Name $Term.Name -Folder "$($SPRootFolder)$($Term.PathOfTerm.Substring(0, $Term.PathOfTerm.LastIndexOf(";")).Replace(";","/"))"
            $values = @()
            $splitTerms = $Term.PathOfTerm.Split(";")
            $count = $splitTerms.Count - 1
            For ($i=0; $i -le $count; $i++)
            {
                If ($i -eq 0)
                {
                    $values += "$($SPTermGroup)|$($SPTermSet)|$($splitTerms[$i])"
                }
                Else
                {
                    $values += "$($values[-1])|$($splitTerms[$i])"
                }
            }
            Set-PnPDefaultColumnValues -List $SPList -Field "Metadaten" -Value $values -Folder "$($Term.PathOfTerm.Replace(";","/"))"
        }
        else
        {
            Add-PnPFolder -Name $Term.Name -Folder $SPRootFolder
            Set-PnPDefaultColumnValues -List $SPList -Field "Metadaten" -Value $Term.Id -Folder $Term.Name
        }
        If ($Term.Terms.Count -gt 0)
        {
            getTerms($Term.Terms)
        }
    }
}

Function main()
{
    $SPCredentials = Get-AutomationPSCredential -Name 'ServiceAccount'
    Connect-PnPOnline -Url $SPUrl -Credentials $SPCredentials

    $SPList = New-PnPList -Title "$($ObjektNr)-$($ObjektName)" -Template DocumentLibrary -OnQuickLaunch -EnableContentTypes
    Write-Output "Bibliothek erstellt"
    Add-PnPContentTypeToList -List $SPList -ContentType $SPCTName -DefaultContentType
    Write-Output "Inhaltstyp hinzugefügt"
    Remove-PnPContentTypeFromList -List $SPList -ContentType "Dokument"
    Write-Output "Inhaltstyp entfernt"
    Set-PnPList -Identity $SPList -EnableContentTypes $false -EnableVersioning $true -EnableMinorVersions $false
    Write-Output "Listeneinstellungen vorgenommen"

    $SPList = Get-PnPList -Identity "$($ObjektNr)-$($ObjektName)"
    $SPRootFolder = "$($SPList.RootFolder.Name)/"
    Write-Output "Neue Bibliothek abgerufen"

    $SPTerms = Get-PnPTerm -TermSet $SPTermSet -TermGroup $SPTermGroup -IncludeChildTerms -Recursive
    Write-Output "Terms abgerufen"

    getTerms($SPTerms)
}

main()