Solved

Script to change file ownership?

Posted on 2006-06-27
16
1,947 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
ID: 16995073
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
ID: 16995108
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
ID: 16995304
woops, my bad.  That's the take ownership permission.

0
 
LVL 3

Author Comment

by:hberenson
ID: 16995625
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
ID: 16995788
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
ID: 16996207
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
ID: 16996759
It runs ok, echos appropriately when a match is found, but doesn't actually seem to change the ownership.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 26

Expert Comment

by:Pber
ID: 16997844
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
ID: 17000874
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
ID: 17001000
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
ID: 17001345
Are you familiar with programming?  Do you have VB6?
0
 
LVL 26

Accepted Solution

by:
Pber earned 500 total points
ID: 17002097
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
ID: 17002181
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
ID: 17019337
Did you ever get this working?
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Check Disk (CHKDSK) on all volumes and fix if needed. 8 178
Moving Files servers to DFS 11 43
DHCP server 6 51
SBS 2003 RWW Login 3 21
Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

27 Experts available now in Live!

Get 1:1 Help Now