Uploading pictures - PowerShell Example

The sample PowerShell script below processes through a series of files in a folder, and uploads the file to the profile picture API endpoint. This assumes that the images are named with the person's ID as the file name, and the folder only contains image files

Add-Type -AssemblyName System.Net.Http

$interactUrl = "{URL to your interact intranet}"
$profileSourceApiKey = "{Profile Source API key - this is set when creating the profile source in Interact"
$profileSourceId = "{ID of the profile source in Interact}"
$filePath = "{Path to files on the filesystem}"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

function ProcessFiles(){
    Get-ChildItem $FilePath | 
      Foreach-Object {
        $personId = $_.BaseName

        $baseUri = $interactUrl + "/api/umi/" + $profileSourceId + "/upload/" + $personId + "/picture"

        SendFile $baseUri $_.FullName
      }
}


function SendFile([string]$uri, [string]$path){
 
    $httpClientHandler = New-Object System.Net.Http.HttpClientHandler
    $httpClient = New-Object System.Net.Http.Httpclient $httpClientHandler

    $packageFileStream = New-Object System.IO.FileStream @($path, [System.IO.FileMode]::Open)
        
    $contentDispositionHeaderValue = New-Object System.Net.Http.Headers.ContentDispositionHeaderValue "form-data"
    $contentDispositionHeaderValue.Name = "fileData"
    $contentDispositionHeaderValue.FileName = (Split-Path $path -leaf)
 
    $streamContent = New-Object System.Net.Http.StreamContent $packageFileStream
    $streamContent.Headers.ContentDisposition = $contentDispositionHeaderValue
    $streamContent.Headers.ContentType = New-Object System.Net.Http.Headers.MediaTypeHeaderValue "image/jpeg"
        
    $content = New-Object System.Net.Http.MultipartFormDataContent
    $content.Add($streamContent)
 
    $httpClient.DefaultRequestHeaders.Add("X-ApiKey", $profileSourceApiKey)

    try
    {
        $response = $httpClient.PostAsync($Uri, $content).Result
    }
    catch [Exception]
    {
        # log it
    }
    finally
    {
        if($null -ne $httpClient)
        {
            $httpClient.Dispose()
        }
 
        if($null -ne $response)
        {
            $response.Dispose()
        }
    }
}

ProcessFiles