Link to home
Start Free TrialLog in
Avatar of auk_experts
auk_experts

asked on

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
Avatar of becraig
becraig
Flag of United States of America image

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 ?
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?
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

Avatar of auk_experts
auk_experts

ASKER

dear becraig,

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


thanks
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
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.
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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of becraig
becraig
Flag of United States of America image

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
SOLUTION
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
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.
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
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.