Display file owner (Perl)

This code can be found in Chapter 10 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 prints the owner of a file. 
# ------ SCRIPT CONFIGURATION ------

use Win32::OLE qw(in);

$strFile = '<FilePath>';
# e.g. d:\scripts\foo.vbs
$strComputer = '<ServerName>';
# e.g. rallen-svr1 or . for local server
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$colItems = $objWMI->ExecQuery('ASSOCIATORS OF {Win32_LogicalFileSecuritySetting=\'' . $strFile . '\'}' . ' WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner');
foreach my $objItem (in $colItems) {
    print 'Owner: ' . $objItem->ReferencedDomainName . '\\' . $objItem->AccountName, "\n";
}

This code has been viewed 1680 times.

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