Public/Bundles.ps1

function Get-TMBundle {
    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $false)][String]$Name,
        [Parameter(Mandatory = $false)][Switch]$ResetIDs,
        [Parameter(Mandatory = $false)][Switch]$Label

    )

    $TMSession = Get-TMSession $TMSession
    $Response = Invoke-TMRestMethod -Api bundle -Method Get
    $Results = $Response | Sort-Object -Property 'name'

    if ($ResetIDs) {
        for ($i = 0; $i -lt $Results.Count; $i++) {

            $Results[$i].assetQuantity = $null
            $Results[$i].id = $null
        }
    }

    if ($Name) {
        $Results = $Results | Where-Object { $_.name -eq $Name }
    }

    return $Results
}


function New-TMBundle {
    [CmdletBinding(DefaultParameterSetName = 'ByObject')]      # Always add CmdletBinding to expose the common Cmdlet variables
    param(
        [Parameter(Mandatory = $false)][psobject]$TMSession = 'Default',

        [Parameter(Mandatory = $true, ParameterSetName = 'ByObject')][psobject]$Bundle,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByProperties')][string]$Name,
        [Parameter(Mandatory = $false, ParameterSetName = 'ByProperties')][string]$Description = [string]::Empty,
        [Parameter(Mandatory = $false, ParameterSetName = 'ByProperties')][bool]$UseForPlanning = $true,

        [Parameter(Mandatory = $false)][switch]$PassThru
    )

    $TMSession = Get-TMSession $TMSession

    if ( $PSCmdlet.ParameterSetName -eq 'ByProperties' ) {
        $Bundle = New-Object -TypeName psobject -ArgumentList @{
            Name           = $Name
            Description    = $Description
            UseForPlanning = $UseForPlanning
        }
    }

    $ExistingBundle = Get-TMBundle -Name $Bundle.name -TMSession $TMSession
    if ($ExistingBundle) {
        Write-Verbose 'Bundle already exists' -ForegroundColor Yellow
        return $PassThru.IsPresent ? $ExistingBundle : $null
    }

    $bodyParams = @{
        name           = $Bundle.name
        description    = $Bundle.description
        useForPlanning = $Bundle.useForPlanning
    }
    $Response = Invoke-TMRestMethod -Api bundle -Method Post -bodyParams $bodyParams

    if ($PassThru.IsPresent) {
        return $Response
    }
}