Add account to TS (Perl)

This code can be found in Chapter 17 of Windows Server 2003 Security 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 code adds a user, group, or computer to Terminal Services
# ------ SCRIPT CONFIGURATION ------

use Win32::OLE qw(in);

use constant WINSTATION_GUEST_ACCESS => 0;
use constant WINSTATION_USER_ACCESS => 1;
use constant WINSTATION_ALL_ACCESS => 2;

$strComputer = '.';
$strAccount = 'srv01\\rallen';
$intPerm = WINSTATION_USER_ACCESS;
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$colItems = $objWMI->ExecQuery('Select * from Win32_TSPermissionsSetting');
foreach my $objItem (in $colItems) {
    $intRC = $objItem->AddAccount($strAccount, $intPerm);
    if ($intRC) {
        print 'Error adding ' . $strAccount . ' to ' . $objItem->TerminalName, "\n";
    }
    else {
        print 'Successfully added ' . $strAccount . ' to ' . $objItem->TerminalName, "\n";
    }
}

This code has been viewed 723 times.

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