PS: Upload CSV To Teams Sharepoint Site

2 minute read

Description:

As the title suggests, put the following snippets in a script if you want to upload a file to a Sharepoint Teams folder. Note that I have a service account added to my team in Teams and had to use the connect as user instead of connect as application method to Graph API which is not what I usually do. For it, you use the same New-MSGraphAPIToken function, but it is slightly tweaked:

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
   Function New-MSGraphAPIToken
   {
      <#
   .SYNOPSIS
   Acquire authentication token for MS Graph API
   .DESCRIPTION
   If you have a registered app in Azure AD, this function can help you get the authentication token
   from the MS Graph API endpoint. Each token is valid for 60 minutes.
   .PARAMETER ClientID
   This is the registered ClientID in AzureAD
   .PARAMETER ClientSecret
   This is the key of the registered app in AzureAD
   .PARAMETER TenantID
   This is your Office 365 Tenant Domain
   .EXAMPLE
   $graphToken = New-MSGraphAPIToken -ClientID <ClientID> -ClientSecret <ClientSecret> -TenantID <TenantID>
   The above example gets a new token using the ClientID, ClientSecret and TenantID combination
   .NOTES
   General notes
   #>
      param(
         [parameter(mandatory = $true)]
         [string]$ClientID,
         [parameter(mandatory = $true)]
         [string]$ClientSecret,
         [parameter(mandatory = $true)]
         [string]$TenantID,
         [parameter(mandatory = $true)]
         [string]$Username,
         [parameter(mandatory = $true)]
         [string]$Password

      )
      $body = @{
         Grant_Type    = "Password"
         client_Id     = $clientID
         Client_Secret = $clientSecret
         Username      = $Username
         Password      = $Password
         Scope         = "https://graph.microsoft.com/.default"
      }
      $oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token -Body $body
      $token = @{'Authorization' = "$($oauth.token_type) $($oauth.access_token)" }    
      Return $token
   }

For reference, here is how you would connect as an application

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
   Function New-MSGraphAPIToken
   {
      <#
      .SYNOPSIS
      Acquire authentication token for MS Graph API
      .DESCRIPTION
      If you have a registered app in Azure AD, this function can help you get the authentication token
      from the MS Graph API endpoint. Each token is valid for 60 minutes.
      .PARAMETER ClientID
      This is the registered ClientID in AzureAD
      .PARAMETER ClientSecret
      This is the key of the registered app in AzureAD
      .PARAMETER TenantID
      This is your Office 365 Tenant Domain
      .EXAMPLE
      $graphToken = New-MSGraphAPIToken -ClientID <ClientID> -ClientSecret <ClientSecret> -TenantID <TenantID>
      The above example gets a new token using the ClientID, ClientSecret and TenantID combination
      .NOTES
      General notes
      #>
      param(
         [parameter(mandatory = $true)]
         [string]$ClientID,
         [parameter(mandatory = $true)]
         [string]$ClientSecret,
         [parameter(mandatory = $true)]
         [string]$TenantID
      )
      $body = @{grant_type = "client_credentials"; scope = "https://graph.microsoft.com/.default"; client_id = $ClientID; client_secret = $ClientSecret }
      $oauth = Invoke-RestMethod -Method Post -Uri https://login.microsoftonline.com/$TenantID/oauth2/v2.0/token -Body $body
      $token = @{'Authorization' = "$($oauth.token_type) $($oauth.access_token)" }    
      Return $token
   }

To Resolve:

  1. Using the first function above, put it in your Begin{} block and then in your process block, put the following:

    1
    2
    3
    4
    5
    6
    7
    
    $token = New-MSGraphAPIToken -clientID $clientID -clientSecret $clientSecret -tenantID $tenantID -Username "myUser@domain.com" -Password $password
    
    # Perform the upload
    $uri = "https://graph.microsoft.com/v1.0/drives/drive-id/items/item-id:/phoneNumbers.csv:/content" 
    
    $ContentType = "text/plain"
    Invoke-RestMethod -Method Put -headers $token -Uri $uri -InFile $file -ContentType $ContentType
    
  2. To change the upload from csv or txt to use an Excel document, change text/plain to application/octet-stream. This has been tested and confirmed to work.

-----------------------------------------------------------
Spotted a mistake in this article? Why not suggest an edit!
Did you like the article? Donations are always welcome!
-----------------------------------------------------------

Comments