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

Citrix Xenapp Rename Icon

Add-PSSnapin Citrix*
$PubFolderPath = Read-Host "Enter the path in appcenter (ex:\Applications\FX PrimeVerify\)"
$Applications = Get-XAApplication -FolderPath $PubFolderPath
foreach ($App in $Applications){
    $NewName = "NEW_Name"
   Rename-XAApplication -BrowserName $App.BrowserName -NewDisplayName $NewName
    }

Citrix Xenapp Rename Client Folder

Add-PSSnapin Citrix*
$PubFolderPath = Read-Host "Enter the path in appcenter (ex:\Applications\FX PrimeVerify\)"
$Applications = Get-XAApplication -FolderPath $PubFolderPath
foreach ($App in $Applications){
     $NewClientFolder = $App.ClientFolder.Replace("OLD_Name", "NEW_Name")
   Set-XAApplication -BrowserName $App.BrowserName -ClientFolder $NewClientFolder
    }

Citrix Xenapp Remove Users

Add-PSSnapin Citrix*
$Pathtest=Test-Path "users.csv"
If (!$Pathtest) 
    {
        write-host -ForegroundColor Red "users.csv file does not exit, create file with users (DOMAIN\USER)"
        Exit
    }
$UsersAdd = Import-Csv users.csv -header "Users"
$FolderListpathtest = Test-Path "FoldersList.txt"
If ($FolderListpathtest) { Remove-Item "FoldersList.txt" }
#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    
#$PubFolderpaths = get-xafolder Applications -Recurse | Select-Object FolderPath | Sort-Object
$Count=1
 foreach($PubFolderpath in $PubFolderpaths)
 {
    $PubFolderpath = $PubFolderpath -replace "@{FolderPath=",""
    $PubFolderpath = $PubFolderpath -replace "}",""
    write-host -ForegroundColor Yellow "(" $Count ")" $PubFolderpath 
    "($Count) $PubFolderpath" | Out-file "FoldersList.txt" -Append
    $Count++
 }
$Count=$Count-1
$PubFolderpathNum = read-host "$Count Folder Path(s) Found, select Citrix farm application folder path or 0 to Exit"
If ($PubFolderpathNum -eq 0) {Exit}
$PubFolderpathNum = $PubFolderpathNum - 1
$PubAppFolder = $PubFolderpaths[$PubFolderpathNum]
$PubAppFolder = $PubAppFolder -replace "@{FolderPath=",""
$PubAppFolder = $PubAppFolder -replace "}",""
Write-host -ForegroundColor Green "$PubAppFolder selected"
$PubAppFolderConfirm = Read-Host "ENTER YES or NO (Default = NO) "
if ($PubAppFolderConfirm -eq "YES")
{
    $application=Get-XAApplication -FolderPath $PubAppFolder | Sort-Object
    $XASubFolder = Get-XAFolder $PubAppFolder -Recurse
    $PubSubFolderpaths =  $XASubFolder.FolderPath | Sort-Object
    $Count=1
    foreach($app in $application)
    {
        write-host -ForegroundColor Yellow "(" $Count ")" $app.DisplayName
        $Count++
    }
    foreach($PubSubFolderpath in $PubSubFolderpaths)
    {
        $PubSubFolderpath = $PubSubFolderpath -replace "@{FolderPath=",""
        $PubSubFolderpath = $PubSubFolderpath -replace "}",""
        write-host -ForegroundColor Yellow "(" $Count ")" $PubSubFolderpath
        $Count++
    }
    $Count=$Count-1
    write-host "$Count App(s)/Folder(s) Found, select the " -NoNewline
    write-host "number" -ForegroundColor Yellow -NoNewline
    write-host " or " -NoNewline
    write-host "AA" -ForegroundColor Yellow -NoNewline
    write-host " (All Apps) or " -NoNewline
    write-host "All" -ForegroundColor Yellow -NoNewline
    write-host " (All apps and folders) or 0 to Exit: " -NoNewline
    $PubAppNameNum = Read-Host #"$Count App(s)/Folder(s) Found, select the number or AA (All Apps) or All (All apps and folders) or 0 to Exit"
    If ($PubAppNameNum -eq 0) {Exit}
    #If ($PubAppNameNum -eq "All")
    switch ($PubAppNameNum)
    {
        "AA" {$PubAppName = "All the App(s)"}
        "All" {$PubAppName = "All the App(s)/Folder(s)"}
        default
        {
            $PubAppNameNum = $PubAppNameNum  - 1
            $PubAppName = $application[$PubAppNameNum].DisplayName
        }
    }
    write-host -ForegroundColor Green $PubAppName
    $PubAppNameConfirm = Read-Host "ENTER YES or NO (Default = NO) "
    If ($PubAppNameConfirm -eq "YES")
    {
        If ($PubAppNameNum -eq "All")
        {
            #$displayname=$application|select $PubAppName
            foreach ($app in $application)
            {
                foreach ($i in $UsersAdd)
                {
                    $i = $i -replace "@{Users=",""
                    $i = $i -replace "}",""
                    Remove-XAApplicationAccount -BrowserName $app.browsername -Accounts $i |select accounts, BrowserName, Folderpath
                }
            }
            foreach($PubSubFolderpath in $PubSubFolderpaths)
            {
                $Subapplication=Get-XAApplication -FolderPath $PubSubFolderpath
                foreach ($subapp in $Subapplication)
                {
                    foreach ($i in $UsersAdd)
                    {
                        $i = $i -replace "@{Users=",""
                        $i = $i -replace "}",""
                        Remove-XAApplicationAccount -BrowserName $subapp.browsername -Accounts $i |select accounts, BrowserName, Folderpath
                    }
                }
            }
        }
        elseIf ($PubAppNameNum -eq "AA")
            {
                foreach ($app in $application)
                {
                    foreach ($i in $UsersAdd)
                    {
                        $i = $i -replace "@{Users=",""
                        $i = $i -replace "}",""
                        Remove-XAApplicationAccount -BrowserName $app.browsername -Accounts $i |select accounts, BrowserName, Folderpath
                    }
                }
            }
        else
        {
            foreach ($i in $UsersAdd)
            {
                $i = $i -replace "@{Users=",""
                $i = $i -replace "}",""
                Remove-XAApplicationAccount -BrowserName $application[$PubAppNameNum].browsername -Accounts $i |select accounts, BrowserName, Folderpath
            }
        }
    }
    else
    {
        write-host -ForegroundColor Red "User selected NO"
        Exit
    }        
}
else
{
    write-host -ForegroundColor Red "User selected NO"
    Exit
}