Solved

Powershell Script to take ownership/copy/delete local folder

Posted on 2015-01-27
10
746 Views
Last Modified: 2015-02-02
I have the script below which I need to take ownership of the folder, copy it to a NAS and then delete it. I can't seem to get the take ownership function to work. Please help! Thanks!

$user = Read-Host 'Enter Username of Profile to Delete'
If ($user -ne $null){
$Folder = "D:\RDSProfiles\$user.PHI.V2"
    If (Test-path $Folder){
    takeown /f $Folder /R /D Y
    Copy-Item -Path "$Folder" -Force -Recurse -Destination "\\PISNAS1\User Archive\\"
    Remove-Item $Folder -Recurse -Force
      }
}
0
Comment
Question by:TheNetworkPro15
  • 5
  • 5
10 Comments
 
LVL 40

Expert Comment

by:footech
ID: 40574070
Try using the /A switch to give ownership to the Administrators group.
takeown /f $Folder /R /A /D Y

Open in new window

0
 

Author Comment

by:TheNetworkPro15
ID: 40576278
It seems like the recursion is not working. It will copy the root level folders but errors out on any contents below that. I'll try to run it again and see if I can give specific errors.
0
 
LVL 40

Expert Comment

by:footech
ID: 40576516
Taking ownership may not be enough.  You may have to adjust permissions.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:TheNetworkPro15
ID: 40577744
Would you be able to add the necessary lines in there to give "PHI\gocadmin" full permissions to all files and folders?
0
 
LVL 40

Expert Comment

by:footech
ID: 40577976
You could try adding the following after the takeown command
icacls $folder /grant "PHI\gocadmin:(OI)(CI)(F)" /T
0
 

Author Comment

by:TheNetworkPro15
ID: 40578725
I added the line and it seemed to work better but it's still giving errors when it gets down to the file level. I look at the NTFS permissions on the files and the new ownership and gocadmin permissions did not apply. I attached the log so maybe that will be helpful.

The script looks like this now.

$user = Read-Host 'Enter Username of Profile to Delete'
If ($user -ne $null){
$Folder = "D:\RDSProfiles\$user.PHI.V2"
    If (Test-path $Folder){
    takeown /f $Folder /R /A /D Y
    icacls $folder /grant "PHI\gocadmin:(OI)(CI)(F)" /T
    Copy-Item -Path "$Folder" -Force -Recurse -Destination "\\PISNAS1\User Archive\\"
    Remove-Item $Folder -Recurse -Force
      }
}
Errors.docx
0
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40578786
If ownership and permissions didn't apply, then it's likely an inheritance issue.  The only way I know to deal with that is to process each file individually.  You might try adding the following after the icacls line.
Get-ChildItem $folder -force -recurse | Select -exand Fullname | % `
{
    takeown /f $_ /A /D Y
    icacls $_ /grant "PHI\gocadmin:(F)"
}

Open in new window


If you want to cut down on some of the output, you can add the /Q switch to icacls.
Permissions can be a pain.  It seems like every time it's a different scenario when dealing with them in batch, depending on what existed before copying/deleting.
0
 

Author Comment

by:TheNetworkPro15
ID: 40580336
So to clarify, my new script should look like this?

$user = Read-Host 'Enter Username of Profile to Delete'
If ($user -ne $null){
$Folder = "D:\RDSProfiles\$user.PHI.V2"
    If (Test-path $Folder){
    takeown /f $Folder /R /A /D Y
    icacls $folder /grant "PHI\gocadmin:(OI)(CI)(F)" /T
Get-ChildItem $folder -force -recurse | Select -exand Fullname | % `
{
    takeown /f $_ /A /D Y
    icacls $_ /grant "PHI\gocadmin:(F)"
}
    Copy-Item -Path "$Folder" -Force -Recurse -Destination "\\PISNAS1\User Archive\\"
    Remove-Item $Folder -Recurse -Force
      }
}
0
 
LVL 40

Expert Comment

by:footech
ID: 40580397
Yes, that's right.
Quick suggestion - in general when posting any code here longer than a line or two, try to use the code block feature to format your text (just select the pertinent text, then click the "Code" formatting item).
0
 

Author Comment

by:TheNetworkPro15
ID: 40584691
That last bit of code seems to have done the trick. Thanks for the help!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

821 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