Public/Unpublish-GhostPost.ps1

function Unpublish-GhostPost {
    <#
    .SYNOPSIS
        Reverts a published or scheduled post to a draft.

    .DESCRIPTION
        Takes a published post off the site, or cancels the publication of a scheduled post. The post and its
        content are kept as a draft. An email that was already sent is not taken back.

    .EXAMPLE
        Unpublish-GhostPost -Id '5b7ada404f87d200b5b1f9c8'

        Reverts the post to a draft.

    .EXAMPLE
        Get-GhostPost -Filter 'status:scheduled' | Unpublish-GhostPost

        Cancels the publication of every scheduled post.

    .LINK
        https://docs.ghost.org/admin-api/posts/updating-a-post
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('Ghost.Post')]
    param(
        # Id of the post to revert to a draft.
        [Parameter(Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [string] $Id,

        # The updated_at value of the post as it was read. Defaults to the current value of the post.
        [Parameter(ValueFromPipelineByPropertyName)]
        [Alias('updated_at')]
        [datetime] $UpdatedAt
    )

    begin {
        Test-GhostConnection
        $resources = Get-GhostAPIData -Endpoint $MyInvocation.MyCommand.Name
    }

    process {
        try {
            # A copy, so that values can be added without leaking into the next object from the pipeline.
            $parameters = [hashtable]::new($PSBoundParameters, [System.StringComparer]::OrdinalIgnoreCase)
            $parameters['Status'] = 'draft'

            if (-not $parameters.ContainsKey('UpdatedAt')) {
                $current = @{
                    Endpoint   = $resources.URI
                    Parameters = $parameters
                    Location   = $resources.Result
                }
                $parameters['UpdatedAt'] = Get-GhostUpdatedAt @current
            }

            $uri = New-URIString -Endpoint $resources.URI -Parameters $parameters
            $body = New-BodyString -BodyKeys $resources.Body -Parameters $parameters -Wrapper $resources.Result

            if ($PSCmdlet.ShouldProcess($Id, $resources.Description)) {
                $result = Submit-Request -Uri $uri -Method $resources.Method -Body $body -Success $resources.Success
                $result = Test-ReturnFormat -Result $result -Location $resources.Result

                Set-ObjectTypeName -InputObject $result -TypeName $resources.TypeName
            }
        }
        catch {
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}