Wednesday, December 9, 2015

Citrix Xenapp Publish Icons From Shortcuts

$SupportFolder = Test-Path 'C:\Temp\NonAppvCitrixPublish'
If (!$SupportFolder){New-Item C:\Temp\NonAppvCitrixPublish -type directory}
$LogFile = "C:\Temp\NonAppvCitrixPublish\NonAppvCitrixPublish.log"
$Count=0
$errorcheck=0
$Timestamp= Get-Date -Format 'dd_MM_yyyy_HH_mm'
$hostname = get-content env:computername
$user = get-content env:username
    Try {$credential = Get-Credential}
    Catch {NonAppv-Main-Menu}
#Display Main Menu
Function NonAppv-Main-Menu
{
    Write-Host ""
    Write-Host -ForegroundColor Green "Non APP-V OFF-LINE MANAGEMENT"
    Write-Host -ForegroundColor Green "( 1 ) Non AppV Citrix Publish"
    Write-Host -ForegroundColor Green "( 2 ) Exit"
    $Selection = read-host "Choose option to continue.."
    switch ($Selection)
        {
            1 {NonAppVCitrixPublish}
            2 {Exit}
        }
}
Function NonAppVCitrixPublish{
    "*********************************************************" | Out-File $LogFile -Append
    "Start Log: $Timestamp" | Out-File $LogFile -Append
    "Publish Non App-v package path for $HostName" | Out-File $LogFile -Append
    "Initated by User: $user" | Out-File $LogFile -Append 
    $header = "server"
    $Servers = Import-Csv servers.csv -header $header
    $Count = 1
    $a = @()
    $c=0
    ForEach ($i in $Servers) {
        $a=$a+$i
        $s = $a -replace '@{server=',''
        $s = $s -replace '}',''
        write-host -ForegroundColor Yellow "(" $Count ")" $s[$c]
        $Count++
        $c++
    }
    $Count = $Count -1
    $ServerNumber = read-host "Select Citrix server to use to retrive application details"
    If ($ServerNumber -eq 0) {NonAppv-Main-Menu}
    $ServerNumber=$ServerNumber-1
    If($Count -eq 1) {
        $ServerName = $a.Server
    }
    Else {
        $ServerName = $a[$ServerNumber].Server
    }
    Write-Host -ForegroundColor Yellow "Retrieving application shortcuts from server $ServerName, please wait..."
    $pss=Get-PSSession
    If($pss){
         Remove-PSSession -ComputerName $ServerName
    }
    $ps = New-PSSession -ComputerName $ServerName -Credential $credential -Authentication Kerberos
Invoke-Command -Session $ps {
    enable-wsmancredssp -role Server -Force | Out-Null
    }
    $ps = New-PSSession -ComputerName $ServerName -Credential $credential -Authentication Credssp
    $NonAppvshortcuts=Invoke-Command -Session $ps -ArgumentList $LogFile -scriptblock  {
       param ($LogFile)
       $SupportFolder = Test-Path 'C:\Temp\NonAppvCitrixPublish'
       If (!$SupportFolder){New-Item C:\Temp\NonAppvCitrixPublish -type directory}
       $Progpath = (Get-ChildItem env:ALLUSERSPROFILE).Value
       $ShortcutPath = "$Progpath\Microsoft\Windows\Start Menu"
       $ShortcutPathtest=Test-Path $ShortcutPath
       If ($ShortcutPathtest) {
          $ShortcutFolders =  @(Get-Item -Path $ShortcutPath )
          $ShortcutFolders = $ShortcutFolders + (gci $ShortcutPath -Recurse -Exclude *.*) 
       }
       $ShortcutFoldersCount = 1
       foreach ($ShortcutFolder in $ShortcutFolders){
          write-host -ForegroundColor Yellow "(" $ShortcutFoldersCount ")" $ShortcutFolder
          $ShortcutFoldersCount++
       }
       $ShortcutFolderNumber = Read-Host "Enter the folder number"
       $ShortcutFolderNumber = $ShortcutFolderNumber -1
       $ShortcutFolderSelect = $ShortcutFolders[$ShortcutFolderNumber]
       $NonAppvlocal= (gci $ShortcutFolderSelect  '*.lnk').FullName
       #Check if packages exist
       If($NonAppvlocal.count -eq 0){
          write-host -ForegroundColor Red "No Shortcuts Found"
          "No Shortcuts Found, ***Exiting Script**" | Out-File $LogFile -Append
          Exit
       }
       Else {
          write-host -ForegroundColor Green $NonAppvlocal.count "Shortcuts Found"
       }
       #List application choice to users
       $Count=1
       $NonAppvshortcuts = @()
       foreach ($NonAppv in $NonAppvlocal){
          #Check if shortcut is linked to App-V and only display them
          $obj = New-Object -ComObject WScript.Shell  
          $SCPath = $obj.CreateShortcut("$NonAppv").IconLocation
          $CheckAppV = $SCPath.Contains("AppV")
          If (!$CheckAppV) {
             $NonAppvshortcuts = $NonAppvshortcuts+$NonAppv
             $ShortLink = (Get-Item $NonAppv | Get-Unique).Name
             write-host -ForegroundColor Yellow "(" $Count ")" $ShortLink 
            "Retrieved Non AppV Shortcuts: ($Count) $NonAppv" | Out-File $LogFile -Append
            $Count++
          }
       }
       $Count=$Count-1
       Return $NonAppvshortcuts
    }
    Remove-PSSession $ps 
    If ($NonAppvshortcuts.Count -eq 0)  {
        write-host -ForegroundColor Yellow "No NonAppV Shortcuts" 
       NonAppv-Main-Menu
    }
    Try {
        $Ctxfarm = (Get-XAfarm).FarmName}
    Catch {
        write-host -ForegroundColor Red "Citrix Farm not found"
        "Citrix Farm not found" | Out-File $LogFile -Append
        NonAppv-Main-Menu
    }
    #List of Application folders for selection
    Write-Host -ForegroundColor Yellow "Retrieving application folders from $Ctxfarm farm..."
    $XAFolder = Get-XAFolder Applications -Recurse
    $PubFolderpaths =  $XAFolder.FolderPath | Sort-Object
    $Count=1
    foreach($PubFolderpath in $PubFolderpaths){
        $PubFolderpath = $PubFolderpath -replace "@{FolderPath=",""
        $PubFolderpath = $PubFolderpath -replace "}",""
        write-host -ForegroundColor Yellow "(" $Count ")" $PubFolderpath
        "Retrieved Application Path(s): ($Count) $PubFolderpath" | Out-File $LogFile -Append
        $Count++
    }
    $Count=$Count-1
    $PubFolderpathNum = read-host "$Count Folder Path(s) Found, select Citrix farm application folder path or 0 to Exit"
    "Folder Path Number $PubFolderpathNum selected by $user " | Out-File $LogFile -Append
    If ($PubFolderpathNum -eq 0) {NonAppv-Main-Menu}
    $PubFolderpathNum = $PubFolderpathNum - 1
    "$Count Folder Path(s) found " | Out-File $LogFile -Append
    $PubAppFolder = $PubFolderpaths[$PubFolderpathNum]
    $PubAppFolder = $PubAppFolder -replace "@{FolderPath=",""
    $PubAppFolder = $PubAppFolder -replace "}",""
    Write-host -ForegroundColor Green "$PubAppFolder selected"
    $PubUserGroup = read-host "Enter User Group"
    "$PubUserGroup user group entered by $user " | Out-File $LogFile -Append
    $PubShortcutCount = 1
    Foreach ($NonAppvshortcut in $NonAppvshortcuts){
       $NonAppvshortcutLength=$NonAppvshortcut.Length
       If($NonAppvshortcutLength -gt 145){
          Write-host -ForegroundColor Yellow "Path too long for Citrix Publish, retrieving short name format"
          $fso = New-Object -ComObject Scripting.FileSystemObject
          $NonAppvShortpath = $fso.GetFile($NonAppvshortcut).ShortPath
          $NonAppvshortcut=$NonAppvShortpath
       }
       Write-Host -ForegroundColor Yellow "Publishing the Shortcut(s) $PubShortcutCount, please wait..."
        $ps = New-PSSession -ComputerName $ServerName
        $PubDisplayName=Invoke-Command -Session $ps -ArgumentList $LogFile,$NonAppvshortcut -scriptblock  {
           param ($LogFile,$NonAppvshortcut)
           asnp citrix*
           $PubDisplayName = (get-item $NonAppvshortcut | Get-Unique).Name
           $PubDisplayName = $PubDisplayName -replace '.lnk',''
           $PubDisplayName = $PubDisplayName -replace '\.','-'
           "Application Name to publish $PubDisplayName "| Out-File $LogFile -Append 
           Return $PubDisplayName
        }
        $PubExePath =Invoke-Command -Session $ps -ArgumentList $LogFile,$NonAppvshortcut -scriptblock  {
            param ($LogFile,$NonAppvshortcut)
            $obj = New-Object -ComObject WScript.Shell
            $PubExePath = $obj.CreateShortcut("$NonAppvshortcut").targetpath
            Return $PubExePath 
        }
        $WorkingDirectory =Invoke-Command -Session $ps -ArgumentList $LogFile,$NonAppvshortcut -scriptblock  {
            param ($LogFile,$NonAppvshortcut)
            $obj = New-Object -ComObject WScript.Shell
            $WorkingDirectory = $obj.CreateShortcut("$NonAppvshortcut").workingDirectory
            Return $WorkingDirectory
        }
        #Get Icon information
        $CtxIcon=Invoke-Command -Session $ps -ArgumentList $LogFile,$NonAppvshortcut -scriptblock  {
            param ($LogFile,$NonAppvshortcut)
            $obj = New-Object -ComObject WScript.Shell  
            $PubIconPath = $obj.CreateShortcut("$NonAppvshortcut")
            $PubIcon = $PubIconPath.IconLocation
            $PubIcon = $PubIcon -replace ",0",""
            $CtxIcon = (Get-CtxIcon -FileName $PubIcon).EncodedIconData
            "Published Application Icon found $PubIcon " | Out-File $LogFile -Append
            Return $CtxIcon
        }
        Remove-PSSession $ps
        $ClientFolder = Split-Path $NonAppvshortcut
        $ClientFolder = $ServerName +"\" + [string]::Join('\', $ClientFolder.Split('\')[5..$($ClientFolder.Split('\').Length)])
        $PubDisplayName = $PubDisplayName + "_" + $ServerName.Substring(($ServerName.Length-4))
        "Publishing XA application " | Out-File $LogFile -Append
        "Display Name : $PubDisplayName " | Out-File $LogFile -Append
        "Executable Path : $PubExePath " | Out-File $LogFile -Append
        "Client Folder : $ClientFolder " | Out-File $LogFile -Append
        "Application Folder : $PubAppFolder " | Out-File $LogFile -Append 
        "WorkingDirectory : $WorkingDirectory " | Out-File $LogFile -Append
        "User Group : $PubUserGroup " | Out-File $LogFile -Append
        Try {
            New-XAApplication -ApplicationType ServerInstalled -DisplayName $PubDisplayName -CommandLineExecutable $PubExePath -FolderPath $PubAppFolder -WorkingDirectory $WorkingDirectory -Force  -EncodedIconData $CtxIcon -ServerNames $ServerName -Accounts $PubUserGroup -AddToClientStartMenu $false -ClientFolder $ClientFolder -WaitOnPrinterCreation $false 
        }
        Catch [System.exception] {
            Write-host -ForegroundColor Red "Error creating published application $PubDisplayName"
            "Error creating published application $PubDisplayName "| Out-File $LogFile -Append 
        }

$PubShortcutCount++
    }
}
cls
asnp citrix*
NonAppv-Main-Menu

No comments:

Post a Comment