HomeUser provisioningUpload profile pictures with PowerShell

Upload profile pictures with PowerShell

A sample PowerShell script that uploads a folder of images to the Interact profile picture API.

Note: This script is an example. Examples in these pages are samples, not officially supported by Interact Technical Support, and should be reviewed and tested by your development team before use.

Before you start

  • You need a profile source configured in Interact. See Profile pictures.
  • Name each image file with the person's ID, and ensure the folder contains only image files.

Overview

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

The script

Add-Type -AssemblyName System.Net.Http

$interactUrl = "{{intranet_url}}"
$profileSourceApiKey = "{{profile_source_api_key}}" # Set when you create the profile source in Interact
$profileSourceId = "{{source_id}}"
$filePath = "{{file_path}}"

[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
Section: User provisioning