Citrix functions Updated!
I didn’t have time last post to additional “Farm” functionality to some the functions. The following are updated to work against multiple farms.
Updated Server functions
Updated Application functions
Citrix Server Functions
#### Citrix Server Functions ####
##########################################
## Publish Application to Server(s)
## -app: Name of Application to remove. This is required
## -Server: Name of Server
## PIPED: It will take Servers via Pipe. It expects a list or Citrix Server Object
## NOTE: App Name must include subfolders of the app.
## If the app in in Applications\Test then app would be Test\MyApp
## Example: Publish-CitrixApplication -server myserver -app Test\MyApp
function Publish-CitrixApplication{
Param([string]$server,[string]$app)
Begin{
Write-Host
function gcs{
Param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeServer",$Server)
$mfServer = [system.Activator]::CreateInstance($type)
$mfServer.Initialize(6,$Server)
$mfServer
}
function gca{
Param($srv,$ma)
$type = [System.Type]::GetTypeFromProgID("MetaFrameCOM.MetaFrameApplication",$Server)
$mfApp = [system.Activator]::CreateInstance($type)
$mfApp.Initialize(3,"Applications\$ma")
$mfApp.LoadData($true)
$mfApp
}
function cPublish {
Param([string]$Srv,[string]$myapp)
$Srv = $Srv.toUpper()
$mfSrv = gcs $srv
$mfApp = gca $srv $myapp
$mfAppBinding = New-Object -ComObject MetaFrameCOM.MetaFrameAppSrvBinding
$mfAppBinding.Initialize(6,$Srv,"Applications\$app")
if($mfAppBinding)
{
Write-Host "Publishing App[$myapp] on Server [$Srv]" -ForegroundColor Green
$mfApp.AddServer($mfAppBinding)
$mfApp.SaveData()
}
else
{
Write-Host "Unable To Create App Binding" -ForegroundColor Red
}
}
$process = @()
}
Process{
if($_){
if($_.ServerName){
$process += $_.ServerName
}
else{
$process += $_
}
}
}
End{
if($Server){$Process += $Server}
foreach($s in $process){
cPublish -srv $s -myapp $app
Write-Host
}
}
}
## UnPublishes ALL Application from Server(s)
## -Server: Name of Server
## PIPED: It will take Servers via Pipe. It expects a list or Citrix Server Object
function UnPublish-CitrixServer{
Param([string]$server)
Begin{
function gcs{
Param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeServer",$Server)
$mfServer = [system.Activator]::CreateInstance($type)
$mfServer.Initialize(6,$Server)
$mfServer
}
function cUnPublish {
Param([string]$Srv)
$Srv = $Srv.toUpper()
$mfSrv = gcs $srv
If($mfSrv.Applications.Count -gt 0)
{
Write-Host "Removing All Published Applications from $Srv" -ForegroundColor Red
Write-Host "===================================================" -ForegroundColor Green
ForEach($a in $mfSrv.Applications)
{
$myApp = $a.AppName
Write-Host "Removing App [$myApp] from Server [$Srv]" -ForegroundColor White
$a.RemoveServer($Srv)
$a.SaveData()
}
}
else
{
Write-Host "No Published Applications for $Srv" -ForegroundColor Red
}
}
Write-Host
$process = @()
}
Process{
if($_){
if($_.ServerName)
{
$process += $_.ServerName
}
else
{
$process += $_
}
}
}
End{
if($Server){$Process += $Server}
foreach($s in $process){
cUnPublish $s
Write-Host
}
}
}
## Remove Specific Application from Server(s)
## -app: Name of Application to remove. This is required
## -Server: Name Server.
## PIPED: It will take Servers via Pipe. It expects a list or Citrix Server Object
function Remove-CitrixApplication {
Param([string]$server,[string]$app)
Begin{
function gcs{
Param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeServer",$Server)
$mfServer = [system.Activator]::CreateInstance($type)
$mfServer.Initialize(6,$Server)
$mfServer
}
function RemoveApp {
Param([string]$Srv,[string]$myapp)
$AppRemoved = $false
$Srv = $Srv.toUpper()
$mfSrv = gcs $srv
If($mfSrv.Applications.Count -gt 0)
{
ForEach($a in $mfSrv.Applications)
{
If(($a.AppName -eq "$myapp") -or ($a.BrowserName -eq "$myapp"))
{
Write-Host "Removing App [$myApp] from Server [$Srv]" -ForegroundColor Green
$a.RemoveServer($Srv)
$a.SaveData()
$AppRemoved = $true
}
}
}
else
{
Write-Host "No Applications Published for $Srv" -ForegroundColor Red
$AppRemoved = $true
}
If($AppRemoved -eq $false)
{
Write-Host "This Application not Published for $Srv" -ForegroundColor Red
}
}
Write-Host
$process = @()
}
Process{
if($_)
{
if($_.ServerName){
$process += $_.ServerName
}
else
{
$process += $_
}
}
}
End{
if($Server){$Process += $Server}
if($process.Length -eq 0){$Process += get-content env:COMPUTERNAME}
foreach($s in $process)
{
RemoveApp -Srv $s -myapp $app
Write-Host
}
}
}
Citrix App Functions
#### Citrix App Functions ####
#########################################
## Returns Citrix Application for Farm
## -Server: Name of Farm Server. This is required
## -App: Name of Application to remove. This is required
function Get-CitrixApp{
Param($Server =$(throw ‘$Server is Required’),$App= $(throw ‘$FarmServer is Required’))
$type = [System.Type]::GetTypeFromProgID("MetaFrameCOM.MetaFrameApplication",$Server)
$mfApp = [system.Activator]::CreateInstance($type)
$mfApp.Initialize(3,"Applications\$myapp")
$mfApp.LoadData($true)
$mfApp
}
## Returns Users currently using APP
## -app: Name of Application. This is required
## -Server: Name of Farm Server. Defaults to local if not passed
## -count: Switch… if set just returns count of servers
function Get-CitrixAppUsers {
Param($app = $(throw ‘$app is required’),$server,[switch]$count)
function gcf{
param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$Server)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize(1)
return $mFarm
}
$ErrorActionPreference = "SilentlyContinue"
Write-host
if($server){$mfm = gcf $server}
else{$mfm = New-Object -com MetaFrameCOM.MetaFrameFarm;$mfm.Initialize(1)}
$users = $mfm.Applications | ?{($_.AppName -eq $app) -or ($_.BrowserName -eq $app)}
$Users = $users.Sessions | sort -Property UserName
if($count){
Write-Host "Found [$($Users.Count)] Users for Application [$app]" -ForegroundColor White
Write-Host
}
else{
Write-Host ""
Write-Host "Found [$($Users.Count)] Users for Application [$app]" -ForegroundColor White
Write-Host "—————————————————–" -ForegroundColor gray
foreach($user in $Users){
If($User.SessionState -eq 1){
Write-Host ($User.UserName).PadRight(10) -ForegroundColor Green -NoNewline
}
else{
Write-Host ($User.UserName).PadRight(10) -ForegroundColor yellow -NoNewline
}
}
Write-Host
Write-Host "—————————————————–" -ForegroundColor gray
Write-Host "Found [$($Users.Count)] Users for Application [$app]" -ForegroundColor White
Write-Host
}
}
## Returns Servers currently published APP
## -app: Name of Application. This is required
## -Server: Name of Farm Server. Defaults to local if not passed
## -count: Switch… if set just returns count of servers
function Get-CitrixAppServers {
Param($app = $(throw ‘$app is required’),$Server,[switch]$count)
function gcf{
param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$Server)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize(1)
return $mFarm
}
if($server){$mfm = gcf $server}
else{$mfm = New-Object -com MetaFrameCOM.MetaFrameFarm;$mfm.Initialize(1)}
$Apps = $mfm.Applications | ?{($_.AppName -eq $app) -or ($_.BrowserName -eq $app)}
# for XP farms
$servers = $apps | %{$_.Servers} | sort -Property ServerName | Select-Object ServerName
if(!$servers){
# for 40/45 farms
$servers = $Apps.Sessions | Select-Object ServerName | Sort-Object -unique ServerName
}
if($count)
{
Write-Host
Write-Host "Found [$($Servers.Count)] Servers for Application [$app]" -ForegroundColor White
Write-Host
}
else
{
Write-Host ""
Write-Host "Found [$($Servers.Count)] Servers for Application [$app]" -ForegroundColor White
Write-Host "———————————————–" -ForegroundColor gray
foreach($server in $servers){Write-Host "$($server.ServerName)" -ForegroundColor Green}
Write-Host "———————————————–" -ForegroundColor gray
Write-Host "Found [$($Servers.Count)] Servers for Application [$app]" -ForegroundColor White
Write-Host ""
}
}
## Returns Server(s) user is logged into via Citrix
## -LoginName: Login Name of user (Domain\User). This is Required
## -Server: Name of Farm Server. Defaults to local if not passed
## -Verbose: Details about User
function Find-CitrixUser {
Param([string]$LoginName,$Server,[switch]$verbose)
$user = $LoginName.Split("\")[1]
$Domain = $LoginName.Split("\")[0]
if($server)
{
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeUser",$Server)
$mfuser = [system.Activator]::CreateInstance($type)
$mfuser.Initialize(1,$Domain,1,$user)
}
else
{
$mfuser = New-Object -ComObject MetaframeCOM.MetaframeUser
$mfuser.Initialize(1,$Domain,1,$user)
}
Write-Host
Write-Host "User: $($mfuser.UserName) found on the Following:"
foreach ($s in $mfuser.Sessions)
{
if($verbose)
{
Write-Host
Write-Host "$($s.ServerName)"
Write-Host "-=-=-=-=-=-"
Write-Host "AppName : $($s.AppName)" -foregroundcolor yellow
Write-Host "SessionName : $($s.SessionName)" -foregroundcolor yellow
Write-Host "SessionID : $($s.SessionID)" -foregroundcolor yellow
Write-Host "ClientAddress : $($s.ClientAddress)" -foregroundcolor yellow
Write-Host "ClientEncryption : $($s.ClientEncryption)" -foregroundcolor yellow
Write-Host
Write-Host "Processes"
Write-Host "========="
foreach ($proc in $s.Processes)
{
Write-Host $proc.ProcessName -foregroundcolor Green
}
Write-host
}
else
{
write-Host " -> $($s.ServerName)"
}
}
}
tshell :: Oct.01.2007 :: Citrix, HowTo, Powershell, Scripting, functions :: No Comments »

Brandon, please can you help with the correct syntax for piping a list of servers into the “UnPublish-CitrixServer” function? I can get it to work against a single server (which, incidentally, works absolutely brilliantly!!) but I am struggling to get my head around how to run the function against multiple servers. Thank you.