Uploading folders in sharepoint from powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-1971767%22%20slang%3D%22en-US%22%3EUploading%20folders%20in%20sharepoint%20from%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1971767%22%20slang%3D%22en-US%22%3E%3CP%3EMy%20below%20code%20successfully%20read%20a%20folder%20and%20uploades%20the%20subfolder%20one%20by%20one%20as%20a%20zip%20file%20in%20SharePoint.%20But%20if%20any%20of%20my%20file%20is%20of%20Zip%20extension%2C%20it%20gives%20me%20an%20error.%20I%20would%20like%20to%20know%20how%20can%20I%20fix%20my%20existing%20code%20below%20to%20handle%20the%20zip%20files%20as%20well%20so%20that%20it%20loads%20both%20the%20zip%20folder%20and%20non-zip%20files%20and%20folders%20in%20the%20SharePoint.%20I%20tried%20multiple%20tings%20like%20removing%20.zip%20extension%20in%20the%20Create-Archive%20and%20file%20but%20it%20does%20not%20work.%20error%20received%3A%3C%2FP%3E%3CP%3E2020-12-09T15%3A49%3A05.7979920Z%20Invoke-RestMethod%20%3A%20The%20remote%20server%20returned%20an%20error%3A%20(400)%20Bad%20Request.%202020-12-09T15%3A49%3A05.7981233Z%20At%20D%3A%5Ca%5Cr1%5Ca_DevOpsScripts%5CReleaseNoteScripts%5CUploadInSharePoint.ps1%3A55%20char%3A17%202020-12-09T15%3A49%3A05.7982104Z%20%2B%20...%20%24response%20%3D%20Invoke-RestMethod%20%24uploadURLObject.uploadUrl%20-Method%20'PUT%20...%202020-12-09T15%3A49%3A05.7982819Z%20%2B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%202020-12-09T15%3A49%3A05.7983312Z%20%2B%20CategoryInfo%20%3A%20InvalidOperation%3A%20(System.Net.HttpWebRequest%3AHttpWebRequest)%20%5BInvoke-RestMethod%5D%2C%20WebExc%202020-12-09T15%3A49%3A05.7983979Z%20eption%202020-12-09T15%3A49%3A05.7984369Z%20%2B%20FullyQualifiedErrorId%20%3A%20WebCmdletWebResponseException%2CMicrosoft.PowerShell.Commands.InvokeRestMethodCommand%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eparam(%24ClientID%2C%24ClientSecret%2C%24WId%2C%24RName%2C%24RReqFor%2C%24SPid%2C%24LibId)%0A%24tempDirectory%20%3D%20%22temp%22%0A%23%20arguments%20defined%20from%20predefined%20variables%20in%20argument%20section%20of%20powershell%20stage%20of%20pipeline%0A%24workingDirectory%20%3D%20%22%24WId%22%0A%24CLIENT_ID%20%3D%20%22%24ClientID%22%0A%24CLIENT_SECRET%20%3D%20%22%24ClientSecret%22%0A%24SharePointSiteId%20%3D%20%22%24SPid%22%0A%24LibraryId%20%3D%20%22%24LibId%22%0A%24azx%20%3D%20%22%24RName%22%20%2B%22_%22%2B%22%24RReqFor%22%0A%24adx%20%3D%20(%24azx.Split(%22%5B%22)%5B0%5D)%0A%24artifactname%20%3D%20%24adx.Substring(0%2C%24adx.Length-1)%0A%0A%0A%23%20Get%20access%20token%0AFunction%20GET-TOKEN%20%0A%7B%0A%20%20%20%20param(%24clientId%2C%20%24clientSecret)%0A%20%20%20%20%24tokenHeaders%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%0A%20%20%20%20%24tokenHeaders.Add(%22Content-Type%22%2C%20%22application%2Fx-www-form-urlencoded%22)%0A%20%20%20%20%24tokenBody%20%3D%20%22client_id%3D%22%2B%20%24clientId%20%2B%20%22%26amp%3Bscope%3Dhttps%253A%2F%2Fgraph.microsoft.com%2F.default%26amp%3Bclient_secret%3D%22%20%2B%20%24clientSecret%20%2B%20%22%26amp%3Bgrant_type%3Dclient_credentials%22%0A%20%20%20%20%24tokenBody%0A%20%20%20%20%24tokenResponse%20%3D%20Invoke-RestMethod%20'https%3A%2F%2Flogin.microsoftonline.com%2Ftest.onmicrosoft.com%2Foauth2%2Fv2.0%2Ftoken'%20-Method%20'POST'%20-Headers%20%24tokenHeaders%20-Body%20%24tokenBody%0A%20%20%20%20return%20%24tokenResponse%20%20%20%20%0A%7D%0A%0A%0A%23%20upload%20the%20file%0AFunction%20UPLOAD-FILE%0A%7B%0A%20%20%20%20param(%24workingDir%2C%20%24tempDir%2C%20%24clientId%2C%20%24clientSecret%2C%20%24artifactname)%0A%20%20%0A%20%20%20%20%0A%20%20%20%20Get-ChildItem%20%24workingDir%20%7C%20ForEach-Object%20%7B%0A%20%20%20%20%0A%20%20%20%20%24name%20%3D%20%24_.FullName.Split(%22%5C%22)%5B-1%5D%0A%20%20%20%20CREATE-ARCHIVE%20-workingDir%20%24_.FullName%20-tempDir%20%24tempDir%20-name%20%24name%0A%20%20%20%20%24file%20%3D%20%24_.FullName%20%2B%20%22%5C%22%20%2B%24tempDir%20%2B%20%22%5C%22%20%2B%20%22%24(%24name).zip%22%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%24fileSize%20%3D%20(Get-Item%20%24file).length%20%20%20%20%20%20%20%0A%20%20%20%20%24uploadURLObject%20%3D%20GET-UPLOADLINK%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%20-artifactname%20%24name%0A%20%20%20%20%24tokenObject%20%3D%20GET-TOKEN%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%0A%20%20%20%20%24uploadHeaders%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Type%22%2C%20%22application%2Fjson%22)%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Range%22%2C%20%22bytes%20%22%20%2B%200%20%2B%22-%22%20%2B%20(%24fileSize-1)%20%2B%20%22%2F%22%20%2B%20%24fileSize)%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Length%22%2C%20%24fileSize)%0A%20%20%20%20%24uploadHeaders.Add(%22Authorization%22%2C%20%22Bearer%20%22%2B%20%24tokenObject.access_token)%20%20%20%20%0A%20%20%20%20%24uploadBody%20%3D%20%5BSystem.IO.File%5D%3A%3AReadAllBytes(%24file)%0A%20%20%20%20%24response%20%3D%20Invoke-RestMethod%20%24uploadURLObject.uploadUrl%20-Method%20'PUT'%20-Headers%20%24uploadHeaders%20-Body%20%24uploadBody%0A%20%20%20%20%24response%20%7C%20ConvertTo-Json%20%20%0A%20%20%20%20REMOVE-TEMPDIR%20-workingDir%20%24_.FullName%20-tempDir%20%24tempDirectory%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%0A%7D%0AFunction%20GET-UPLOADLINK%0A%7B%0A%20%20%20%20param(%24clientId%2C%20%24clientSecret%2C%20%24artifactname)%0A%20%20%20%20%24tokenObject%20%3D%20GET-TOKEN%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%0A%20%20%20%20%24uploadLinkRequestHeaders%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%0A%20%20%20%20%24uploadLinkRequestHeaders.Add(%22Content-Type%22%2C%20%22application%2Fjson%22)%0A%20%20%20%20%24uploadLinkRequestHeaders.Add(%22Authorization%22%2C%20%22Bearer%20%22%2B%20%24tokenObject.access_token)%0A%20%20%20%20%24uploadLinkRequestBody%20%3D%20%22%22%20%20%20%20%20%0A%20%20%20%20%23update%20the%20actual%20link%20%20%0A%20%20%20%20%24fullname%20%3D%20%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2F%24(%24SPid)%2Fdrives%2F%24(%24LibId)%2Froot%2Fchildren%2F%22%20%20%20%20%0A%20%20%20%20%24comname%20%3D%20%24fullname%20%2B%20%24artifactname%20%20%20%20%0A%20%20%20%20%24uploadLinkResponse%20%3D%20Invoke-RestMethod%20%22%24(%24comname).zip%2FcreateUploadSession%22%20-Method%20'POST'%20-Headers%20%24uploadLinkRequestHeaders%20-Body%20%24uploadLinkRequestBody%0A%20%20%20%20%24uploadLinkResponse%20%7C%20ConvertTo-Json%0A%20%20%20%20return%20%24uploadLinkResponse%20%20%20%20%0A%7D%0A%0AFunction%20CREATE-ARCHIVE%0A%7B%0A%20%20%20%20param(%24workingDir%2C%20%24tempDir%2C%20%24name%20)%0A%20%20%20%20write-host%20%24workingDir%0A%20%20%20%20cd%20%24workingDir%0A%20%20%20%20md%20%24tempDir%0A%20%20%20%20%23Compress-Archive%20-Path%20%24workingDir%20-DestinationPath%20%24workingDir%5C%24tempDir%5C%24artifactname%0A%20%20%20%20Compress-Archive%20-Path%20%24workingDir%20-DestinationPath%20%24workingDir%5C%24tempDir%5C%24name%0A%7D%0A%0AFunction%20REMOVE-TEMPDIR%0A%7B%0A%20%20%20%20param(%24workingDir%2C%20%24tempDir)%0A%20%20%20%20%23This%20will%20be%20the%20last%20step%0A%20%20%20%20rm%20%24workingDir%5C%24tempDir%20-Recurse%20%20%20%0A%7D%0A%0A%0A%0AUPLOAD-FILE%20-workingDir%20%24workingDirectory%20-tempDir%20%24tempDirectory%20-clientId%20%24CLIENT_ID%20-clientSecret%20%24CLIENT_SECRET%20-artifactname%20%24artifactname%0Apowershell%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1971767%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2047354%22%20slang%3D%22en-US%22%3ERe%3A%20Uploading%20folders%20in%20sharepoint%20from%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2047354%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F797152%22%20target%3D%22_blank%22%3E%40Ankit007%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20could%20modify%20the%20UPLOAD-FILE%20function%20so%20that%20it%20identifies%20files%20with%20a%20.zip%20extension%20and%20skips%20running%20the%20CREATE-ARCHIVE%20function%2C%20just%20assigns%20them%20to%20the%20%24file%20variable.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EFunction%20UPLOAD-FILE%0A%7B%0A%20%20%20%20param(%24workingDir%2C%20%24tempDir%2C%20%24clientId%2C%20%24clientSecret%2C%20%24artifactname)%0A%20%20%0A%20%20%20%20Get-ChildItem%20%24workingDir%20%7C%20ForEach-Object%20%7B%0A%20%20%20%20%24name%20%3D%20%24_.FullName.Split(%22%5C%22)%5B-1%5D%0A%20%20%20%20if%20(%24name%20-contains%20%22.zip)%20%7B%0A%20%20%20%20%20%20%20%20%23skip%20running%20CREATE-ARCHIVE%0A%20%20%20%20%20%20%20%20%24file%20%3D%20%24_.FullName%20%2B%20%22%5C%22%20%2B%24tempDir%20%2B%20%22%5C%22%20%2B%20%22%24(%24name)%22%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20%20CREATE-ARCHIVE%20-workingDir%20%24_.FullName%20-tempDir%20%24tempDir%20-name%20%24name%0A%20%20%20%20%20%20%20%20%20%24file%20%3D%20%24_.FullName%20%2B%20%22%5C%22%20%2B%24tempDir%20%2B%20%22%5C%22%20%2B%20%22%24(%24name).zip%22%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%7D%0A%20%20%20%20%24fileSize%20%3D%20(Get-Item%20%24file).length%20%20%20%20%20%20%20%0A%20%20%20%20%24uploadURLObject%20%3D%20GET-UPLOADLINK%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%20-artifactname%20%24name%0A%20%20%20%20%24tokenObject%20%3D%20GET-TOKEN%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%0A%20%20%20%20%24uploadHeaders%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Type%22%2C%20%22application%2Fjson%22)%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Range%22%2C%20%22bytes%20%22%20%2B%200%20%2B%22-%22%20%2B%20(%24fileSize-1)%20%2B%20%22%2F%22%20%2B%20%24fileSize)%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Length%22%2C%20%24fileSize)%0A%20%20%20%20%24uploadHeaders.Add(%22Authorization%22%2C%20%22Bearer%20%22%2B%20%24tokenObject.access_token)%20%20%20%20%0A%20%20%20%20%24uploadBody%20%3D%20%5BSystem.IO.File%5D%3A%3AReadAllBytes(%24file)%0A%20%20%20%20%24response%20%3D%20Invoke-RestMethod%20%24uploadURLObject.uploadUrl%20-Method%20'PUT'%20-Headers%20%24uploadHeaders%20-Body%20%24uploadBody%0A%20%20%20%20%24response%20%7C%20ConvertTo-Json%20%20%0A%20%20%20%20REMOVE-TEMPDIR%20-workingDir%20%24_.FullName%20-tempDir%20%24tempDirectory%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3ESteve%20Correia%3C%2FP%3E%3CP%3Etumtum1973%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2047355%22%20slang%3D%22en-US%22%3ERe%3A%20Uploading%20folders%20in%20sharepoint%20from%20powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2047355%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F797152%22%20target%3D%22_blank%22%3E%40Ankit007%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20could%20modify%20the%20UPLOAD-FILE%20function%20to%20skip%20files%20with%20a%20.zip%20extension%20and%20assign%20them%20to%20the%20%24file%20value%20for%20upload.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20upload%20the%20file%0AFunction%20UPLOAD-FILE%0A%7B%0A%20%20%20%20param(%24workingDir%2C%20%24tempDir%2C%20%24clientId%2C%20%24clientSecret%2C%20%24artifactname)%0A%20%20%0A%20%20%20%20%0A%20%20%20%20Get-ChildItem%20%24workingDir%20%7C%20ForEach-Object%20%7B%0A%20%20%20%20%0A%20%20%20%20%24name%20%3D%20%24_.FullName.Split(%22%5C%22)%5B-1%5D%0A%20%20%20%20if%20(%24name%20-contains%20%22.zip%22)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%23skip%20file%0A%20%20%20%20%20%20%20%20%20%20%24file%20%3D%20%24_.FullName%20%2B%20%22%5C%22%20%2B%24tempDir%20%2B%20%22%5C%22%20%2B%20%22%24(%24name)%22%0A%20%20%20%20%7D%0A%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20%20CREATE-ARCHIVE%20-workingDir%20%24_.FullName%20-tempDir%20%24tempDir%20-name%20%24name%0A%20%20%20%20%20%20%20%20%20%24file%20%3D%20%24_.FullName%20%2B%20%22%5C%22%20%2B%24tempDir%20%2B%20%22%5C%22%20%2B%20%22%24(%24name).zip%22%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%24fileSize%20%3D%20(Get-Item%20%24file).length%20%20%20%20%20%20%20%0A%20%20%20%20%24uploadURLObject%20%3D%20GET-UPLOADLINK%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%20-artifactname%20%24name%0A%20%20%20%20%24tokenObject%20%3D%20GET-TOKEN%20-clientId%20%24clientId%20-clientSecret%20%24clientSecret%0A%20%20%20%20%24uploadHeaders%20%3D%20New-Object%20%22System.Collections.Generic.Dictionary%5B%5BString%5D%2C%5BString%5D%5D%22%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Type%22%2C%20%22application%2Fjson%22)%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Range%22%2C%20%22bytes%20%22%20%2B%200%20%2B%22-%22%20%2B%20(%24fileSize-1)%20%2B%20%22%2F%22%20%2B%20%24fileSize)%0A%20%20%20%20%24uploadHeaders.Add(%22Content-Length%22%2C%20%24fileSize)%0A%20%20%20%20%24uploadHeaders.Add(%22Authorization%22%2C%20%22Bearer%20%22%2B%20%24tokenObject.access_token)%20%20%20%20%0A%20%20%20%20%24uploadBody%20%3D%20%5BSystem.IO.File%5D%3A%3AReadAllBytes(%24file)%0A%20%20%20%20%24response%20%3D%20Invoke-RestMethod%20%24uploadURLObject.uploadUrl%20-Method%20'PUT'%20-Headers%20%24uploadHeaders%20-Body%20%24uploadBody%0A%20%20%20%20%24response%20%7C%20ConvertTo-Json%20%20%0A%20%20%20%20REMOVE-TEMPDIR%20-workingDir%20%24_.FullName%20-tempDir%20%24tempDirectory%0A%20%20%20%20%7D%0A%20%20%20%20%0A%20%20%20%20%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

My below code successfully read a folder and uploades the subfolder one by one as a zip file in SharePoint. But if any of my file is of Zip extension, it gives me an error. I would like to know how can I fix my existing code below to handle the zip files as well so that it loads both the zip folder and non-zip files and folders in the SharePoint. I tried multiple tings like removing .zip extension in the Create-Archive and file but it does not work. error received:

2020-12-09T15:49:05.7979920Z Invoke-RestMethod : The remote server returned an error: (400) Bad Request. 2020-12-09T15:49:05.7981233Z At D:\a\r1\a_DevOpsScripts\ReleaseNoteScripts\UploadInSharePoint.ps1:55 char:17 2020-12-09T15:49:05.7982104Z + ... $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT ... 2020-12-09T15:49:05.7982819Z + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2020-12-09T15:49:05.7983312Z + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 2020-12-09T15:49:05.7983979Z eption 2020-12-09T15:49:05.7984369Z + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

 

param($ClientID,$ClientSecret,$WId,$RName,$RReqFor,$SPid,$LibId)
$tempDirectory = "temp"
# arguments defined from predefined variables in argument section of powershell stage of pipeline
$workingDirectory = "$WId"
$CLIENT_ID = "$ClientID"
$CLIENT_SECRET = "$ClientSecret"
$SharePointSiteId = "$SPid"
$LibraryId = "$LibId"
$azx = "$RName" +"_"+"$RReqFor"
$adx = ($azx.Split("[")[0])
$artifactname = $adx.Substring(0,$adx.Length-1)


# Get access token
Function GET-TOKEN 
{
    param($clientId, $clientSecret)
    $tokenHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $tokenHeaders.Add("Content-Type", "application/x-www-form-urlencoded")
    $tokenBody = "client_id="+ $clientId + "&scope=https%3A//graph.microsoft.com/.default&client_secret=" + $clientSecret + "&grant_type=client_credentials"
    $tokenBody
    $tokenResponse = Invoke-RestMethod 'https://login.microsoftonline.com/test.onmicrosoft.com/oauth2/v2.0/token' -Method 'POST' -Headers $tokenHeaders -Body $tokenBody
    return $tokenResponse    
}


# upload the file
Function UPLOAD-FILE
{
    param($workingDir, $tempDir, $clientId, $clientSecret, $artifactname)
  
    
    Get-ChildItem $workingDir | ForEach-Object {
    
    $name = $_.FullName.Split("\")[-1]
    CREATE-ARCHIVE -workingDir $_.FullName -tempDir $tempDir -name $name
    $file = $_.FullName + "\" +$tempDir + "\" + "$($name).zip"         
    
    $fileSize = (Get-Item $file).length       
    $uploadURLObject = GET-UPLOADLINK -clientId $clientId -clientSecret $clientSecret -artifactname $name
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadHeaders.Add("Content-Type", "application/json")
    $uploadHeaders.Add("Content-Range", "bytes " + 0 +"-" + ($fileSize-1) + "/" + $fileSize)
    $uploadHeaders.Add("Content-Length", $fileSize)
    $uploadHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)    
    $uploadBody = [System.IO.File]::ReadAllBytes($file)
    $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Body $uploadBody
    $response | ConvertTo-Json  
    REMOVE-TEMPDIR -workingDir $_.FullName -tempDir $tempDirectory
    }
    
    
}
Function GET-UPLOADLINK
{
    param($clientId, $clientSecret, $artifactname)
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadLinkRequestHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadLinkRequestHeaders.Add("Content-Type", "application/json")
    $uploadLinkRequestHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)
    $uploadLinkRequestBody = ""     
    #update the actual link  
    $fullname = "https://graph.microsoft.com/v1.0/sites/$($SPid)/drives/$($LibId)/root/children/"    
    $comname = $fullname + $artifactname    
    $uploadLinkResponse = Invoke-RestMethod "$($comname).zip/createUploadSession" -Method 'POST' -Headers $uploadLinkRequestHeaders -Body $uploadLinkRequestBody
    $uploadLinkResponse | ConvertTo-Json
    return $uploadLinkResponse    
}

Function CREATE-ARCHIVE
{
    param($workingDir, $tempDir, $name )
    write-host $workingDir
    cd $workingDir
    md $tempDir
    #Compress-Archive -Path $workingDir -DestinationPath $workingDir\$tempDir\$artifactname
    Compress-Archive -Path $workingDir -DestinationPath $workingDir\$tempDir\$name
}

Function REMOVE-TEMPDIR
{
    param($workingDir, $tempDir)
    #This will be the last step
    rm $workingDir\$tempDir -Recurse   
}



UPLOAD-FILE -workingDir $workingDirectory -tempDir $tempDirectory -clientId $CLIENT_ID -clientSecret $CLIENT_SECRET -artifactname $artifactname
powershell

 

2 Replies

@Ankit007 

 

You could modify the UPLOAD-FILE function so that it identifies files with a .zip extension and skips running the CREATE-ARCHIVE function, just assigns them to the $file variable.  

 

 

Function UPLOAD-FILE
{
    param($workingDir, $tempDir, $clientId, $clientSecret, $artifactname)
  
    Get-ChildItem $workingDir | ForEach-Object {
    $name = $_.FullName.Split("\")[-1]
    if ($name -contains ".zip") {
        #skip running CREATE-ARCHIVE
        $file = $_.FullName + "\" +$tempDir + "\" + "$($name)"         
    }
    else {
         CREATE-ARCHIVE -workingDir $_.FullName -tempDir $tempDir -name $name
         $file = $_.FullName + "\" +$tempDir + "\" + "$($name).zip"         
    }
    $fileSize = (Get-Item $file).length       
    $uploadURLObject = GET-UPLOADLINK -clientId $clientId -clientSecret $clientSecret -artifactname $name
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadHeaders.Add("Content-Type", "application/json")
    $uploadHeaders.Add("Content-Range", "bytes " + 0 +"-" + ($fileSize-1) + "/" + $fileSize)
    $uploadHeaders.Add("Content-Length", $fileSize)
    $uploadHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)    
    $uploadBody = [System.IO.File]::ReadAllBytes($file)
    $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Body $uploadBody
    $response | ConvertTo-Json  
    REMOVE-TEMPDIR -workingDir $_.FullName -tempDir $tempDirectory
    }
    
    
}

 

 

Thanks,

Steve Correia

tumtum1973

@Ankit007 

 

You could modify the UPLOAD-FILE function to skip files with a .zip extension and assign them to the $file value for upload.

 

# upload the file
Function UPLOAD-FILE
{
    param($workingDir, $tempDir, $clientId, $clientSecret, $artifactname)
  
    
    Get-ChildItem $workingDir | ForEach-Object {
    
    $name = $_.FullName.Split("\")[-1]
    if ($name -contains ".zip") {
          #skip file
          $file = $_.FullName + "\" +$tempDir + "\" + "$($name)"
    }
    else {
         CREATE-ARCHIVE -workingDir $_.FullName -tempDir $tempDir -name $name
         $file = $_.FullName + "\" +$tempDir + "\" + "$($name).zip"
    }
    
    $fileSize = (Get-Item $file).length       
    $uploadURLObject = GET-UPLOADLINK -clientId $clientId -clientSecret $clientSecret -artifactname $name
    $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
    $uploadHeaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $uploadHeaders.Add("Content-Type", "application/json")
    $uploadHeaders.Add("Content-Range", "bytes " + 0 +"-" + ($fileSize-1) + "/" + $fileSize)
    $uploadHeaders.Add("Content-Length", $fileSize)
    $uploadHeaders.Add("Authorization", "Bearer "+ $tokenObject.access_token)    
    $uploadBody = [System.IO.File]::ReadAllBytes($file)
    $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Body $uploadBody
    $response | ConvertTo-Json  
    REMOVE-TEMPDIR -workingDir $_.FullName -tempDir $tempDirectory
    }
    
    
}