Jump to content
Welcome to our new Citrix community!
  • 0

Powershell command to duplicate application XenApp 7.x


Nushrath Pathan

Question

1 answer to this question

Recommended Posts

  • 0

This is the code I developed for my needs... You can modify to suit your needs...??   (It's not 100% perfect, but it does a good job so long as the apps don't already exist in the to site.)

 

You don't really mention if you are talking same site or different site I guess, so this may not be at all what you are looking for. :/ 

 

############################################################################
# VARIABLE DEFINITIONS
############################################################################
$global:FROMAdminAddress = '<<deliverycontrollerFQDN>>:80'
$global:TOAdminAddress = '<<deliverycontrollerFQDN>>:80'

############################################################################
# FUNCTION DEFINITIONS
############################################################################

function Verify-Snapins
{
$LoadedSnapins = Get-PSSnapin -Name *citrix* -ErrorAction SilentlyContinue | Select-Object Name
If($LoadedSnapins.Name -contains 'Citrix.Broker.Admin.V2')
    {
    Write-Host "Successfully verified that necessary Citrix components are loaded in PowerShell" -ForegroundColor Yellow
    }
Else
    {
    $RegisteredSnapins = Get-PSSnapin -Name *citrix* -Registered | Select-Object Name
    If($RegisteredSnapins.Name -contains 'Citrix.Broker.Admin.V2')
        {
        Write-Host "Citrix components are not currently loaded in to PowerShell. Attempting to load components." -ForegroundColor DarkYellow
        Add-PSSnapin *citrix*
        Verify-Snapins
        }
    Else
        {
        Read-Host "The necessary Citrix components to execute this script are not installed on this computer. Press enter to exit."
        Exit
        }
    }
}

function Reset-Script
{
$Message = "Operation completed."
$Question = "Do you want to exit or start over?"

$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&StartOver'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Exit'))

$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) 
    {
    Write-Host 'Starting over!' -ForegroundColor Yellow
    Start-Script
    } 
else 
    {
    Write-Host 'Exiting!' -ForegroundColor Yellow
    Exit
    }
}

function Select-DeliveryGroup
{
    $DeliveryGroups = (Get-BrokerDesktopGroup -AdminAddress $global:FROMAdminAddress).Name
    $i=1
    Write-Host "Please choose a customer Delivery Group to migrate applications for:" -ForegroundColor Yellow
    ForEach($DeliveryGroup in $DeliveryGroups)
        {
        Write-Host "$i. $DeliveryGroup"
        $i++
        }
    [int]$Choice = Read-Host 'Enter the number for the delivery group to choose'
    $global:DeliveryGroupChoice=$DeliveryGroups[$choice-1]
    Write-Host "$($global:DeliveryGroupChoice) was selected as the Delivery Group." -ForegroundColor Yellow
}

function Export-Apps
{
    # Setting start up location
    $Location = $MyInvocation.MyCommand.Path -replace $MyInvocation.MyCommand.Name,"";
    set-location $Location;

    # Grabs applications
    #$apps = Get-BrokerApplication -MaxRecordCount 2147483647
    $apps =  Get-BrokerApplication -AdminAddress $global:FROMAdminAddress -DesktopGroupUid (Get-BrokerDesktopGroup -AdminAddress $global:FROMAdminAddress -Name $global:DeliveryGroupChoice).Uid -MaxRecordCount 2147483647

    $global:Results = @()

    foreach($app in $apps)
    {
        # Building Properties for each application
    
        $Properties = @{
        AdminFolderName = $app.AdminFolderName
        AdminFolderUid = $app.AdminFolderUid
        ApplicationName = $app.ApplicationName
        ApplicationType = $app.ApplicationType
        AssociatedDesktopGroupPriorities = $app.AssociatedDesktopGroupPriorities
        AssociatedDesktopGroupUUIDs = $app.AssociatedDesktopGroupUUIDs
        AssociatedDesktopGroupUids = $app.AssociatedDesktopGroupUids
        AssociatedUserFullNames = $app.AssociatedUserFullNames
        AssociatedUserNames = $app.AssociatedUserNames
        AssociatedUserUPNs = $app.AssociatedUserUPNs
        BrowserName = $app.BrowserName
        ClientFolder = $app.ClientFolder
        CommandLineArguments = $app.CommandLineArguments
        CommandLineExecutable = $app.CommandLineExecutable
        CpuPriorityLevel = $app.CpuPriorityLevel
        Description = $app.Description
        Enabled = $app.Enabled
        IconFromClient = $app.IconFromClient
        EncodedIconData = (Get-Brokericon -AdminAddress $global:FROMAdminAddress -Uid $app.IconUid).EncodedIconData # Grabs Icon Image
        MetadataKeys = $app.MetadataKeys
        MetadataMap = $app.MetadataMap
        Name = $app.Name
        PublishedName = $app.PublishedName
        SecureCmdLineArgumentsEnabled = $app.SecureCmdLineArgumentsEnabled
        ShortcutAddedToDesktop = $app.ShortcutAddedToDesktop
        ShortcutAddedToStartMenu = $app.ShortcutAddedToStartMenu
        StartMenuFolder = $app.StartMenuFolder
        UUID = $app.UUID
        Uid = $app.Uid
        UserFilterEnabled = $app.UserFilterEnabled
        Visible = $app.Visible
        WaitForPrinterCreation = $app.WaitForPrinterCreation
        WorkingDirectory = $app.WorkingDirectory
        }
 #       $app | gm
 #       write-host $app.AdminFolderName -ForegroundColor Red
 #       break
        # Stores each Application setting for export
        $global:Results += New-Object psobject -Property $properties
    }

    <#

    # Setting File name with time stamp
    $Date = Get-Date
    $FileName = $Date.ToShortDateString() + $Date.ToLongTimeString()
    $FileName = (($FileName -replace ":","") -replace " ","") -replace "/",""
    $FileName = "Apps" + $FileName + ".xml"

    # Exporting results
    $global:Results | export-clixml .\$FileName   

    #>
}

function Import-Apps
{
    # Importing applications and associating icons with applications 
    # Setting Delivery Group
    $dg = $global:DeliveryGroupChoice
 
    # Importing Application Data
    # Set the path to your .xml
    $apps = $global:Results
 
    foreach ($app in $apps)
    {
        
        Write-Host "Working on Application: " $app.Name -ForegroundColor Green
       
        If ((Get-Brokerapplication -Name $app.Name -AdminAddress $global:TOAdminAddress -ErrorAction Ignore).Count)
        {

        #Do Not attempt to recreate the application, just use the existing one and add the Delivery Group to it
            Write-Host "Adding Application - Not creating new"
            Try
            {
                Add-BrokerApplication -AdminAddress $global:TOAdminAddress -Name $app.PublishedName -DesktopGroup $global:DeliveryGroupChoice | Out-Null
            }
            Catch
            {
                Add-BrokerApplication -AdminAddress $global:TOAdminAddress -Name $app.Name -DesktopGroup $global:DeliveryGroupChoice | Out-Null
            }
        } else {
        If ($app.AdminFolderName -ne $null -and (Get-BrokerAdminFolder -Name $app.AdminFolderName -AdminAddress $global:TOAdminAddress -ErrorAction Ignore).Count -eq 0) 
        {
            $adminFolder = $app.AdminFolderName.Split("\")
            $folderpath = ""
            $fproot = ""
            foreach ($folder in $adminFolder) 
            {
                if ($folder -ne "")
                {
                    $folderpath += $folder + "\"
                    if ((Get-BrokerAdminFolder -Name $folderpath -AdminAddress $global:TOAdminAddress -ErrorAction Ignore).Count -eq 0)
                    {
                        If ($fproot -ne "") 
                        {
                            New-BrokerAdminFolder -FolderName $folder -ParentFolder $fproot -AdminAddress $global:TOAdminAddress
                        } else {
                            New-BrokerAdminFolder -FolderName $folder -AdminAddress $global:TOAdminAddress
                        }
                    }
                    $fproot = $folder
                }
            }
        }
           #Importing Icon 
          # write-host "Doing Icon - New-BrokerIcon -AdminAddress $global:TOAdminAddress"
           $IconUid = New-BrokerIcon -AdminAddress $global:TOAdminAddress -EncodedIconData $app.EncodedIconData
           $DoIcon = $true

           #Creating New Application

           #If the BrowserName already exists, modify to something that doesn't...
           If((Get-BrokerApplication -BrowserName $app.BrowserName).Count) {
             $unique = $False
             $counter = 0
             
             Do {
                $counter += 1
                $BNTemp = $app.BrowserName + $counter
                If(!(Get-BrokerApplication -BrowserName $BNTemp).Count) {
                    $app.BrowserName = $BNTemp
                    $unique = $True
                }
             }
             Until ($unique)
           }


           #Create the new Application
            Try
            {
                if ($app.CommandLineArguments.Length -lt 2) {$app.CommandLineArguments = " "} 
                #Adding Application
            

                write-host "AdminFolder" $app.AdminFolderName
                write-host "BrowserName" $app.BrowserName
                write-host "ComdLineExe" $app.CommandLineExecutable
                write-host "ClientFldr " $app.ClientFolder
                write-host "Description" $app.Description
                write-host "ComdLineArg" $app.CommandLineArguments
                write-host "Enabled    " $app.Enabled
                write-host "Name       " $app.Name
                write-host "UserFiltEna" $app.UserFilterEnabled
                write-host "WorkingDire" $app.WorkingDirectory
                write-host "Published  " $app.PublishedName

###Comment this out if first attempt fails and use alternate attempt

#<#             
                If($app.ClientFolder -ne $null)
                {
                $appClientFolder = $app.ClientFolder
                #write-host New-BrokerApplication -AdminAddress $global:TOAdminAddress -AdminFolder $app.AdminFolderName -ApplicationType $app.ApplicationType -BrowserName $app.BrowserName -CommandLineExecutable $app.CommandLineExecutable -Description $app.Description -ClientFolder $app.ClientFolder -CommandLineArguments $app.CommandLineArguments -Enabled $app.Enabled -Name $app.PublishedName -UserFilterEnabled $app.UserFilterEnabled -DesktopGroup $dg -WorkingDirectory $app.WorkingDirectory -PublishedName $app.PublishedName | Out-Null
                } else {
                $appClientFolder = "\"
                #write-host New-BrokerApplication -AdminAddress $global:TOAdminAddress -AdminFolder $app.AdminFolderName -ApplicationType $app.ApplicationType -BrowserName $app.BrowserName -CommandLineExecutable $app.CommandLineExecutable -Description $app.Description -ClientFolder $app.ClientFolder -CommandLineArguments $app.CommandLineArguments -Enabled $app.Enabled -Name $app.PublishedName -UserFilterEnabled $app.UserFilterEnabled -DesktopGroup $dg -WorkingDirectory $app.WorkingDirectory -PublishedName $app.PublishedName | Out-Null
                }
                New-BrokerApplication -AdminFolder $app.AdminFolderName -ApplicationType $app.ApplicationType -BrowserName $app.BrowserName -CommandLineExecutable $app.CommandLineExecutable -Description $app.Description -ClientFolder $appClientFolder -CommandLineArguments $app.CommandLineArguments -Enabled $app.Enabled -Name $app.PublishedName -UserFilterEnabled $app.UserFilterEnabled -DesktopGroup $dg -WorkingDirectory $app.WorkingDirectory -PublishedName $app.PublishedName | Out-Null

#>


<#
### Use this if the first attempt fails to properly create all applications

                $MakeApp = 'New-BrokerApplication -AdminAddress $global:TOAdminAddress -ApplicationType $app.ApplicationType'
                if($app.BrowserName -ne $null){$MakeApp += ' -BrowserName $app.BrowserName'}
                if($app.CommandLineExecutable -ne $null){$MakeApp += ' -CommandLineExecutable $app.CommandLineExecutable'}
                if($app.Description -ne $null){$MakeApp += ' -Description $app.Description'}
                if($app.ClientFolder -ne $null){$MakeApp += ' -ClientFolder $app.ClientFolder'}
                if($app.CommandLineArguments -ne $null){$MakeApp += ' -CommandLineArguments $app.CommandLineArguments'}
                if($app.PublishedName -ne $null){$MakeApp += ' -Name $app.PublishedName'} 
                if($app.UserFilterEnabled -ne $null){$MakeApp += ' -UserFilterEnabled $app.UserFilterEnabled'}
                if($app.Enabled -ne $null){$MakeApp += ' -Enabled $app.Enabled'}
                if($dg -ne $null){$MakeApp += ' -DesktopGroup $dg'}
                if($app.WorkingDirectory -ne $null){$MakeApp += ' -WorkingDirectory $app.WorkingDirectory'}
                if($app.PublishedName -ne $null){$MakeApp += ' -PublishedName $app.PublishedName'}

                Invoke-Expression $MakeApp | Out-Null
#>
            }
            Catch
            {
                write-host $_.Exception.Message
                write-host $_.Exception.ItemName
                write-host "Error on "  $app.BrowserName
                write-host "Error on "  $app.CommandLineExecutable
                write-host "Error on "  $app.Description
                write-host "Error on "  $app.CommandLineArguments
                write-host "Error on "  $app.Enabled
                write-host "Error on "  $app.Name
                write-host "Error on "  $app.UserFilterEnabled
            }
            
                #Setting applications icon  
            #     write-host "Doing - Set-BrokerApplication -AdminAddress $global:TOAdminAddress app.Name -IconUid IconUid.Uid"
            If($DoIcon) {
                Try
                {
                   Set-BrokerApplication -Name $app.Name -AdminAddress $global:TOAdminAddress -IconUid $IconUid.Uid | Out-Null
                }
                Catch
                {
                   Set-BrokerApplication -Name $app.PublishedName -AdminAddress $global:TOAdminAddress -IconUid $IconUid.Uid | Out-Null
                }
                   write-host "Working on Icon" $IconUid.Uid
            }
 
            # Adding User Associations
            If($app.AssociatedUserNames -ne $null) 
            {
                Try
                {
                    $users = $app.AssociatedUserNames
 
                    foreach($user in $users)
                    {
                        Try
                        {
                              Add-BrokerUser -AdminAddress $global:TOAdminAddress -Name "$user" -Application $app.Name | Out-Null
                        }
                        Catch
                        {
                              Add-BrokerUser -AdminAddress $global:TOAdminAddress -Name "$user" -Application $app.PublishedName | Out-Null
                        }
                    }
                }
                Catch
                {
                    write-host $_.Exception.Message
                    write-host $_.Exception.ItemName
                    write-host "Error on User  "  $user  $app.Name
                }
            }
        }
    }
 
}
function Start-Script
{
    Write-Host "We will perform the following: Select customer Delivery Group. Then export the apps for that group and import them into alternate Environment." -ForegroundColor Yellow
    Select-DeliveryGroup
    Export-Apps
    Import-Apps
}

############################################################################
# SCRIPT EXECUTION BLOCK
############################################################################

Write-Host "Welcome to the application provisioning tool for XenApp 7.6" -ForegroundColor Yellow
Verify-Snapins
Read-Host "Press enter to begin. Hit CTRL-C at any time to cancel the current operation and exit the script."
Start-Script

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...