question

hitendersingh-1558 avatar image
0 Votes"
hitendersingh-1558 asked MichaelHan-MSFT commented

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

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


office-sharepoint-onlinewindows-server-powershell
image.png (44.6 KiB)
image.png (35.6 KiB)
image.png (14.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @hitendersingh-1558,

Is there anything update? If my answer helps you, please click "Accept Answer" and upvote it:)

0 Votes 0 ·

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT commented

Hi @hitendersingh-1558,

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.