Modify 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 transfers ownership of the specified file to the
# user running the script.  If strFile is set to a folder path
# then ownership of all files within the folder will be changed. 
# ------ SCRIPT CONFIGURATION ------

use Win32::OLE;

$strFile = '<FilePath>';
# e.g. d:\scripts
$strComputer = '<ServerName>';
# e.g. rallen-svr1 or . for local server
# ------ END CONFIGURATION ---------
$objWMI = Win32::OLE->GetObject('winmgmts:\\\\' . $strComputer . '\\root\\cimv2');
$objFile = $objWMI->Get('CIM_DataFile.Name=\'' . $strFile . '\'');
$intRC = $objFile->TakeOwnership;
if ($intRC == 0) {
    print "File ownership successfully changed\n";
}
else {
    print 'Error transferring file ownership: ' . $intRC, "\n";
}

This code has been viewed 1238 times.

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