Skip to content

Commit 7a61cfd

Browse files
committed
OneDrive
1 parent 59f5d58 commit 7a61cfd

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

Hello World.pdf

31.1 KB
Binary file not shown.

Hello World.png

3.68 KB
Loading

Hello World.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
au2mator.com

Upload File to Onedrive.ps1

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)