Public/Get-GhostPost.ps1
|
function Get-GhostPost { <# .SYNOPSIS Gets posts from the Ghost site. .DESCRIPTION Without parameters every post is returned, whatever its status. Ghost returns posts in pages; all pages are retrieved unless -Limit or -Page is used, in which case exactly that page is returned. Use -Id or -Slug to get a single post. The content of a post is returned in the lexical property. Use -Formats to get it as HTML or plain text. .EXAMPLE Get-GhostPost Returns every post of the site. .EXAMPLE Get-GhostPost -Filter 'status:draft' -Order 'updated_at desc' -Limit 5 Returns the five drafts that were changed most recently. .EXAMPLE Get-GhostPost -Slug 'welcome' -Formats html, plaintext Returns the post with the slug welcome, with its content as HTML and as plain text. .EXAMPLE Get-GhostPost -Fields id, title, status Returns only the id, the title and the status of every post, which is much faster on a large site. .LINK https://docs.ghost.org/admin-api/posts/overview #> [CmdletBinding(DefaultParameterSetName = 'Browse')] [OutputType('Ghost.Post')] param( # Id of the post to get. [Parameter(Mandatory, ParameterSetName = 'Id', Position = 0, ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] [string] $Id, # Slug of the post to get. [Parameter(Mandatory, ParameterSetName = 'Slug')] [ValidateNotNullOrEmpty()] [string] $Slug, # Filter in the Ghost query language (NQL), such as status:draft or tag:news+featured:true. [Parameter(ParameterSetName = 'Browse')] [string] $Filter, # Sort order, such as 'published_at desc' or 'title asc'. [Parameter(ParameterSetName = 'Browse')] [string] $Order, # Number of posts in a page, 100 at most. Returns a single page instead of every post. [Parameter(ParameterSetName = 'Browse')] [ValidateRange(1, 100)] [int] $Limit, # Number of the page to return. Returns a single page instead of every post. [Parameter(ParameterSetName = 'Browse')] [ValidateRange(1, [int]::MaxValue)] [int] $Page, # Related data to include, such as authors and tags. [string[]] $Include, # Formats to return the content in. Ghost returns lexical when this is not used. [ValidateSet('html', 'plaintext', 'lexical', 'mobiledoc')] [string[]] $Formats, # Names of the properties to return, to limit the size of the response. [string[]] $Fields ) begin { Test-GhostConnection $resources = Get-GhostAPIData -Endpoint $MyInvocation.MyCommand.Name } process { try { # Every page is retrieved unless the caller asked for a specific one. $singlePage = $PSBoundParameters.ContainsKey('Limit') -or $PSBoundParameters.ContainsKey('Page') $paginate = $PSCmdlet.ParameterSetName -eq 'Browse' -and -not $singlePage $uri = New-URIString -Endpoint $resources.URI[$PSCmdlet.ParameterSetName] -Parameters $PSBoundParameters $uri = New-QueryString -Uri $uri -QueryKeys $resources.Query -Parameters $PSBoundParameters $result = Submit-Request -Uri $uri -Method $resources.Method -Success $resources.Success -Paginate:$paginate $result = Test-ReturnFormat -Result $result -Location $resources.Result Set-ObjectTypeName -InputObject $result -TypeName $resources.TypeName } catch { $PSCmdlet.ThrowTerminatingError($_) } } } |