Determine the Exchange role of a server (Perl)

This code can be found in Chapter 4 of Exchange Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This script was originally published in the Exchange Cookbook,
# (http://www.exchangebookcook.com). Written by Paul Robichaux, 
# Missy Koslosky, and Devin Ganger. Redistributed with permission 
# of the publisher, O'Reilly & Associates.

# This code uses ADO to query the MSExchServerRole attribute for all servers
# in the domain. 
# --------------SCRIPT CONFIGURATION------------------

use Win32::OLE;

$strBase = '<LDAP://cn=administrative groups,cn=<orgName>,cn=microsoft exchange,cn=services,cn=configuration,<domain>';
# dc=<domain>,dc=<tld>;"
$strFilter = '(objectcategory=MSExchExchangeServer);';
$strAttrs = 'ServerRole,cn;';
$strScope = 'subtree';
# ---------------END CONFIGURATION---------------------
$objConn = Win32::OLE->new('ADODB.Connection');
$objConn->Open('Provider=ADsDSOObject;');

$objRS = $objConn->Execute($strBase . $strFilter . $strAttrs . $strScope);
$objRS->MoveFirst();
while (!$objRS->EOF) {
    $strName = $objRS->Fields('cn')->value;

    # Get the value of the MSExchServerRole
    if ($objRS->Fields('ServerRole')->value == 1) {
        $strRole = 'Front-End Server';
    }
    elsif (!($objRS->Fields('ServerRole')->value)) {
        $strRole = 'Back-End Server';
    }
    elsif ($objRS->Fields('ServerRole')->value == 0) {
        $strRole = 'Back-End Server';
    }
    print $strName . ' is a ' . $strRole, "\n";
    $objRS->MoveNext();
}

This code has been viewed 908 times.

New from the creators of TechTasks.com: StatSheet.com