Tuesday, April 10, 2007

getting a list of all users currently connected to Smallworld

I learned a new aspect of Windows command-line scripting the other day that I thought might be worth sharing here.

The requirement was to get a list of all users connected to a particular swmfs server and notify them that the server was about to go down.

I discovered that the FOR command is just what the doctor ordered for this task. The basic form is as follows...

FOR /F "skip=5 delims=@" %%i IN ('swmfs_list -full \\%COMPUTERNAME%\sw400\product\data message.ds') DO net send %%i Smallworld server going down in 2 minutes.


Where...

  • FOR /F ... IN ...
    • Command key words that tell the system that you want to iteratively process the results of some file or command
  • ('swmfs_list -full \\%COMPUTERNAME%\sw400\product\data message.ds')
    • the command that will have each line processed individually for some information. This particular command provides a list of all the user@computername that are connected to message.ds in the specified folder on %COMPUTERNAME%
  • %%i
    • the internal variable representing each line of the file/command
  • "skip=5 delims=@"
    • instructions to the command to skip the first 5 lines of swmfs_list and then also split the processed lines by the @ character.
  • DO
    • a command key word that indicates that what follows next is the command to be processed on each line of iterator
  • net send %%i Smallworld server going down in 2 minutes.
    • the NET SEND command is used to send the message "Smallworld server going down in 2 minutes" to the user specified in variable %%i. Remember that the variable %%i represents the first element of each line after it has been split by the @ character.

Special note about the variables. If you are running this script from within a BAT file, you need to reference the variable with two percent signs: eg., %%i. If you want to test this functionality at the command line, you need to reference the variable with only one percent sign: eg., %i.

I hope you find this useful.

No comments: