You can use AzurePowerShell@5 Task to achieve this, More details here on this URL
https://dev.to/kkazala/azure-devops-pipeline-with-workload-identity-federation-2gpj
Following is sample code
- task: AzurePowerShell@5
name: Upload Folder To SP
inputs:
azureSubscription: {SerivicePrincipal Name Created in ADO}
azurePowerShellVersion: latestVersion
ScriptType: InlineScript
Inline: |
try {
$azAccessToken = Get-AzAccessToken -ResourceUrl "https://{tenantID}.sharepoint.com"
$siteUrl = "https://{tenantID}.sharepoint.com/sites/{sitename}"
$conn = Connect-PnPOnline -Url $siteUrl -AccessToken $azAccessToken.Token -ReturnConnection
Write-Host $conn.Url
}
catch {
Write-Host "##[error] 1 (Connect-PnPOnline -AccessToken): $($_.Exception.Message)"
}
try {
$lists = Get-PnPList -Connection $conn
Write-Host "List names:"
$lists | ForEach-Object { Write-Host $_.Title }
$sharePointFolderUrl = "$siteUrl/Dokumente"
Add-PnPFolder -Name $folderName -Folder $sharePointFolderUrl -Connection $conn
}
catch {
Write-Host "##[error] 2 (Add-PnPFolder): $($_.Exception.Message)"
}
Note: You have to use following command to provide permission for specific site
Grant-PnPAzureADAppSitePermission -AppId 'xxx' -Site 'https://xx.sharepoint.com/sites/{sitename}' -Permissions Write