public/Invoke-FacebookPostWithPhoto.ps1

function Invoke-FacebookPostWithPhoto {
 
    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$PageId,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Token,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Message,
        
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [ValidateSet('image','story')]
        [string]$Type

    )
    process{

        $ErrorActionPreference = 'Stop'

        try {
    
            $PathExists = Test-Path -Path $Path

            if ($PathExists) {

                $Source = Get-Item -Path $Path -ErrorAction Stop

                if ($Type -eq 'image') {
                
                    # Photos endpoint
                    $Uri = "https://graph.facebook.com/v22.0/$PageId/photos"

                    $Form = @{
                        message = $Message
                        access_token = $Token
                        source = $Source
                    }
   
                } # if

                if ($Type -eq 'story') {
                    
                    # Stories endpoint
                    $Uri = "https://graph.facebook.com/v22.0/$PageId/stories"

                    $Form = @{
                        access_token = $Token
                        source = $Source
                    }
    
                } # if

                $Response = Invoke-RestMethod -Uri $Uri -Method Post -Form $Form -ErrorAction Stop
                return $response

            } # if

        }
        catch {

            "$($MyInvocation.MyCommand.Name): $_.Exception.Message"
 
         } # trycatch

    } # process

} # function