Tuesday, September 11, 2012

XenApp Powershell Script to Publish Apps from CSV

As part of this Citrix migration project I am currently working on I had the necessity to publish multiple apps and the process was very time consuming and tedious. In order to accelerate and make the process more accurate I developed this script to publish apps from a CSV file. I also included some error checking/handling in the script for errors that I had encountered. Also be aware that for the icons to be correctly identified you will need to run this off the XA server that hosts the exe file with the path as per the "CommandLineExecutable" field.

The CSV file structure was setup as follows:
BrowserName,DisplayName,WorkerGroupNames,Accounts,CommandLineExecutable,WorkingDirectory,FolderPath,ClientFolder
Example App, Example, Test Worker Group, mshorrosh, C:\windows\system32\notepad.exe, c:\windows\system32\, Applications/Test, Test\


And the script....

# Load the snapins 
Add-PSSnapin citrix* -ErrorAction SilentlyContinue
#import the csv file
$apps = Import-Csv C:\apps.csv
#each line in the csv
foreach($app in $apps)
{
    #test if workergroup provided exists
    try{
        $WG = Get-XAWorkerGroup $app.WorkerGroupNames -ErrorAction Stop
     } catch {
        Write-Host "Error: " $app.BrowserName " resulted in " $_.Exception.Message " please create worker group first"
        break
     }
    #trying to get the executables icon from the CommandLineExecutable
    try{
        $EncodedIconData = Get-CtxIcon $app.CommandLineExecutable -index 0
    } catch [Exception] {
        Write-Host "Error: Obtaining the icon failed: " $_.Exception.Message
    }
    #checking browsername length, found out it has a limit
    if($app.BrowserName.length -gt 36)
    {
        Write-Host "Error: BrowserName for " $app.BrowserName " length is to long, must be less than 36 characters, please correct"
    }
    else
    {
   
        $success = $FALSE
       
        #try to publish new app
        try {
       
            $NewApp = New-XAApplication  `
            -ApplicationType "ServerInstalled" `
            -EncodedIconData $EncodedIconData `
            -WorkerGroupNames $app.WorkerGroupNames `
            -BrowserName $app.BrowserName `
            -Enabled $TRUE `
            -Accounts $app.Accounts `
            -WorkingDirectory $app.WorkingDirectory `
            -CommandLineExecutable $app.CommandLineExecutable `
            -FolderPath $app.FolderPath `
            -ClientFolder $app.ClientFolder  `
            -WindowType "100%" `
            -ColorDepth Colors32Bit `
            -InstanceLimit "-1" `
            -AddToClientStartMenu $false `
            -AnonymousConnectionsAllowed $false `
            -DisplayName $app.DisplayName `
            -ErrorAction Stop
            $success = $TRUE
        } catch {
             Write-Host "Error: " $_.Exception.Message
        } finally {
            if($success)
            {
                Write-Host $app.BrowserName "added successfully."
            }
        }
       
       
    }

   
}

No comments:

Post a Comment