Wednesday, December 9, 2015

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
}

No comments:

Post a Comment