Solved

Script to change file ownership?

Posted on 2006-06-27
16
1,935 Views
Last Modified: 2008-03-10
Anyone have a script that you can point at the top of a directory tree, give it new and old file ownership, and have it go through all folders and files in the tree and change any with the old file ownership to have the new ownership?
0
Comment
Question by:hberenson
  • 10
  • 4
16 Comments
 
LVL 26

Expert Comment

by:Pber
Comment Utility
download xcacls.vbs

http://support.microsoft.com/?id=825751

then do a

cscript xcacls.vbs *.* /S /T /E /G domain\user:D

0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
Forget that, you don't need the vbs version, just use xcacls from the support tools

xcacls.exe *.* /T /E /C /G domain\user:O
0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
woops, my bad.  That's the take ownership permission.

0
 
LVL 3

Author Comment

by:hberenson
Comment Utility
xcacls.vbs will let me change ownership, but it doesn't seem to have a way to do it conditional to who the current owner is.
0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
Try the following vbs Script...

path = "c:\"
Const oldOwner = "BUILTIN\Administrators"
Const newOwner = "domain\user"

'do not change below this point


Recurse(path)

Public Sub Recurse(path)
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set fldr = fso.GetFolder(path)

      dim subfolders,files,folder,file
       Set subfolders = fldr.SubFolders
       Set files = fldr.files
       
         'Display the path and all of the folders.
         Wscript.Echo ""
         Wscript.Echo fldr.Path
         For Each folder in subfolders
            Wscript.Echo folder.path
            'WScript.Echo folder.path
               setOwner oldOwner,newOwner,folder.path
         Next
 
   'Display all of the files.
       For Each file in files
               wscript.echo file.path
           setOwner oldOwner,newOwner,file.path
           
       Next  
   'Recurse all of the subfolders.
       For Each folder in subfolders
          Recurse folder
       Next  
       Set subfolders = Nothing
         Set files = Nothing
End Sub


Sub setOwner(oldOwner,newOwner,path)
      Dim dACL, ace, sd
      
      Set sec = CreateObject("AdsSecurity")
      Set sd = sec.GetSecurityDescriptor("FILE://" & path)
      Set dACL = sd.DiscretionaryAcl
      WScript.Echo " - Old Owner: " & sd.owner
      
      If lcase(sd.owner) = lcase(oldOwner) Then
            'WScript.Echo " - Old Owner: " & sd.owner
            WScript.Echo " - New Owner: " & newOwner
            sd.owner = newOwner
             sd.DiscretionaryAcl = dACL
             sec.SetSecurityDescriptor sd
      End if
       
       Set dACL = nothing
       Set sd = Nothing
       Set sec = nothing
 End sub
0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
Sorry failed to mention you'll need to install the AdsSecurity.dll

http://download.microsoft.com/msdownload/adsi/2.5/sdk/x86/en/Sdk.zip
0
 
LVL 3

Author Comment

by:hberenson
Comment Utility
It runs ok, echos appropriately when a match is found, but doesn't actually seem to change the ownership.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 26

Expert Comment

by:Pber
Comment Utility
Hmmm interesting.  It was working for me.  I'll take a look when I get back to work... maybe I trimmed something out before I posted it
0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
It would seem the problem is a priviledge level to be able to set the owner.  You can only take ownership yourself or you can set it to the builtin\administrators.

See this article from microsoft:
http://support.microsoft.com/?kbid=318744

Unfortunately you can't do this in vbs, only vb.

You said the xcacls.vbs file changed your ownership.  Did it set it to the owner you wanted or to your userid?
0
 
LVL 3

Author Comment

by:hberenson
Comment Utility
I didn't actually run xcacls.vbs, just looked at it.  I am able to change the owner to whatever I want from Explorer.  The account I'm using for all of this is an Administrator account.
0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
Are you familiar with programming?  Do you have VB6?
0
 
LVL 26

Accepted Solution

by:
Pber earned 500 total points
Comment Utility
Try using subinacl.exe - download the latest version from microsoft (as the !@#$!@# old one doesn't work): http://www.microsoft.com/downloads/details.aspx?FamilyId=E8BA3E56-D8FE-4A91-93CF-ED6985E3927B&displaylang=en


Then use the following script ro run it:


path = "c:\"
Const oldOwner = "BUILTIN\Administrators"
Const newOwner = "domain\user"

'do not change below this point


Recurse(path)

Public Sub Recurse(path)
     Set fso = CreateObject("Scripting.FileSystemObject")
     Set fldr = fso.GetFolder(path)

     dim subfolders,files,folder,file
      Set subfolders = fldr.SubFolders
      Set files = fldr.files
     
        'Display the path and all of the folders.
        Wscript.Echo ""
        Wscript.Echo fldr.Path
        For Each folder in subfolders
           Wscript.Echo folder.path
           'WScript.Echo folder.path
             setOwner oldOwner,newOwner,folder.path
        Next
 
   'Display all of the files.
      For Each file in files
             wscript.echo file.path
          setOwner oldOwner,newOwner,file.path
         
      Next  
   'Recurse all of the subfolders.
      For Each folder in subfolders
         Recurse folder
      Next  
      Set subfolders = Nothing
        Set files = Nothing
End Sub

 Sub setOwner(oldOwner,newOwner,path)
    Set sec = CreateObject("AdsSecurity")      
    Set sd = sec.GetSecurityDescriptor("FILE://" & path)
    WScript.Echo " - Old Owner: " & sd.owner
     
    If lcase(sd.owner) = lcase(oldOwner) Then
        WScript.Echo " - New Owner: " & newOwner
        Set CommandLine = CreateObject("WScript.Shell")
        commandline.run "subinacl /file " & path & " /setowner=" & newOwner
    End if
     
    Set sd = Nothing
    Set sec = nothing
 End Sub
 
0
 
LVL 3

Author Comment

by:hberenson
Comment Utility
Oh I think I can program a bit :-)  Let me try your last example, and if that doesn't work I'll go ahead and write something.
0
 
LVL 26

Expert Comment

by:Pber
Comment Utility
Did you ever get this working?
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

This may not be a text book method to resolve VSS backup issues but it seemed to have worked on few of the Windows 2003 servers we had issues while performing a Volume Shadow Copy backup. If you have issues while performing a shadow copy backup usin…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now