Link to home
Start Free TrialLog in
Avatar of creative555
creative555

asked on

take ownership of files using powershell

hello,
I found the following script to take ownership of files. Could you please modify the script so that it can read csv input file with the PATH column and Users column which will assign the new owners for the shares specified in the excel. Thank you so much.

$FTPPath = 'c:\temp\test'
$acl = get-acl $FTPPath
$acl.SetOwner([System.Security.Principal.NTAccount] "domain\Caussyr")
set-acl $FTPPath $acl
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

$ownerpath = import-csv -Path .\ownerpath.csv
<# Sample CSV
user,path
'domain\user1','c:\temp\abc'
'domain\user2','c:\temp\abcd'
#>
foreach ($opath in $ownerpath){
$acl = get-acl -Path $opath.path
$acl.SetOwner([System.Security.Principal.NTAccount] $opath.user)
set-acl $owner.path $acl 
}

Open in new window

Avatar of creative555
creative555

ASKER

Awesome!! Let me test out the script. I was reading somewhere that it was throwing some errors with powershell and that's why it was recommended to use other external utility such as takeown or subinacle. But I searched everywhere and didn't find any switches for those so that I can use excel file as import. It will take me forever to run line by line with takeownership or subinacle. So hopefully this will work.

Question,
Would import-csv work as well with takeown or subinacle util?
if so then use powershell to parse the csv and pass parameters to the programs
start-process program.exe    $opath.path $opath.user
Here is the subniacle and takeown command.

subinacl /file test.txt /setowner=domain\foo

or

takeown /f \\Fileserver01\sales /r /A /d y

So I would do like this?
start-process subniacle   $opath.path $opath.user
Unfortunately, it didn't work: I got this error. Please help


ERROR: +     set-acl $owner.path $acl
ERROR: +             ~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidData: (:) [Set-Acl], ParameterBindingValidationException
ERROR:     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetAclCommand
ERROR:
a friend sent me an example of vbscript below that is used to work with subinacl to do a loop. The vbscript does completely different thing but this is an example that subinacl could be used with vbscript or possibly powershell to loop. Since I am new to powershell and scripting, I would not be able to do this. Could you please help so that I can change owners for the files using csv import file that lists multiple shares and new owners.

subinacl /file c:\test\ntfs\ /setowner=Test

subinacl /file c:\test\ntfs\ /setowner=Test

  '===================================================================

strFilePath = "MSLogFile.log"
strOutFolderPath = "C:\BackupPermission"
strSubInAclPath = "C:\Program Files\Windows Resource Kits\Tools\SUBINACL.exe"

Set objFile = Createobject("Scripting.FileSystemObject")
set objTextFile = objFile.OpenTextFile(strFilePath,1)
Set WshShell = WScript.CreateObject("WScript.Shell")

If Not objFile.FolderExists(strOutFolderPath) Then
objFile.CreateFolder (strOutFolderPath)
End If

count = 0

Do Until objTextFile.AtEndOfStream
strLine = objTextFile.ReadLine
strLine = Split(strLine,",")(0)
If Trim(strLine) <> "" Then
  strOutLog = strOutFolderPath & "\" & Mid(strLine,InStrRev(strLine,"\") + 1,Len(strLine)) & ".txt"
  strCommand = Chr(34) & strSubInAclPath & Chr(34) & " /noverbose /outputlog=" & strOutLog & " /subdirectories " & strLine & " /display"
  wscript.echo strCommand
  Return = WshShell.Run(strCommand, 1, true)
End If


Loop
ASKER CERTIFIED SOLUTION
Avatar of creative555
creative555

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THis was a solution