Solved

Script to remove listed users home folder from server

Posted on 2014-10-23
14
118 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
  • 6
  • 5
  • 3
14 Comments
 
LVL 28

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 24

Expert Comment

by:lionelmm
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 28

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
 

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 28

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
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

 
LVL 28

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 28

Accepted Solution

by:
becraig earned 400 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 24

Assisted Solution

by:lionelmm
lionelmm earned 100 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 28

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 24

Expert Comment

by:lionelmm
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

Want to promote your upcoming event?

Attending an event? Speaking at a conference? Or exhibiting at a tradeshow? Easily inform your contacts by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

Join & Write a Comment

I had a question today where the user wanted to know how to delete an SSL Certificate, so I thought that I would quickly add this How to! Article for your reference. WHY WOULD YOU WANT TO DELETE A CERTIFICATE? 1. If an incorrect certificate was …
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…
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now