azurepowershellsharepointazure-functions

PowerShell Azure function to connect to sharepoint site


I have a power-shell azure function which have its managed identity enabled and i applied the needed perversion to the azure function to access SharePoint sites. now i have a site named "HR" and a list named "emails", so how i can get all the items inside the emails list? and then delete them?

I tried the following:-

1- Inside the requirements file, i added those references:-

@{

'Az'  =  '13.*'

'ExchangeOnlineManagement'  =  '3.*'

'Microsoft.Graph'  =  '2.*'

'PnP.PowerShell' = '2.12.0'

}

2- Then i added this code to connect to SharePoint online:-

$siteUrl = "https://**i.sharepoint.com/sites/HR"

Connect-PnPOnline -Url @siteUrl -ManagedIdentity

# Get all items from the 'emails' list

$items = Get-PnPListItem -List "emails"

# Loop through items and delete each one

foreach ($item in $items) {

   Remove-PnPListItem -List "emails" -Identity $item.Id -Force

}

But I got this error :-

2025-05-24T16:25:05Z [Warning] The Function app may be missing a module containing the 'Connect-PnPOnline' command definition. If this command belongs to a module available on the PowerShell Gallery, add a reference to this module to requirements.psd1. Make sure this module is compatible with PowerShell 7. For more details, see https://aka.ms/functions-powershell-managed-dependency. If the module is installed but you are still getting this error, try to import the module explicitly by invoking Import-Module just before the command that produces the error: this will not fix the issue but will expose the root cause. 2025-05-24T16:25:05Z [Error] ERROR: The term 'Connect-PnPOnline' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Message : The term 'Connect-PnPOnline' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Any advice?

Thanks

EDIT

I added this to the requirements files:-

@{
'Az'  =  '13.*'
"Az.Accounts" = "2.*"
'ExchangeOnlineManagement'  =  '3.*'
'Microsoft.Graph'  =  '2.*'
'PnP.PowerShell' = '2.0.45-nightly'
}

And the following code:-

using namespace System.Net
# Input bindings are passed in via param block.
param($Timer)

Connect-PnPOnline -Url @siteUrl -ManagedIdentity

# Get all items from the 'emails' list

$items = Get-PnPListItem -List "****"

# Loop through items and delete each one

foreach ($item in $items) {

    Remove-PnPListItem -List "emails" -Identity $item.Id -Force

}

But i got this error:-

2025-05-26T13:37:27Z   [Warning]   The Function app may be missing a module containing the 'Connect-PnPOnline' command definition. If this command belongs to a module available on the PowerShell Gallery, add a reference to this module to requirements.psd1. Make sure this module is compatible with PowerShell 7. For more details, see https://aka.ms/functions-powershell-managed-dependency. If the module is installed but you are still getting this error, try to import the module explicitly by invoking Import-Module just before the command that produces the error: this will not fix the issue but will expose the root cause.
2025-05-26T13:37:27Z   [Error]   ERROR: The term 'Connect-PnPOnline' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

here are the permissions:-

enter image description here


Solution

  • Below approach worked for me to use Connect-PnPOnline:

    run.ps1:

    using namespace System.Net
    param($Request, $TriggerMetadata)
    
    Connect-PnPOnline -Url "https://m365x542.sharepoint.com/sites/DemoSite" -ManagedIdentity
    $ri_itms = Get-PnPListItem -List "list01"
    $cho_res = @()
    $ri_itms | ForEach-Object {
        Write-Host "Hello Rithwik, Item: $($_.FieldValues.Title)"
        $cho_res += $_.FieldValues.Title
    }
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK
        Body = $cho_res
    
    })
    
    

    The profile.ps1 should be empty:

    enter image description here

    requirements.psd1:

    @{
    'PnP.PowerShell' = '2.0.45-nightly'
     }
    

    Roles:

    enter image description here

    Output:

    enter image description here