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
creative555Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
$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

0
creative555Author Commented:
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?
0
David Johnson, CD, MVPOwnerCommented:
if so then use powershell to parse the csv and pass parameters to the programs
start-process program.exe    $opath.path $opath.user
0
Redefining Cyber Security w/ AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Join our webinar on Sept. 21st to learn more about leveraging AI and machine learning to protect your business.

creative555Author Commented:
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
0
creative555Author Commented:
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:
0
creative555Author Commented:
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
0
creative555Author Commented:
I found such a simple solution for takeown command.....I was overthinking...loops, vbscripts, powershell that didn't work.....I can just run line by line and paste it from excel file all the shares. I can use excel file and then concatenate all the rows with the command and paste it. This scripts works as a charm and re-assigns owners to Builtin\Administrators to all the folders that even got access denied.

takeown /f C:\Installs\1 /a /r
takeown /f C:\Installs\test /a /r
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
creative555Author Commented:
THis was a solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.