Post from May, 2008

New Citrix MFCOM Scripting Guide

Wednesday, 28. May 2008 14:03

If you need to automate Citrix XenApp (formerly known as Presentation Server) download now the updated Ultimate Guide to MFCOM SDK by “Dr. SDK”

According Citrix Blogger Vishal Ganeriwala this is the only guide you will ever need to understand and learn about MFCOM SDK. It covers everything up to SDK version 4.5. It covers all the advance MFCOM topics such as multifarm management, publishing different type of Applications, policies, load evaluators and more.

All sample code in the new MFCOM guide is still written in VBScript.

According Dr. SDK Citrix is considering to develop a PowerShell-based interface to replace MFCOM entirely. Part of the reason for such a consideration is that most of the farm and server properties will be moved Active Directory. That will result in a large portion of MFCOM properties being invalid.

If you’re interested in scripting MFCOM with PowerShell see also: PowerShell and Citrix MFCOM recorded Webinar and Powerpoint slide deck.

Category:Scripting, VBScript | Comment (0) | Author: Frank-Peter

How To Script Downloads With BITS?

Tuesday, 27. May 2008 20:30

When it comes to large file downloads one should use a download manager which at least is able to resume broken downloads. Windows has a built-in download manager service called Background Intelligent Transfer Service(BITS). Some of us at Login Consultants (including me) prefer to script large file downloads with the Microsoft’s BITS Admin Utility – Bitsadmin. This article explains how to use Bitsadmin to download a file.

The BITS service is built into Windows XP, Vista, Server 2003, and Server 2008. For example the Windows Automatic Update, WSUS 3.0, or Microsoft SCCM 2007 depend on BITS to transfer files like Windows Updates and software packages. For one thing BITS has been designed for robustness over slow or unstable network connections, and for another thing BITS is a background process that economizes on bandwidth usage. If a download does not complete due to a network problem or restart of your machine, BITS will automatically try to continue the download from where it left off, and repeat this until the whole file has been retrieved. BITS file transfers can be invoked and managed from the command line with the Bitsadmin utility which is part of the Windows Support Tools.

Please note that this article applies to Bitsadmin 3.0 and the version of BITS that is included in Windows Vista SP1.

In order to download a file with Bitsadmin, respectively BITS, you need to type a series of Bitsadmin commands to create a download job, to add the file the the job, eventually to set credentials and proxy settings, and finally to start the download job. Bitsadmin has many command line switches, for this see also Bitsadmin’s help by typing "bitsadmin" at the command prompt. For our purposes we need the switches CREATE, ADDFILE, SETNOTIFYCMDLINE, COMPLETE, SETCREDENTIALS (maybe), and RESUME.

Let’s say we want to download the english copy of the Automated Installation Kit (AIK) for Windows Vista SP1 and Windows Server 2008. This is a real large download, the ISO file is about 1350 MB. Since the URL string is very large the examples below don’t contain the complete download URL. If you want to reproduce the examples you must use this URL: http://download.microsoft.com/download/9/c/d/9cdfa30e-5901-40e4-b6bf-4a0086ea0a6a/6001.18000.080118-1840-kb3aikl_en.iso

1. Create Job

First of all, we need to create a new download job with Bitsadmin /CREATE

set bits_job=%random%
bitsadmin /create %bits_job%

Bitsadmin will confirm job creation by echoing the job GUID like "Created job {58C6770A-AB4E-477C-8944-33246EC8791E}". For now, the job is created and its state is suspended. (Therefore, we will need to resume the job later in order to start the download process.)

2. Add File

Now we can add the file to the job with Bitsadmin /ADDFILE. We need to specify the job name, the download URL, and the file’s local name.

bitsadmin.exe /addfile %bitsjob% ...aikl_en.iso C:\Downloads\aik_en.iso

Again, Bitsadmin will confirm the action. (Hint: if you want to add multiple files to a job take a look at Bitsadmin’s switch /ADDFILESET which reads each file’s remote URL and local name from a text file.)

3. Optional Settings

The job is still in suspended state, and we are almost ready to start the download. But to start with we should pay attention to three important and useful Bitsadmin switches: COMPLETE, SETNOTIFYCMDLINE, and SETCREDENTIALS

By default, Bitsadmin doesn’t tell anything about a finished file transfer. Even worse, though BITS may have downloaded a file completely you won’t find it unless you have completed the job explicitly. To kill two birds with one stone we will use Bitsadmin /SETNOTIFYCMDLINE to launch Bitsadmin /COMPLETE as loon as the transfer is finished. (Virtually, SETNOTIFYCMDLINE enables you to set a command line to execute for notification when the file download is finished.)

bitsadmin /setnotifycmdline %bitsjob% "%SystemRoot%\system32\bitsadmin.exe" "%SystemRoot%\system32\bitsadmin.exe /complete %bitsjob%"

Maybe the file is located on a password protected web server. In this case you can add credentials to a job using Bitsadmin /SETCREDENTIALS. This time you have to go without an example. Please refer to Bitsadmin’s syntax help by typing "bitsadmin" at the command line.

4. Start/Resume Download

Finally, we are ready to resume the download job with Bitsadmin /RESUME:

bitsadmin /resume %bitsjob%

Now, BITS is downloading the file in the background. Since we have defined the launch of an according job completion command on transfer completion we can just wait and see… or check out some Bitsadmin command lines:

5. Monitor or Cancel Job

How to display information about the BITS job?

bitsadmin /info %bitsjob%

How to retrieve the size of the BITS job?

bitsadmin /getbytestotal %bitsjob%

How to retrieve the number of bytes transferred?

bitsadmin /getbytestransferred %bitsjob%

How to monitor the BITS copy manager?

bitsadmin /monitor

How to cancel the BITS job?

bitsadmin /cancel %bitsjob%

How to delete all BITS jobs?

bitsadmin /reset

6. Write a Shell Script

The batch file below, Bits-Download.cmd, simplifies the usage of Bitsadmin:

@ECHO OFF
:: NAME
::	Bits-Download.cmd
::
:: SYNOPSIS
::	Downloads a remote file with BITS.
::
:: SYNTAX
::	Bits-Download remote_url local_name
::
:: DETAILED DESCRIPTION
::	The Bits-Download.cmd batch file uses BITS to download
::	the given remote file. Bits-Download.cmd requires the
::	BITS Admin Utility Bitsadmin.exe.
::
:: NOTES
::	Bits-Download.cmd was developed and tested on Windows Vista.
::
:: AUTHOR
::	Frank-Peter Schultze
::
:: DATE
::	00:18 21.07.2008

SETLOCAL

	IF "%2"=="" (
		TYPE "%~f0" | findstr.exe /R "^::"
		GOTO :END
	)

	SET bits_job=tigbits%RANDOM%

	SET remote_url="%~1"
	IF NOT DEFINED remote_url (
		ECHO %~n0 : Cannot bind argument to parameter 'remote_url' because it is empty.
		GOTO :END
	)

	SET local_name="%~2"
	IF NOT DEFINED local_name (
		ECHO %~n0 : Cannot bind argument to parameter 'local_name' because it is empty.
		GOTO :END
	)

	(SET /P remote_user=User name ^(leave empty if not required^): )
	IF DEFINED remote_user (SET /P remote_pass=Password: )

	bitsadmin.exe /CREATE /DOWNLOAD %bits_job%

	bitsadmin.exe /ADDFILE %bits_job% %remote_url% %local_name%

	bitsadmin.exe /SETNOTIFYCMDLINE %bits_job% "%SystemRoot%\system32\bitsadmin.exe" "%SystemRoot%\system32\bitsadmin.exe /COMPLETE %bits_job%"

	IF DEFINED remote_user IF DEFINED remote_pass (
		bitsadmin.exe /SETCREDENTIALS %bits_job% SERVER BASIC %remote_user% %remote_pass%
	)

	bitsadmin.exe /RESUME %bits_job%

:END
ENDLOCAL

Category:Cmd.exe Shell, Scripting, Utilities | Comment (0) | Author: Frank-Peter

ICA Client Detection

Friday, 2. May 2008 15:50

With a few lines of VBScript you can check if an ICA Client is installed and, if yes, determine its version.

If you only want to display the version information you can use the ICA Client Object’s AboutBox method:

On Error Resume Next
Set objICA = WScript.CreateObject("Citrix.ICAClient")
If IsObject(objICA) Then
	objICA.AboutBox
Else
	MsgBox "No ICA Client installed."
End If

This works for ICA Client Version 8 or newer.

Another approach uses the ICA Client Object’s ClientVersion property. The function below, GetICAClientVersion, returns the ICA Client’s version as a string:

Dim strICAClientVersion

strICAClientVersion = GetICAClientVersion()

If (strICAClientVersion <> "") Then
	MsgBox "ICA Client " & strICAClientVersion
Else
	MsgBox "No ICA Client installed."
End If

Function GetICAClientVersion()
	Dim strV
	On Error Resume Next
	Set objICA = WScript.CreateObject("Citrix.ICAClient")
	If IsObject(objICA) Then
		strV = objICA.ClientVersion
	Else
		Err.Clear
		strV = ""
	End If
	On Error GoTo 0
	GetICAClientVersion = strV
End Function

Category:Scripting, VBScript | Comment (0) | Author: Frank-Peter