Can anyone help me with Azure PowerShell function

Mohammed Sohel 66 Reputation points
2023-07-19T12:37:43.13+00:00

I need to create a Azure PowerShell timer function to transfer files from storage account container to SharePoint.

I have tried all possible ways but luck. Any help highly appreciated.

Thanks,

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,679 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,301 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,329 questions
{count} votes

Accepted answer
  1. Ryan Hill 27,111 Reputation points Microsoft Employee
    2023-07-19T20:57:16.9333333+00:00

    @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()
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful