I was finally able to achieve the upload using script bellow. I had to make some modifications with counting the chunks. Hope someone might find it useful :).
$driveid = "*******************"
$fileName = "20220627_143840.jpg"
$path = "C:\testUpload\20220627_143840.jpg"
$AZcred = New-Object -TypeName PSObject -Property @{
[string]"appID" = "*******************";
[string]"tenantID" = "*******************";
[string]"secret" = (ConvertTo-SecureString "*******************" -AsPlainText -Force)
}
$AuthToken = Get-MsalToken -TenantId $AZcred.tenantID -ClientId $AZcred.appID -ClientSecret $AZcred.secret
$AuthToken = @{'Authorization' = "Bearer $($AuthToken.AccessToken)" }
$createUploadSessionUri = "https://graph.microsoft.com/v1.0/sites/<siteID>/drives/$($driveid)/root:/$($fileName):/createUploadSession"
$uploadSession = (Invoke-WebRequest -Method POST -Uri $createUploadSessionUri -Headers $AuthToken -ContentType 'application/json').content | ConvertFrom-Json
#### Send bytes to the upload session
$fileInBytes = [System.IO.File]::ReadAllBytes($path)
$fileLength = $fileInBytes.Length
## Split the file up
$PartSizeBytes = 320 * 1024 # 327680
$index = 0
$start = 0
$end = 0
while ($fileLength -gt ($end + 1)) {
$start = $index * $PartSizeBytes
if (($start + $PartSizeBytes - 1) -lt $fileLength) {
$end = ($start + $PartSizeBytes - 1)
}
else {
$end = ($start + ($fileLength - ($index * $PartSizeBytes)) - 1)
}
[byte[]]$body = $fileInBytes[$start..$end]
write-host $body.Length.ToString()
$headers = @{
'Content-Length' = $body.Length.ToString()
'Content-Range' = "bytes $start-$end/$fileLength"
}
write-Host "bytes $start-$end/$fileLength | Index: $index and ChunkSize: $PartSizeBytes"
$response = Invoke-WebRequest -Method Put -Uri $uploadSession.uploadUrl -Body $body -Headers $headers
$index++
}