Public/AssetTypes/New-FreshServiceAssetType.ps1

<#
.SYNOPSIS
    Creates Freshservice asset type.
 
.DESCRIPTION
    Creates Freshservice asset type via REST API.
 
    https://api.freshservice.com/#create_an_asset_type
 
.PARAMETER name
    Name of the asset type..
 
.PARAMETER description
    Description of the asset type.
 
.PARAMETER parent_asset_type_id
    Unique id of the parent asset type.
 
.EXAMPLE
    New-FreshServiceAssetType -name MyAssetType
 
    id : 21002902008
    name : MyAssetType
    parent_asset_type_id :
    visible : True
    description :
    created_at : 4/12/2023 2:46:13 AM
    updated_at : 4/12/2023 2:46:13 AM
 
    Creates a new Freshservice Asset Type.
 
.NOTES
    This module was developed and tested with Freshservice REST API v2.
#>

function New-FreshServiceAssetType {
    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]

    param (
        [Parameter(
            Mandatory = $true,
            HelpMessage = 'Name of the Product.'
        )]
        [string]$name,
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Description of the asset type.'
        )]
        [string]$description,
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Unique id of the parent asset type.'
        )]
        [long]$parent_asset_type_id
    )
    begin {

        $PrivateData  = $MyInvocation.MyCommand.Module.PrivateData

        if (!$PrivateData.FreshserviceBaseUri) {
            throw "No connection found! Setup a new Freshservice connection with New-FreshServiceConnection and then Connect-FreshService. Set a default connection with New-FreshServiceConnection or Set-FreshConnection to automatically connect when importing the module."
        }

        $uri = [System.UriBuilder]('{0}/asset_types' -f $PrivateData['FreshserviceBaseUri'])

    }
    process {

        $jsonBody = @{}
        $PSBoundParameters.keys.where{
            $PSItem -notin $PrivateData.FreshserviceBodyExclusions
        }.foreach{
            #if ($PSBoundParameters[$PSItem] -is [boolean]) {
            # $jsonBody[$PSItem.ToLower()] = [int]$PSBoundParameters[$PSItem]
            #}
            #else {
                $jsonBody[$PSItem.ToLower()] = $PSBoundParameters[$PSItem]
            #}
        }

        try {
            if ($PSCmdlet.ShouldProcess($uri.Uri.AbsoluteUri)) {

                $params = @{
                    Uri         = $uri.Uri.AbsoluteUri
                    Method      = 'POST'
                    Body        = $jsonBody | ConvertTo-Json
                    ErrorAction = 'Stop'
                }

                $result = Invoke-FreshworksRestMethod @params

                if ($result.Content) {
                    $content = $result.Content |
                                    ConvertFrom-Json

                    #API returns singluar or plural property based on the number of records, parse to get property returned.
                    $objProperty = $content[0].PSObject.Properties.Name
                    Write-Verbose -Message ("Returning {0} property with count {1}" -f $objProperty, $content."$($objProperty)".Count)
                    $content."$($objProperty)"
                }

            }
        }
        catch {
            Throw $_
        }

    }
    end {}
}