|
| 1 | +$clientID_OneDrive = "your Client ID" |
| 2 | +$Clientsecret_OneDrive = "your Secret" |
| 3 | +$tenantID = "your Tenant ID" |
| 4 | + |
| 5 | +$GraphBaseURL="https://graph.microsoft.com/v1.0" |
| 6 | + |
| 7 | +#OneDrive User |
| 8 | +$UserUPN="youer UPN" |
| 9 | + |
| 10 | +#Authentication |
| 11 | +$tokenBody_OneDrive = @{ |
| 12 | + Grant_Type = "client_credentials" |
| 13 | + Scope = "https://graph.microsoft.com/.default" |
| 14 | + Client_Id = $clientID_OneDrive |
| 15 | + Client_Secret = $Clientsecret_OneDrive |
| 16 | +} |
| 17 | +$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantID/oauth2/v2.0/token" -Method POST -Body $tokenBody_OneDrive |
| 18 | +$headers_OneDrive = @{ |
| 19 | + "Authorization" = "Bearer $($tokenResponse.access_token)" |
| 20 | + "Content-type" = "application/json" |
| 21 | +} |
| 22 | + |
| 23 | +#Get the Drive ID |
| 24 | +$Drive = Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drive" -Method GET -Headers $headers_OneDrive |
| 25 | + |
| 26 | +#Get the Folder |
| 27 | +$DestFolder = Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/root:/0-Temp" -Method GET -Headers $headers_OneDrive |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +#Word Document |
| 34 | +$File="Hello World.docx" |
| 35 | +$fileName=$File.Split("\")[-1] |
| 36 | +#Upload |
| 37 | +Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'application/docx' |
| 38 | + |
| 39 | + |
| 40 | +#pdf Document |
| 41 | +$File="Hello World.pdf" |
| 42 | +$fileName=$File.Split("\")[-1] |
| 43 | +#Upload |
| 44 | +Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'application/pdf' |
| 45 | + |
| 46 | + |
| 47 | +#TXT Document |
| 48 | +$File="Hello World.txt" |
| 49 | +$fileName=$File.Split("\")[-1] |
| 50 | +#Upload |
| 51 | +Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'plain/text' |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +#Image |
| 56 | +$File="Hello World.png" |
| 57 | +$fileName=$File.Split("\")[-1] |
| 58 | +#Upload |
| 59 | +Invoke-RestMethod -Uri "$GraphBaseURL/users/$UserUPN/drives/$($Drive.id)/items/$($DestFolder.id):/$($FileName):/content" -Method PUT -Headers $headers_OneDrive -InFile $file -ContentType 'image/png' |
0 commit comments