?
Solved

Script to remove listed users home folder from server

Posted on 2014-10-23
14
Medium Priority
?
125 Views
Last Modified: 2014-11-06
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
Comment
Question by:auk_experts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40400438
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
 
LVL 25

Expert Comment

by:Lionel MM
ID: 40402967
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
 
LVL 29

Expert Comment

by:becraig
ID: 40403174
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:auk_experts
ID: 40404511
dear becraig,

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


thanks
0
 

Author Comment

by:auk_experts
ID: 40404789
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
 
LVL 29

Expert Comment

by:becraig
ID: 40404900
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
 

Author Comment

by:auk_experts
ID: 40408173
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
 
LVL 29

Expert Comment

by:becraig
ID: 40408840
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
 

Author Comment

by:auk_experts
ID: 40410036
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
 
LVL 29

Accepted Solution

by:
becraig earned 1200 total points
ID: 40410987
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
 
LVL 25

Assisted Solution

by:Lionel MM
Lionel MM earned 300 total points
ID: 40411085
/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
 
LVL 29

Expert Comment

by:becraig
ID: 40411091
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
 
LVL 25

Expert Comment

by:Lionel MM
ID: 40412176
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
 

Author Comment

by:auk_experts
ID: 40412577
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
Suggested Courses

800 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