Monthly Archives: September 2005

In order to detect if a script was started in a terminal server session the script can check the SESSIONNAME environment variable which is only defined if the Terminal Services are installed on Windows 2000 Server, Windows Server 2003, and Windows XP clients (or newer). When a client connects via a RDP or ICA (Citrix) session, this variable is a combination of the connection name, i.e. “RDP-Tcp” or “ICA-Tcp”, followed by a pound symbol “#” and then the session number in three figures “nnn“. When logging on directly to the machine, this variable returns “Console”.

The VScript function below returns True if the SESSIONNAME variable resolve to a value beginning with “RDP” or “ICA”

The VBScript file below, Tail.vbs, shows the end (the last lines) of a text file.

During the read of the text file, instead of counting the lines, each line is pushed into a small, rotating stack held in a dynamic array (size = number of tail lines). At the end of file, the stack array is popped to echo the tail.

The VBScript file IniFileClass.vbs provides a VBScript class to read from and write to ini files.

Import and Usage

The following approach shows how to import and use the IniFile class with a WSH 2.0 .ws file:

Visual Basic

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<job>

<script language="VBScript"src="IniFileClass.vbs"/>

<script language="VBScript">

' Instantiate the IniFile class

Dim objInifile

Set objInifile = new IniFile

' Show "Last File" key under "Settings" section of

' uedit32.ini (if exists)

objInifile.FileName = "C:\WINDOWS\uedit32.ini"

objInifile.Section = "Settings"

objInifile.Key = "Last File"

If objInifile.Value <> "" Then MsgBox objInifile.Value

' Now, set "Last File" key to "C:\boot.ini"

objInifile.Value = "C:\boot.ini"

' Now, delete the "Last File" key

objInifile.Value = ""

' Now, show the complete ini file

MsgBox objInifile.Content

Set objInifile = Nothing

</script>

</job>

If you still use WSH 1.0 or prefer .vbs files, you can include all the class file’s content at the top of a script or import the class file’s content using the VBScript 5.0 runtime evaluation function ExecuteGlobal as that shown below.

The script reads the AD user object’s properties givenname and sn in order to determine the user’s username and userinitials.

Due to a limitation (or call it bug) in the WSH’s RegWrite implementation with respect to REG_BINARY values the script uses WMI’s StdRegProv‘s SetBinaryValue method. SetBinaryValue expects the registry value’s data in array format. SetOff11Usr.vbs converts the string data to array using the function StringToArray: