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.Secu rity.Princ ipal.NTAcc ount] "domain\Caussyr")
set-acl $FTPPath $acl
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.Secu
set-acl $FTPPath $acl
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?
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
start-process program.exe $opath.path $opath.user
ASKER
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
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
ASKER
Unfortunately, it didn't work: I got this error. Please help
ERROR: + set-acl $owner.path $acl
ERROR: + ~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidData: (:) [Set-Acl], ParameterBindingValidation Exception
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidatio nErrorNull NotAllowed ,Microsoft .PowerShel l.Commands .SetAclCom mand
ERROR:
ERROR: + set-acl $owner.path $acl
ERROR: + ~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidData: (:) [Set-Acl], ParameterBindingValidation
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidatio
ERROR:
ASKER
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.Fi leSystemOb ject")
set objTextFile = objFile.OpenTextFile(strFi lePath,1)
Set WshShell = WScript.CreateObject("WScr ipt.Shell" )
If Not objFile.FolderExists(strOu tFolderPat h) 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(strLi ne,"\") + 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
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.Fi
set objTextFile = objFile.OpenTextFile(strFi
Set WshShell = WScript.CreateObject("WScr
If Not objFile.FolderExists(strOu
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(strLi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THis was a solution
Open in new window