• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 126
  • Last Modified:

Script to remove listed users home folder from server

Hello All,

I want to get a script which can take a list of users in csv file and then run a check against it on the remote file server location  to see if folder exists and then zip the folder to a new location and then delete that folder once the task of zip is completed successfully.

I had one programmer writing that as a program for me but he is not working with me anymore.

Thanks for all the help and support i can get.

Shaik Sayeed
0
auk_experts
Asked:
auk_experts
  • 6
  • 5
  • 3
2 Solutions
 
becraigCommented:
Just for clarity, you want to convert the users home folder into a zip file then copy it to a specified destination and delete the original folder ?
0
 
Lionel MMSmall Business IT ConsultantCommented:
I suggest you check against the list and then robocopy all those folders to the zip location using the move switch in robocopy (which will remove it from the old server location) and then nonce it is in the new location (which ensures you have a full and complete move) then you can zip it there--if that approach will work for you I can work on a script to do that. I use 7z-zip to zip--which zip tool do you use?
0
 
becraigCommented:
So here is a decent path to get you there:

First download the pcsx modules (we need their write-zip module):
http://pscx.codeplex.com/downloads/get/923562
Run the msi and install the components on your computer

Then we need to launch a powershell window to ensure the modules are available:
Enter the commands below in an elevated powershell window:

$p = [Environment]::GetEnvironmentVariable("PSModulePath")
$p += ";C:\Program Files (x86)\PowerShell Community Extensions\Pscx3"
[Environment]::SetEnvironmentVariable("PSModulePath",$p)

Once this is complete we are good to proceed:

Here is the actual script that you will run from a machine where you installed the modules above.


Import-Module Pscx

#$_.username is assuming you have a header named username for your username value

import-csv | %  {
	$user = $_.username
	if (Test-path \\server\folder\$user)
	{
		gci \\server\folder\$user -Recurse | Write-Zip -IncludeEmptyDirectories -OutputPath \\destination\$user.zip}
		if (Test-Path \\destination\$user.zip)
		{ Remove-item -path \\server\folder\$user -Force -recurse }
	}

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
auk_expertsAuthor Commented:
dear becraig,

I see the method shown by you as most easiest, will try it out and let you know by tmrw.


thanks
0
 
auk_expertsAuthor Commented:
script ended up with below errors ,please advise

Write-Zip : Skipping New folder. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\S00027627\New folder:String) [Write-Zip], ArgumentExceptio
   n
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Cartoon assi. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...er\Cartoon assi:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping level 3 fall. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...er\level 3 fall:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Bader Al-Seedo-06.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...l-Seedo-06.docx:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Bader Al-Seedo.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...r Al-Seedo.docx:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Bader_Abu_Seedo_Report.html. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...edo_Report.html:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Cartoon narrative.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\... narrative.docx:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping EAST OF EDEN.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...ST OF EDEN.docx:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Introduction.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...troduction.docx:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Mla.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...folder\Mla.docx:String) [Write-Zip], ArgumentException
    + FullyQualifiedErrorId : FileError,Pscx.Commands.IO.Compression.WriteZipCommand

Write-Zip : Skipping Persuasive Presentation DETAILED OUTLINE2.docx. It is not under the entry path root U:\.
At C:\temp\del_homefolder.ps1:9 char:39
+         gci \\filesrv\home\$user -Recurse | Write-Zip -IncludeEmptyDirectories -Output ...
+                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (\\filesrv\home\...D OUTLINE2.docx:String) [Write-Zip], ArgumentException
0
 
becraigCommented:
Ok so it seems we missed adding an input parameter:

Import-Module Pscx

#$_.username is assuming you have a header named username for your username value

import-csv | %  {
	$user = $_.username
	$pathval = "\\server\folder\$user"
	if (Test-path \\server\folder\$user)
	{
		gci $pathval  -Recurse | Write-Zip -OutputPath \\destination\$user.zip  -IncludeEmptyDirectories -EntryPathRoot $pathval
	}
		if (Test-Path \\destination\$user.zip)
		{ Remove-item -path \\server\folder\$user -Force -recurse }
	}

Open in new window



The above edited version should resolve this for you.
0
 
auk_expertsAuthor Commented:
Dear Becraig,

Now i do not have error but the content of destination zipped file is 1kb and the source content is removed once i runt the script ( as requested).Good that i did run it only on a test users list.

Please advise.

shaik
0
 
becraigCommented:
The only thing I can assume that might have gone wrong would be a matter of permissions.

We can probably add a robocopy to the script which will allow you to run multiple threads and do the moves in parallel, then just simply do the zip as the last step.

Let me know if you need to retain security information etc on the folders first before I make the update.
0
 
auk_expertsAuthor Commented:
Dear Becraig,

Security permissions are not must , data should be intact mainly and yes robocopy is also suffice as long as it copies the data without loss and then zip at the destination.

thanks
0
 
becraigCommented:
So we will do this in two stages:
1:
import-csv | %  {
	$user = $_.username
	$pathval = "\\server\folder\$user"
	if (Test-path \\server\folder\$user)
	{
		robocopy $pathval \\destination\ /mov /MT:16 /MIR
	}

Open in new window

     
2:
We now create the zip file at the destination:

import-csv | %  {
	$user = $_.username
	if (Test-path \\destination\$user)
	{
		gci \\destination\$user -Recurse | Write-Zip -OutputPath  \\destination\$user.zip -IncludeEmptyDirectories -EntryPathRoot \\destination\$user
	}
	}

Open in new window

0
 
Lionel MMSmall Business IT ConsultantCommented:
/mov needs to be /move so that files and folders are moved
and using /mir is dangerous if you happen to run this more than once because /MIR will make the destination identical to the source and if the source is empty it will also make the destination empty so I suggest you only use the /move switch
0
 
becraigCommented:
Thanks Lionel, I actually thought I had the move switch and not the mov switch.

Thanks for the correction.

I would suggest the OP award you full points for this question as we pretty much ended up going your path.
0
 
Lionel MMSmall Business IT ConsultantCommented:
That is generous of you but you provided much of the code and the code is what does the work. I didn't give any code because you did so you deserve the points--in my opinion you should get 75% to 100% of the points
0
 
auk_expertsAuthor Commented:
Dear Craig,

I will try the code today, i see that second part is doing a zip and writing back again to the dest with unc path, cant we zip there and without having an additional output path isnt that a double work.

Thanks for any advise.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now