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
As you can see it indeed created a file in document library but job eventually suspended.