• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2117
  • Last Modified:

Script to change file ownership?

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
hberenson
Asked:
hberenson
  • 10
  • 4
1 Solution
 
PberSolutions ArchitectCommented:
download xcacls.vbs

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

then do a

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

0
 
PberSolutions ArchitectCommented:
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
 
PberSolutions ArchitectCommented:
woops, my bad.  That's the take ownership permission.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hberensonAuthor Commented:
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
 
PberSolutions ArchitectCommented:
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
 
PberSolutions ArchitectCommented:
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
 
hberensonAuthor Commented:
It runs ok, echos appropriately when a match is found, but doesn't actually seem to change the ownership.
0
 
PberSolutions ArchitectCommented:
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
 
PberSolutions ArchitectCommented:
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
 
hberensonAuthor Commented:
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
 
PberSolutions ArchitectCommented:
Are you familiar with programming?  Do you have VB6?
0
 
PberSolutions ArchitectCommented:
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
 
hberensonAuthor Commented:
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
 
PberSolutions ArchitectCommented:
Did you ever get this working?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now