Wednesday, September 3, 2008

Monitor Citrix Licenses

Was tasked with monitoring Citrix Licenses. We needed a way to gauge when demand was the highest and overall utilization. Following is a PowerShell script that runs every 15 minutes and stores the result in a SQL Express DB. Will be using PowerGadgets at a later point to present a graphical representation of the data.

First the function to grab the Citrix info:

Function Get-CitrixLicenses {
$Licenses
= Get-WmiObject `
-class 'Citrix_GT_License_Pool' `
-Namespace "ROOT\CitrixLicensing" `
-ComputerName $_ Select __Server, Count, InUseCount
$dt
= Get-Date -Format g
foreach ($License in $Licenses) {
$obj
= New-Object psObject
$obj
Add-Member NoteProperty Server $License.__Server
$obj
Add-Member NoteProperty Total $License.Count
$obj
Add-Member NoteProperty InUse $License.InUseCount
$obj
Add-Member NoteProperty Date $dt
Write
-Output $obj }
}


Second the DB insert function:

function Write-CitrixLicense {

BEGIN {
# Open the DB Connection
$conn
= New-Object System.Data.OleDb.OleDbConnection
$connstr
= "Your connection string"
$conn.connectionstring
= $connstr
$conn.open()

# create DB command
$cmd
= New-Object system.Data.OleDb.OleDbCommand
$cmd.connection
= $conn }

PROCESS {
# create the INSERT statement
using object properties
$now
= Get-Date -form g
$sql
= "INSERT INTO tblCitrixLicense (Server,Count,InUse,
SubmitDate) VALUES ("
$sql
+= "'" + $_.Server + "',"
$sql
+= "'" + $_.Total + "',"
$sql
+= "'" + $_.InUse + "',"
$sql
+= "'" + $now + "')"
$cmd.commandtext
= $sql
$cmd.executenonquery()
Out-Null }

END {$conn.close()}
}


Finally the call....

'Server1','Server2' %{Get-CitrixLicenses} Write-CitrixLicense

No comments: