Sunday, January 16, 2022

Copy Applications from One Delivery Group to Another

A forum user asked if there was an easy way to copy all applications from one delivery group to another. Here is a simple PowerShell script to accomplish this:

Function Select-DG ($DGs, $Title = 'Select Delivery Group'){

    Write-Host ""
    Write-Host "=====   Delivery Groups   ====="
    $menuDGs = @{}
    For ($i=1;$i -le $DGs.count; $i++)
    {
        Write-Host "$i. $($DGs[$i-1].Name)"
        $menuDGs.Add($i,($DGs[$i-1].Name))
    }
    [int]$ansDG = if(($ansDG = Read-Host $Title) -eq ''){0} else {$ansDG}

    if ($ansDG -eq 0) { return 0 }
    if ($ansDG -gt 0 -and $ansDG -lt $DGs.Count) {
        $DGSelected = $menuDGs.Item($ansDG)
        (Get-BrokerDesktopGroup | ? Name -eq "$($DGSelected)").UID
    } else {
        Write-Host "Selection not valid, please make a valid selection between 1 and $($i-1)..."
        return 0
    }
}

# retrieve Delivery Groups and pass to function so you don't have to retrieve them twice
$DeliveryGroups = Get-BrokerDesktopGroup | Sort Name | Select Name, UID

$sourceDG = Select-DG $DeliveryGroups "Select the source Delivery Group (0 to exit)"
if ($sourceDG -ne 0) {
    $targetDG = Select-DG $DeliveryGroups "Select the target Delivery Group (0 to exit)"
}
if ($targetDG -eq 0) { return }
if ($sourceDG -eq $targetDG) {
    Write-Host "Target Delivery Group may not be the same as the Source Delivery Group."
    return
}
$DGApps = Get-BrokerApplication | ? AllAssociatedDesktopGroupUids -contains $sourceDG

$sourceName = (Get-BrokerDesktopGroup | ? Uid -eq $sourceDG).Name
$targetName = (Get-BrokerDesktopGroup | ? Uid -eq $targetDG).Name
Write-Host ""
Write-Host "Source Delivery Group: $($sourceName)"
Write-Host "Target Delivery Group: $($targetName)"

# copy each app
foreach ($app in $DGApps) {
    Write-Host "Copying: $($app.BrowserName)"
    $app | Add-BrokerApplication -DesktopGroup $targetName
}
Write-Host "Done."