Templates/Scripts/Send-ArtifactToSharepoint.ps1

[CmdletBinding()]
param (
    # Artifacts to send to Sharepoint
    [Parameter(Mandatory=$true)]
    [string]
    $ArtifactPath,
    # Sharepoint Document Library
    [Parameter(Mandatory=$true)]
    [string]
    $documentLibraryName,
    # Sharepoint Site Url
    [Parameter(Mandatory=$true)]
    [string]
    $Url,
    # Sharepoint Credentials
    [Parameter(Mandatory=$true)]
    [PSCredential]
    $Credentials
)

Write-Host "Provided Site :$Url" -ForegroundColor Green
Write-Host "Provided Path :"$ArtifactPath -ForegroundColor Green
Write-Host "Provided Document Library name :"$documentLibraryName -ForegroundColor Green

    try{

        Connect-PnPOnline -Url $Url -CreateDrive -Credentials $Credentials

        $file = Get-ChildItem -Path $ArtifactPath -Recurse
        $i = 0;
        Write-Host "Uploading documents to Site.." -ForegroundColor Cyan
        (dir $ArtifactPath -Recurse) | %{
            try{
                $i++
                if($_.GetType().Name -eq "FileInfo"){
                    $SPFolderName =  $documentLibraryName + $_.DirectoryName.Substring($ArtifactPath.Length);
                    $status = "Uploading Files :'" + $_.Name + "' to Location :" + $SPFolderName
                    $te = Add-PnPFile -Path $_.FullName -Folder $SPFolderName
                    }          
                }
            catch{
            }
            }
    }
    catch{
        Write-Host $_.Exception.Message -ForegroundColor Red
    }

Disconnect-PnPOnline