Skip to content

Instantly share code, notes, and snippets.

@SingingBush
Created September 12, 2019 12:05

Revisions

  1. SingingBush created this gist Sep 12, 2019.
    19 changes: 19 additions & 0 deletions post-images-to-api.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@

    $baseUrl = 'http://api.domain.com/some-service/'

    Get-ChildItem -Path ".\*.jpg" | Sort-Object Name | % {
    $response = try {
    Invoke-RestMethod -uri "$baseUrl/image" -Method Post -Infile $_.Name -ContentType 'image/jpeg' -ErrorAction Stop
    } catch [System.Net.WebException] {
    Write-Error "Media Service returned $($_.Exception.Response.StatusCode.Value__) : $($_.Exception.Message)"
    Exit 1;
    }
    write-host "Postimg image for " -nonewline;
    write-host $_.BaseName -nonewline -foregroundcolor cyan;
    write-host " {0}" -f $response.id;

    New-Object PSObject -Property @{
    filename = $_.Name;
    id = $response.id;
    }
    } | Export-Csv 'image-upload-result.csv' -Encoding UTF8 -Delimiter "," -Force -NoTypeInformation