Writing a Script to take a MetaFrame Server Online or Offline

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.

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:

  1. Removes the server from the list of servers hosting that application.
  2. Saves that changes.
  3. 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

Join the conversation

12 comments

Send me notifications when other members comment.

Please create a username to comment.

This message was originally posted by Davin Laine on May 27, 2004
This script looks great, but could the same thing be accomplished, that is no one logging on, thru load balancing? Can you script load balancing to tweak the 'overall' to max, thus preventing any logons?
Cancel
This message was originally posted by Kevin Phillips on May 27, 2004
Ron sent me a smiliar script 6 months ago. Since we have to stay on FR2 due to an issue with an internal app w/ FR3, Apputil.exe was not an option. I modified it and used winbatch to create a simple user interface, and now it is much easier to unpublish/publish apps from 50 servers at a time.
Cancel
This message was originally posted by Ron Oglesby on May 27, 2004
Well it all depends right? Brian had an article on this site before about using an offline evaluator for taking a server offline. Thats great for GUI guys or jsut doing things through the CMC. This script (and others like it) was done for guys that script everything (Ie: why I am writing a Scripting for MetaFrame series). Anyway when doing automated tasks on schedules or bringing a new server on line little scripts like this make things simpler and full proof.(as full proff as anything gets). Generally this is tied into a scheduled reboot or scheduled maintenance cycle when you have large farms. Do you ahve to use it? No. But a simple 10-12 line script with one command line works like a champ.
Cancel
This message was originally posted by plsntn_rules@yahoo.com on September 3, 2004
U mentioned in ur write up above that you modified the script to include some logic...is it possible to see the final version of the scripts? I am a newbie at VBS but am slowly but surely learning...I would like to see
Cancel
Really useful script - thanks for providing this. It has saved a lot of time and hassle recently. I do however on occasion get an "unspecified error" when attempting to use the -removeapps switch. It is listed as line 139,1. I have tried to debug it but have had no success yet. It only happens on a couple of servers in the farm, the same servers can have their apps listed and backed up just not removed. Thought it was DCOMCNFG security related but have checked settings against a working server and they are the same. Anyone else experienced this ?
Cancel
Is there anyway to include Load Evaluators?
Cancel
I also get " Microsoft VBScript runtime error: Invalid procedure call or argument" on some of our servers...don't know why - all on same OS, SP, MPS level...
Anyone who can help???
Cancel
What about stopping the XML or the IMA services (or both) ?
(using Net stop command line )
Cancel
ORIGINAL: Guest

What about stopping the XML or the IMA services (or both) ?
(using Net stop command line )
Cancel
I am running the basic script and it works great on two of my servers and fails on the other two. All servers are imaged and identical. The only difference is the applications currently published. I get <40,7> runtime error:Invalid procedure call or argument. Any ideas. I am going to cycle through the applications and see if one of them is causing the issue.
Cancel

I have that same problem... But it looks like the servers where it is going wrong the applications are wrongly published.
In other words: I think the name of the application on the server is different from the one where metaframe is installed.

I haven't thoroughly tested this, so don't flame me when it's not correct.. 

Cancel

Hi


I am also getting the same problem...


Has any one got the solution for this..


Cancel

-ADS BY GOOGLE

SearchVirtualDesktop

SearchEnterpriseDesktop

SearchServerVirtualization

SearchVMware

Close