It has been a while since I dove into the MFCom pool. Most of the Citrix functions I wrote where at the very beggining of my Powershell Adventure. I decided to go through and rewrite some of them and post the others.
So… here you go.
I broke these down into three sections
- Citrix Farm Functions
- Citrix App Functions
- Citrix Server Functions
If you get a chance to use these let me know if you run into any troubles… I did test them thoroughly.. but you never know.
Other than Publish/Unpublish/Remove they shouldn’t change anything.
UPDATED: I added information about functions before each section. And a list at the bottom!
Farm functions
————–
# Returns Farm Object.
# -Server: Any PS should do.. I recommend ZDC though) !!REQ!!
Get-CitrixFarm
# Returns a collection of Server Objects
# -zone= This will be the zone name (normally the subnet i.e. 10.1.1.0) !!REQ!!
Get-CitrixOnline
# Returns Load Evaluators Information
# -Server: Any PS should do.. I recommend ZDC though) !!REQ!!
Get-CitrixLE
# Returns Printer Drivers install in Farm
# -Server: Any PS should do.. I recommend ZDC though) !!REQ!!
# This can take a REAL long time
Get-CitrixPrintDrivers
# Returns Polices in the Farm
# -Server !!REQ!! (any PS should do.. I recommend ZDC though)
Get-CitrixPolicies
#########################################
#### Citrix Farm Functions ####
#########################################
# Get Citrix Farm
function Get-CitrixFarm{
param($Server)
$type =
[System.
Type]::
GetTypeFromProgID("MetaframeCOM.MetaframeFarm",
$Server)
$mfarm =
[system.
Activator]::
CreateInstance($type)
$mfarm.
Initialize(1)
return $mFarm
}
# Get Online Servers by Zone
function Get-CitrixOnline {
Param($zone)
$mfzone = New-Object -ComObject MetaFrameCOM.MetaFrameZone
$mfzone.Initialize($zone)
$servers = $mfzone.OnlineServers
$servers
}
# Get Citrix Load Evaluators (only 4.0/4.5)
function Get-CitrixLE{
Param($server=$(throw "Server is Required"))
function Load-Farm{
param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$srv)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize(1)
return $mFarm
}
$Farm = load-farm $server
if($Farm.LoadEvaluators){
foreach($eval in $Farm.LoadEvaluators)
{
$eval.loadData(1)
"+ Load Evaluator: {0}" -f $eval.LEName
$servers = $eval.AttachedServers(1)
if($servers.count -ne 0)
{
" + Servers"
$servers | %{" - {0}" -f $_.ServerName}
}
$rules = $eval.rules | Select-Object RuleType,HWM,LWM,Schedules
if($rules.count -ne 0)
{
" + Rules"
foreach($rule in $rules)
{
" - {0}" -f $rule
}
}
}
}
}
# Gets the Citrix Printer Drivers for the Farm (Can be REAL slow)
function Get-CitrixPrintDrivers{
Param($server=$(throw "Server is Required"))
function Load-Farm{
param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$srv)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize(1)
return $mFarm
}
$farm = Load-Farm $Server
$farm.Drivers
}
# Gets Citrix Policies
function Get-CitrixPolicies{
param($Server)
function Load-Farm{
param($srv)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$srv)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize(1)
return $mFarm
}
$farm = Load-Farm $server
$type = [System.Type]::GetTypeFromProgID("MetaFrameCOM.MetaFrameUserPolicy")
foreach($pol in $Farm.policies($type))
{
$pol.loadData(1)
"+ Name: {0}" -f $pol.Name
" - Description: {0}" -f $pol.Description
" - Enabled: {0}" -f $pol.Enabled
if($pol.AllowedAccounts)
{
" + AllowedAccounts"
foreach($aa in $pol.AllowedAccounts)
{
" - {0}" -f $aa.AccountName
}
}
if($pol.UserPolicy2)
{
" + UserPolicy"
$props = $pol.UserPolicy2 | Get-Member -membertype Property | %{$_.Name} | Sort-Object Name
foreach($prop in $props)
{
if(($pol.UserPolicy2.$prop -match "\d") -and ($pol.UserPolicy2.$prop -ne 0))
{
" - {0}:{1}" -f $prop,$pol.UserPolicy2.$prop
}
}
}
write-Output " "
}
}
App functions
————–
# Returns User Count for an App or All Apps
# -FarmServer !!REQ!! (any PS should do.. I recommend ZDC though)
# -app: Name of Application
# This loops CTRL+C to break
Get-ApplicationUserCount
# Returns Server(s) user is logged into via Citrix
# -LoginName: Login Name of user (Domain\User) !!REQ!!
# -Verbose: Details about User
Find-CitrixUser
# Returns Servers published for Application or just Count
# -app: Name of Application !!REQ!!
# -count: Switch… if set just returns count of servers
Get-CitrixServers
# Returns Users currently using APP
# -app: !!REQ!!
# -count: Switch… if set just returns count of servers
Get-CitrixAppUsers
#########################################
#### Citrix App Functions ####
#########################################
# Outputs the number of Users using a Citrix App or Apps
function Get-ApplicationUserCount {
Param([string]$app,
[string]$farmServer = $
(throw
‘$FarmServer is Required’))
function List-AllCitrixApps
{
Param($mFarm)
ForEach($app in $mFarm.
Applications)
{
$name =
$app.
BrowserName.
PadRight(25)
$count =
"$($app.Sessions.Count)"
$count =
$count.
PadRight(10)
Write-Host "$name $count"
}
}
function List-App
{
param($mApp,
$mfFarm)
ForEach($app in $mfFarm.
Applications)
{
if($app.
BrowserName -eq "$mApp")
{
$name =
$app.
BrowserName.
PadRight(25)
$count =
"$(($app.Sessions | ?{$_.SessionState -eq 1}).Count)"
$count =
$count.
PadRight(10)
Write-Host "$name $count"
}
}
}
function Load-Farm
{
$type =
[System.
Type]::
GetTypeFromProgID("MetaframeCOM.MetaframeFarm",
$srv)
$mfarm =
[system.
Activator]::
CreateInstance($type)
$mfarm.
Initialize(1)
return $mFarm
}
Write-Host
$title1 =
"Application".
PadRight(25)
$title2 =
"===========".
PadRight(25)
Write-Host "$title1 User Count" -ForegroundColor White
Write-Host "$title2 ==========" -ForegroundColor Red
$mf = Load-Farm
$farmServer
While($true)
{
$oldpos =
$host.
UI.
RawUI.
CursorPosition
If($app)
{
List-App
$app $mf
}
else
{
List-AllCitrixApps
$mf
}
sleep
(5)
$host.
UI.
RawUI.
CursorPosition =
$oldpos
}
Write-Host ""
}
# Finds what Server a User is on
function Find-CitrixUser {
Param([string]$LoginName,[switch]$verbose)
$user = $LoginName.Split("\")[1]
$Domain = $LoginName.Split("\")[0]
$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