Public/MIT/Invoke-MITFileUpload.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
function Invoke-MITFileUpload
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Id,
        [Parameter(Mandatory)]
        [string]$FilePath

    )

    # This function will only work on version 6 or later.
    if ($PSVersionTable.PSVersion.Major -lt 6) {
        throw "Invoke-MITFileUpload requires PowerShell 6 or later"
    }

    $hostname = $Script:MITHostname
    $token = $Script:MITToken
    $resource = "/folders/$Id/files"

    $Uri = "https://$hostname/api/v1/$Resource"
    $Headers = @{
            Accept = "application/json"
            Authorization = "Bearer $token"
        }
    $form = @{
        comments = "Uploaded via REST API"
        hashtype = 'sha-1'
        hash = (Get-FileHash -Algorithm 'SHA1' -Path $FilePath).Hash
        file = Get-Item -Path $FilePath
    }

    $response = Invoke-RestMethod -Method 'Post' -Uri $Uri -Headers $Headers -Form $form -TransferEncoding chunked
    Write-Output $response


}