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