@Mohammed Sohel you can use VS Code to create a PowerShell function project. Stepping through, select PowerShell as the runtime and Timer as the binding trigger. You should a have several project files with a function.json
that has the following
{
"bindings": [
{
"name": "Timer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 */5 * * * *"
}
]
}
In the run.ps1
file:
# Input bindings are passed in via param block.
param($Timer)
# Get the current universal time in the default string format
$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' porperty is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Set the variables
$SiteURL = "<your-site-url>"
$LibraryName = "<your-library-name>"
$StorageAccountName = "<your-storage-account-name>"
$StorageAccountKey = "<your-storage-account-key>"
$ContainerName = "<your-container-name>"
# Set the Sharepoint context
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("<your-username>", (ConvertTo-SecureString "<your-password>" -AsPlainText -Force))
# Get the library
$library = $context.Web.Lists.GetByTitle($LibraryName)
$context.Load($library)
$context.ExecuteQuery()
# Get the storage container
$storageAccount = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$container = Get-AzureStorageContainer -Name $ContainerName -Context $storageAccount
$Blobs = Get-AzureStorageBlob -Container $container.Name -Context $storageAccount
# Upload the files to the library
ForEach ($blob in $Blobs) {
$file = $library.RootFolder.Files.Add($blob.Name, $blob.Uri.AbsoluteUri, $true)
$context.Load($file)
$context.ExecuteQuery()
}
GitHub copilot was used to generate the code above and hasn't been tested but should provide some direction.
EDIT 2023 July 24 Updated above snippet to use SharePoint PnP modules instead
# Input bindings are passed in via param block.
param($Timer)
# Get the current universal time in the default string format
$currentUTCtime = (Get-Date).ToUniversalTime()
# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
Write-Host "PowerShell timer is running late!"
}
# Set the variables
$SiteURL = "<your-site-url>"
$LibraryName = "<your-library-name>"
$StorageAccountName = "<your-storage-account-name>"
$StorageAccountKey = "<your-storage-account-key>"
$ContainerName = "<your-container-name>"
$UserName = "<your-username>"
$Password = "<your-password>" | ConvertTo-SecureString -AsPlainText -Force
# Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Credentials (New-Object System.Management.Automation.PSCredential ($UserName, $Password))
# Get the library
$library = Get-PnPSite | Get-PnPList | Where-Object { $_.Title -eq $LibraryName }
# Get the storage container
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$container = Get-AzureStorageContainer -Name $ContainerName -Context $ctx
$Blobs = Get-AzureStorageBlob -Container $container.Name -Context $ctx
# Upload the files to the library
ForEach ($blob in $Blobs) {
$file = Add-PnPFile -Folder $LibraryName -FileName $blob.Name -Stream $blob.ICloudBlob.OpenRead()
}