LISTSERV at Work
Tweet This
LISTSERV Tech Tip

Q: How can I get a list of all subscribers on all of my LISTSERV lists?

Recently a customer came up with a somewhat unusual request that couldn't easily be handled with LISTSERV's built-in web interface reporting. The request was to output a list of email addresses for all subscribers on all of his lists. While this is technically possible to do for single lists in several different ways, the customer had several hundred lists to process.

In this case, the simplest way to service the request was to use the listview -e command from the Windows command prompt. When invoked with the -e option, the listview utility outputs only the list of email addresses that are currently subscribed to the list. However, the listview utility doesn't recognize the wildcard as input, so the only way to use it to produce output from multiple lists is either to supply the names of the lists, space-separated, on the command line, or to execute the command separately for each list. Again, neither of these methods efficiently satisfied the customer request.

Enter Windows PowerShell. For those who are not aware of PowerShell, a first-order description is that it's a .NET-enabled command shell. It has been around since Windows XP and Windows Server 2003, and the current version is 5.1 for Windows 10 and Windows Server 2016 (although 5.1 is also available for earlier Windows OS, just not fully featured). A new, open-source, cross-platform PowerShell 6.0 Core was recently released for Windows, MacOS and Linux. However, for our purposes, we will restrict our discussion to PowerShell 5.1 running under Windows.

And as it turns out, it is quite simple to write a PowerShell applet to iterate through all of a site's list files to produce the desired output.

$lists = Get-ChildItem -Name -Path *.LIST

write-output "*****"

foreach ($listname in $lists) {

    $listname = $listname.Replace(".LIST","")
    $listname = $listname.ToUpper()
    $buffer = cmd /c listview -e $listname
    $c = $buffer.Count

    write-output "$listname : $c subscribers."

    foreach ($line in $buffer) {

        write-output "$listname : $line"

    }

    write-output "*****"

}

The first line:

$lists = Get-ChildItem -Name -Path *.LIST

pulls a directory of files in the current directory that have an extension of .LIST into a variable (really a one-dimensional string array) called $lists. This is followed (after printing the initial separator string) by a "foreach" loop structure:

foreach ($listname in $lists) {

PowerShell then loops through the following code for each array member it finds in $lists, terminating the loop after the last array member is processed. It executes listview -e against each of the lists it finds and places the resulting listview output into a buffer:

$buffer = cmd /c listview -e $listname

The next line simply counts the number of lines in $buffer, then a second foreach structure is used to write formatted output to the console:

foreach ($line in $buffer) {

    write-output "$listname : $line"

}

followed by another separator line, and then the program loops back to get the next list.

All this produces output as follows (for a test server with two lists, ODBCTEST and TEST):

PS E:\listserv\main> .\listdump.ps1
*****
ODBCTEST : 0 subscribers.
*****
TEST : 3 subscribers.
TEST : someone@SOMEWHERE.COM
TEST : you@UNIVERSITY.EDU
TEST : me@EXAMPLE.COM
*****

PS E:\listserv\main>

Note: This script will not produce any meaningful output for a DBMS-based list, or any other list that does not use the LISTSERV LIST file to store its subscriber addresses, for instance, a Dynamic Query List (DQL). For instance, the ODBCTEST list in the sample output, above, is a DBMS-based list.

Naturally it's possible to produce output without the ***** separators and the subscriber count lines by simply commenting out (or removing) the lines that produce them. The PowerShell commenting symbol is #, placed at the beginning of the text that should be commented. It should also be possible to sort the subscriber output, if desired, but that is beyond the scope of the original request. There are also other efficiencies that could be applied to the script, but such will be left as an exercise for the reader.

Use with Caution: While PowerShell can be a powerful tool for producing ad-hoc reports and doing other LISTSERV maintenance tasks under the Windows OS, it should always be remembered that at no time should PowerShell or any other third-party programming interface be used to directly manipulate any LISTSERV system files – including but not restricted to LISTSERV's *.LIST files.


Subscribe to LISTSERV at Work.

© L-Soft 2018. All Rights Reserved.




Powered by LISTSERV Maestro