In this series of articles I focus on how an you can use scripting in a Terminal Server / Citrix MetaFrame environment for common and recurring tasks. This article goes into the details of a set of scripts created to take a server offline by removing it from all published applications and then bringing it back online by adding it back into the server list for its published applications.
Since this is the first article in this series, I’ll start by stating some perquisites. It is assumed that the reader has some knowledge of basic scripting. While you don’t have to be a VBS/WSH expert to write these scripts it does help to have some basic VBS skills. Future articles will also deal with CMD and KIX scripting but this one utilizes VBS. Now, onto the fun!
One of the most common questions I hear about MetaFrame scripting is how to script common tasks normally done within the CMC. Administrators often want to script tasks they do almost every day or write a simple script to ensure that new applications get the same default settings each time, etc.
This article looks at a basic MFCOM script that dumps a list of published applications to a text file and removes a server from all its published applications. (In other words, it takes a server offline.) Then, a second script reads from that text file and re-publishes all the applicationss to that server (putting the server back online). These scripts (called “svrOnline” and “svrOffline”) were originally written by Ronn Martin at Citrix as samples for Doug Brown. In this article, we’ll go over the basics of these scripts and show you how RapidApp has added some basic logic to them for use during large numbers of automated reboots.
The first thing you should do before you write any script is to see if anyone has written something like it before. With scripting there is no need to re-invent the wheel. Save yourself some time by perusing sites like thethin.net and Citrix.com/cdn to see if there are scripts you can re-use or modify to fit your needs. With that in mind, let’s take a look at the first script we hacked: srvOffline.VBS.
SrvOffline.VBS uses the MFCOM object to determine which applications a server is publishing. It then enumerates these application names and writes them to a text file for later use. Basically, you run the script using the following command line and get a text file with the name of the server and all of its apps:
Cscript svroffline.vbs SERVERNAME C:\path
This script also removes all the published application assignments from the server, so after it’s run you can do your maintenance by allowing users to log off gracefully and then taking control of the machine. When you’re done, you run the second script to put the server back in the list:
Cscript svrOnline.vbs SERVERNAME C:\Path
Let’s take a look at the whole script before breaking it down and modifying it to fit our needs:
*********************************************************************
'
'svrOffline.vbs
'
'Description: Take a MF XP server "offline" by unpublishing all applications
' published from the server. The published app names are stored
' to a text file in a path specified in the command line so that
' the server can be brought back 'online" by the svrOnline script.
'
'Usage: Two required command line parameters - 1) MF XP server name (case
' sensitive 2) file path (with trailing "\" character)
'
' e.g. >cscript svrOffline.vbs MFSVR1 C:\WINNT\TEMP\
'
'********************************************************************
Dim fso
Dim fp
Dim svrName
Dim savePath
Dim mfSvr
Dim mfApp
Dim objArgs
Set objArgs = WScript.Arguments
If objArgs.Count > 1 Then
svrName = objArgs(0)
savePath = objArgs(1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set fp = fso.CreateTextFile(savePath & svrName & ".txt", True, False)
Set mfSvr = CreateObject("MetaFrameCOM.MetaFrameServer")
mfSvr.Initialize 6, svrName
For Each mfApp In mfSvr.Applications
mfApp.RemoveServer svrName
mfApp.SaveData
fp.WriteLine mfApp.DistinguishedName
Next
fp.Close
Else
MsgBox "Usage: svrOffline.vbs "
End If
You’ll notice right off the bat that after declaring the variables the author checks for possible command line script arguments with these two lines:
Set objArgs = WScript.Arguments
If objArgs.Count > 1 Then
Notice that Ronn checks to see if the number of arguments is greater than one. If not he jumps to the end of the script and pops up a message box that shows the proper syntax. It’s important to notice this since your modification may have more arguments and you may want to set this to a fixed number.
The next section of the script (if the objArgs.count is greater than 1) is the real meat of the script. Let’s look at the next four lines in the script:
svrName = objArgs(0)
savePath = objArgs(1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set fp = fso.CreateTextFile(savePath & svrName & ".txt", True, False)
The first two lines set the svrName and savePath variables to the arguments passed from the command line. “svrName” is the server name you are taking offline and “savePath” is where the you want the text file with the application names written to. The next two lines then setup the script for logging the published application names. This is done by accessing the File System Object which is then used to create the text file with a name and path based on the two arguments passed into the script.
The next two lines in the script are the first ones to actually touch any MetaFrame information. Here we begin to interact with the MFCOM object and locate the server name that was passed into the script as an argument.
Set mfSvr = CreateObject("MetaFrameCOM.MetaFrameServer")
mfSvr.Initialize 6, svrName
With the server object located, the script then parses the server object for all applications that it is hosting. You can see the For Each loop that looks for the application then for each application it finds it does three things:
- Removes the server from the list of servers hosting that application.
- Saves that changes.
- Writes the Distinguished Name of the Published application to the text file we are creating.
Once all the apps have been removed from the server the script then closes the log file and exits
For Each mfApp In mfSvr.Applications
mfApp.RemoveServer svrName
mfApp.SaveData
fp.WriteLine mfApp.DistinguishedName
Next
fp.Close
Else
MsgBox "Usage: svrOffline.vbs "
End If
As you can see, this is a pretty slick little script. As you can imagine the svrOnline.vbs does the exact opposite of this script. It reads the text file and re-adds the specified server into the list of servers for each published application in the file.
“Quick and to the point” describes this script pretty well. Does it fit your needs? I have no idea. But I can show you some of the modifications I made using this script as my base. Here’s what part of the script looked like once we finished:
Set fp = fso.CreateTextFile(savePath & svrName & ".txt", True, False)
If fso.FileExists(LogPath & svrName & "-Apps.log") Then
Set fpl = fso.OpenTextFile(LogPath & svrName & "-Apps.log", 8)
Else
Set fpl = fso.CreateTextFile(LogPath & svrName & "-Apps.log", True, False)
End If
fpl.WriteLine "On " & Date & " at " & Time & " the server " & svrName & " was removed from the following applications: "
For Each anApp In theServer.Applications
anApp.LoadData(TRUE)
'DisplayMessage = WshShell.Popup("DistinguishedName: " & anApp.DistinguishedName, 2, "")
anApp.RemoveServer svrName
anApp.SaveData
fp.WriteLine anApp.DistinguishedName
fpl.WriteLine anApp.DistinguishedName
Next
fpl.WriteLine "The end of the applications list on the server " & svrName
fpl.WriteLine " "
fp.Close
fpl.Close
One thing you’ll notice was that the script had no logic to determine if that file was already there. If the offline script was accidentally run more than once, the second time it ran it would see no published applications attached to the server and create a blank file. To mitigate this we added some logic to see if the file existed. If it did we opened the file and append to it instead of creating a new one. In addition, when we write the application names to the file we start with a date and time they were removed for later identification.
This little bit of logic ensured that we wouldn’t have problems running the scripts unattended. Our concern was that the offline script might run (on a schedule) then the online script might not run if there was a failure somewhere. (This was also on a schedule). If this happened we would have no idea what applications were on the server if the offline ran again at its next scheduled interval. A simple bit of modification to the script handled our problems.
Output from this script is a simple text file with a list of apps. The cool thing is that you can tie this into your reboot scripts to allow the server to remain offline while you do maintenance, or, in our case, to allow it to remain offline for 15-20 minutes while the server was up but packages were installed.
How can you use this script? It depends on your environment. You can use the svrOnline and svrOffline as they sit (full scripts shown below) or modify them to act as part of your reboots. Our modified versions were tied into the reboot schedule and ran before the server rebooted then a little while after it came back up. It’s up to you to see where they fit. Happy Scripting!
Look for the next article in the series in which we will create a basic KIX login script for MetaFrame users.
'***********************************************************************
'
'svrOffline.vbs
'
'Description: Take a MF XP server "offline" by unpublishing all applications
' published from the server. The published app names are stored
' to a text file in a path specified in the command line so that
' the server can be brought back 'online" by the svrOnline script.
'
'Usage: Two required command line parameters - 1) MF XP server name (case
' sensitive 2) file path (with trailing "\" character)
'
' e.g. >cscript svrOffline.vbs MFXPTS05 C:\WINNT\TEMP\
'
'***********************************************************************
Dim fso
Dim fp
Dim svrName
Dim savePath
Dim mfSvr
Dim mfApp
Dim objArgs
Set objArgs = WScript.Arguments
If objArgs.Count > 1 Then
svrName = objArgs(0)
savePath = objArgs(1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set fp = fso.CreateTextFile(savePath & svrName & ".txt", True, False)
Set mfSvr = CreateObject("MetaFrameCOM.MetaFrameServer")
mfSvr.Initialize 6, svrName
For Each mfApp In mfSvr.Applications
mfApp.RemoveServer svrName
mfApp.SaveData
fp.WriteLine mfApp.DistinguishedName
Next
fp.Close
Else
MsgBox "Usage: svrOffline.vbs "
End If
'***********************************************************************
'
'svrOnline.vbs
'
'Description: Bring a MF XP server "online" by re-publishing applications
' published from the server. The published app names are stored
' to a text file in a path specified in the command line that is
' the output of the svrOffline script.
'
'Usage: Two required command line parameters - 1) MF XP server name (case
' sensitive 2) file path (with trailing "\" character)
'
' e.g. >cscript svrOnline.vbs MFXPTS05 C:\WINNT\TEMP\
'
'***********************************************************************
Dim fso
Dim fp
Dim svrName
Dim savePath
Dim mfSvr
Dim mfApp
Dim appName
Dim mfAppBinding
Set objArgs = WScript.Arguments
If objArgs.Count > 1 Then
svrName = objArgs(0)
savePath = objArgs(1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set fp = fso.OpenTextFile(savePath & svrName & ".txt", 1, vbFalse)
Set mfSvr = CreateObject("MetaFrameCOM.MetaFrameServer")
mfSvr.Initialize 6, svrName
While Not fp.AtEndOfStream
Set mfApp = CreateObject("MetaFrameCOM.MetaFrameApplication")
appName = fp.ReadLine
mfApp.Initialize 3, appName
mfApp.LoadData True
Set mfAppBinding = CreateObject("MetaFrameCOM.MetaFrameAppSrvBinding")
mfAppBinding.Initialize 6, svrName, appName
mfApp.AddServer mfAppBinding
mfApp.SaveData
Wend
fp.Close
Else
MsgBox "Usage: svrOnline.vbs "
End If