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."
            }
        }
       
       
    }

   
}

Friday, September 7, 2012

VBScript to rename Program Neighborhood Paths

The client I was working at had a multi-farm XenApp environment so I ended up needing a script that would run on XA 4.5 as well. This is essentially the same script I wrote in powershell translated to vbscript, edit appropriately as you need for your environment.


option explicit
Const MetaFrameWinAppObject = 3
Const MetaFrameWinFarmObject = 1
Dim aMFApp, hMFAppTemp, aMFTemp
Dim MFFarm : Set MFFarm = WScript.CreateObject ("MetaFrameCOM.MetaFrameFarm")
MFFarm.Initialize(MetaFrameWinFarmObject)
Dim mode, count, strReplace
mode = "Debug"
count = 0
Dim objECA, objAPAC, objAGLNG
Set objECA = New RegExp
Set objAPAC = New RegExp
Set objAGLNG = New RegExp
With objECA
                .Pattern = "ECA\\"
                .IgnoreCase = True
End With
With objAGLNG
                .Pattern = "AGLNG\\"
                .IgnoreCase = True
End With
With objAPAC
                .Pattern = "APAC\\"
                .IgnoreCase = True
End With
For Each aMFApp in MFFarm.Applications
Set hMFAppTemp = CreateObject("MetaFrameCOM.MetaFrameApplication")
hMFAppTemp.Initialize MetaFrameWinAppObject,aMFApp.DistinguishedName
hMFAppTemp.LoadData(True)
Set aMFTemp = hMFAppTemp.WinAppObject2
                If objAGLNG.Test(aMFTemp.PNFolder) = True Then
                                count = count + 1
                                strReplace = Replace(aMFTemp.PNFolder,"AGLNG\","")
                                If mode = "Debug" Then
                                                Wscript.Echo aMFTemp.AppName & " - old: " & aMFTemp.PNFolder & " new: " & strReplace
                                Elseif mode = "Real" Then
                                                aMFTemp.PNFolder = strReplace
                                                aMFTemp.SaveData()
                                End If   
                ElseIf objAPAC.Test(aMFTemp.PNFolder) = True Then
                                count = count + 1
                                strReplace = Replace(aMFTemp.PNFolder,"APAC\","")
                                If mode = "Debug" Then
                                                               
                                                Wscript.Echo aMFTemp.AppName & " - old: " & aMFTemp.PNFolder & " new: " & strReplace
                                Elseif mode = "Real" Then
                                                aMFTemp.PNFolder = strReplace
                                                aMFTemp.SaveData()
                                End If   
                ElseIf objECA.Test(aMFTemp.PNFolder) = True Then
                                count = count + 1
                                strReplace = Replace(aMFTemp.PNFolder,"ECA\","")
                                If mode = "Debug" Then
                                                Wscript.Echo aMFTemp.AppName & " - old: " & aMFTemp.PNFolder & " new: " & strReplace
                                Elseif mode = "Real" Then
                                                aMFTemp.PNFolder = strReplace
                                                aMFTemp.SaveData()
                                End If   
                End If
Set aMFTemp = Nothing
Set hMFAppTemp = Nothing
Next
Wscript.Echo "Total applications updated : " & count