Add-pnpfile | Azure Automation Runbook job goes into suspended mode when adding file to Sharepoint

hitender singh 126 Reputation points
2021-06-11T05:17:35.763+00:00

Hello Team,

I am using below code to create a file in Spo document library via Azure Runbook, but the job goes in suspended state after trying 3 times however I can see it eventually creates the file with data. I am unable to understand why job goes into suspended state. Is there a different way to streamdata and create a file?

# Get Azure Run As Connection Name  
$connectionName = "AzureRunAsConnection"  
# Get the Service Principal connection details for the Connection name  
$servicePrincipalConnection = Get-AutomationConnection –Name $connectionName           
  
# Logging in to Azure AD with Service Principal  
"Logging in to Azure AD…"  
Connect-azuread –TenantId $servicePrincipalConnection.TenantId `  
    –ApplicationId $servicePrincipalConnection.ApplicationId `  
    –CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint  
  
  
"Logging in to SPO"  
$connection = Get-AutomationConnection –Name "AzureRunAsConnection"  
$tenant = 'domain.onmicrosoft.com'  
$url = 'https://domainsharepoint.com/sites/test1'  
  
Connect-PnPOnline -Url $url –Thumbprint $connection.CertificateThumbprint –ClientId $connection.ApplicationID -Tenant $tenant  
  
$modifiedusers = get-azureaduser | out-string  
  
$FolderName = 'Shared Documents'  
$FileName = 'test09090.txt'  
$fileContent = 'test string'  
  
$Stream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($fileContent))  
  
# $Size = 8192;  
# [System.IO.MemoryMappedFiles.MemoryMappedFile]$MMF = [System.IO.MemoryMappedFiles.MemoryMappedFile]::CreateNew($FileName, $Size);  
# If ($Null -eq $MMF) {  
#     Return;  
# }  
# $Stream = $MMF.CreateViewStream();  
# $StreamWriter = [System.IO.StreamWriter]::new($Stream);  
# $StreamWriter.Write($FileContent);  
  
Add-PnPFile -FileName $FileName -Folder $FolderName -Stream $Stream  
  
# $StreamWriter.Dispose();  
# $Stream.Dispose();  
# $MMF.Dispose();  
  
Write-Host "----"  

OR

$tclp = '  
{    "CompanyName":  "The-cloudpro",  
    "SkuPartNumber":  "DEVELOPERPACK_E5",  
    "Maxallowed":  10,  
    "Consumed": 0  
}  
'  
$stream = [IO.MemoryStream]::new([Text.Encoding]::UTF8.GetBytes($tclp))  
Add-pnpfile -Folder "Shared Documents" -FileName "thecloudpro.json" -Stream $stream  

104576-image.png

As you can see it indeed created a file in document library but job eventually suspended.

104593-image.png

104594-image.png

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,682 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-06-11T09:34:12.01+00:00

    Hi @hitender singh ,

    Got the same result as yours on my end. Per my research, we need to store the result of PNP Actions in a variable, like the below:

    $file=Add-PnPFile -FileName $FileName -Folder $FolderName -Stream $Stream  
    

    The similar issue was discussed here: https://github.com/pnp/PnP-PowerShell/issues/1541#issuecomment-402043124


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments