Public/Bundles.ps1

function Get-TMBundle {
    <#
    .SYNOPSIS
        Gets Bundles from TransitionManager.
 
    .DESCRIPTION
        Retrieves Bundle records from TM and optionally filters them by name.
        Returned objects can also be normalized or label-focused with the
        available switches.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER Name
        The Bundle name to retrieve.
 
    .PARAMETER ResetIDs
        Switch indicating that identifiers on the returned objects should be
        refreshed or normalized during retrieval.
 
    .PARAMETER Label
        Switch indicating that label-oriented output should be returned when
        supported by the function.
 
    .EXAMPLE
        Get-TMBundle -Name 'Wave 1'
 
        Retrieves the Bundle named `Wave 1`.
 
    .NOTES
        This function resolves the session by calling `Get-TMSession`.
    #>

    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 {
    <#
    .SYNOPSIS
        Creates a Bundle in TransitionManager.
 
    .DESCRIPTION
        Creates a TM Bundle either from a prebuilt Bundle object or from
        individual Bundle properties such as name and description.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER Bundle
        The Bundle object to create when using the `ByObject` parameter set.
 
    .PARAMETER Name
        The name of the Bundle to create when using the `ByProperties`
        parameter set.
 
    .PARAMETER Description
        The description to assign to the new Bundle.
 
    .PARAMETER UseForPlanning
        Boolean indicating whether the Bundle should be marked for planning use.
 
    .PARAMETER PassThru
        Switch indicating that the created Bundle should be returned.
 
    .EXAMPLE
        $Bundle = [pscustomobject]@{ name = 'Wave 1'; description = 'Initial migration wave' }
        New-TMBundle -Bundle $Bundle -PassThru
 
        Creates a Bundle from a prepared object and returns it.
 
    .EXAMPLE
        New-TMBundle -Name 'Wave 2' -Description 'Follow-up migration wave' -UseForPlanning $true -PassThru
 
        Creates a Bundle from individual property values and returns it.
 
    .NOTES
        Choose the parameter set that best matches whether you already have a
        Bundle object prepared.
    #>

    [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'
        return $PassThru.IsPresent ? $ExistingBundle : $null
    }

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

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