Monday, February 9, 2009

Some basic MFCOM scripting

In the environment I support, we have production servers and BCP servers (Business Continuity Planning). The BCP servers are duplicates of the production servers, and both share SAN disk via an SRDF link. When, for example, we have a hardware problem with a production server, we failover the SRDF link, repoint the Citrix published applications to the BCP server and voila! Service restored. The problem is, in this environment, each server has about 8 published applications and changing the defined servers manually used to take 2-3 minutes each due to the slow nature of the old Citrix Management Console. 8x3 = up to 24 minutes just to repoint applications. Further complicate that with the fact that each server can have up to 3 hubs (24x3 = you get the idea). So, after doing this once or twice, I decided it was time to learn how to script using MFCOM, Citrix' COM interface to Xenapp. Below is a script I cobbled together quickly.

Each server serves one or more hub locations in our environment, and the hubs are consistently numbered with a four-digit number that begins with 0. So, the below script (which is very inefficient, I know) simply finds every application in the published application list that has that hub number in the title. Once it finds these apps, it looks at the server list from which the app is published, and if it's a production server, replaces the server with the BCP and vice versa. What used to take 16-24 minutes now takes 16-24 seconds.


Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(1)

strHubNumber = Wscript.Arguments.Item(0)

For Each objApp In objFarm.Applications
objApp.LoadData 1

If Instr(objApp.Appname,strHubNumber) Then
strAppDN = objApp.DistinguishedName
Wscript.Echo strAppDN

For each server in objApp.Servers
If Instr(server.ServerName, "MMSPC") Then
strNewServer = Replace(server.ServerName, "MMSPC", "MMSBC")
Wscript.Echo vbTab & "Replacing " & server.ServerName & " With " & strNewServer & vbCRLF

Set objAppSrvBind = CreateObject("MetaFrameCOM.MetaFrameAppSrvBinding")
objAppSrvBind.InitializeByName strNewServer,objApp.BrowserName
objApp.AddServer objAppSrvBind

ElseIf Instr(server.ServerName, "MMSBC") Then
strNewServer = Replace(server.ServerName, "MMSBC", "MMSPC")
Wscript.Echo vbTab & "Replacing " & server.ServerName & " With " & strNewServer & vbCRLF

Set objAppSrvBind = CreateObject("MetaFrameCOM.MetaFrameAppSrvBinding")
objAppSrvBind.InitializeByName strNewServer,objApp.BrowserName
objApp.AddServer objAppSrvBind
Else
Wscript.Echo "Server doesn't match naming convention"
End If

objApp.RemoveServer(server.ServerName)
objApp.SaveData

Next
End If
Next