Solved

help to write a script to copy all folders/subfolders from a network drive location to another share location

Posted on 2013-06-14
16
972 Views
Last Modified: 2013-06-24
Hello Experts

I am beginner in Powershell, and just wondering if you could help me to write a powershell script to do the following in AD

Every single user's profile in AD has a network drive map connection called X. This drive is used by each user to save their documents, folders/files, etc

When someone leaves our company, we would like to copy all content from this X drive[folder/subfolders/files] for each user in AD to be disabled to another secure shared location called LegacyRoot\Users\UserX..

Can anyone please help me with some ideas, best efforts to build an script to accomplish this goal?
0
Comment
Question by:Jerry Seinfield
  • 6
  • 6
  • 3
  • +1
16 Comments
 
LVL 39

Expert Comment

by:Adam Brown
ID: 39248524
All you really need to do is type
copy \\fileservername\sourcefolder\*.* -destination \\server\legacyroot\users\userx  -recurse
and it will do that for you. Is there some kind of automation you're looking for?
0
 
LVL 42

Expert Comment

by:Amit
ID: 39248526
better to use xcopy here.
0
 

Author Comment

by:Jerry Seinfield
ID: 39248649
Thanks for the ideas. I need to build that in powershell, and please keep in mind i have to copy a complete folder structure including but not limited to folders, sub-folders, files from source[my source is a network mapped drive for each user's profile to a another location

Powershell is mandatory, For example

User A has a windows profile, and every time he login into the network, by logon script 3 network drives are automatically mapped. so i have to copy all content from X drive[network mapped drive] to another location

Any ideas?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39248709
$source = \\uncpath\
$destination = [whateverpath]
copy-item $source $destination -recurse
0
 
LVL 42

Expert Comment

by:Amit
ID: 39248710
are you looking for permission copy too?
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39248856
looks like you want to use move-item vs COPY item
$usernames = get-content -path c:\temp\firedusers.txt
$DestinationRoot = "\\servername\LegacyRoot\User$\"
$uncpath = "\\server1\user$\"
#In your folder redirection policies you will get the ACTUAL path to the redirected users 
# location i.e. \\server1\user$

foreach ($user in $usernames) 
     {
     $source = $uncpath + $user + "\"
     $destination = $DestinationRoot + $user + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     }

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39248862
yes, need to copy all folders and subfolders including permissions

Can you please clarify how can i look into every single user's profile and copy the data from their M drive to destination shared folder?
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39248876
You don't need the users mapped drive.. You have access to the server where these items are stored.

Why even bother with saving permissions for terminated users??? Their account is disabled in AD, I hope!
0
 
LVL 42

Expert Comment

by:Amit
ID: 39248878
that was my doubt, i know what you were looking, basically permission is the main part here. Which is not that easy. Let me check with my friend tomorrow, he wrote something similar long back....
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39248893
$usernames = get-content -path c:\temp\firedusers.txt
$DestinationRoot = "\\servername\LegacyRoot\User$\"
$uncpath = "\\server1\user$\"
#In your folder redirection policies you will get the ACTUAL path to the redirected users 
# location i.e. \\server1\user$

foreach ($user in $usernames) 
     {
     $source = $uncpath + $user + "\"
     $TempAcl = get-acl $source
     $destination = $DestinationRoot + $user + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
     Get-ChildItem $destination -Recurse -Include * -Force | Set-Acl -AclObject $TempAcl -whatif
    #
     }

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39263118
Hi V3eofa,

FilerC is a netapp that hosts all shared folders.

By group policy, and as per attached screen shoot, users will get mapped drive M.

My destination root is another shared folder on a different SAN storage[Dell Compellent]. Both will keep same structure [\\SAN_ShareFolderName\%username%$

I would like to use a CSV file instead of a TXT, please see also csv file attached.

%username%$ represents a variable that indicates user profile.

For example, the user where SAM account name or AdUsername is jdoe has a user profile and if this user is part of the csv file, we must copy the content of M drive to destination root

Based on the CSV file attached,  the group policy and new values for source and destination,

Can i use the code below?

$usernames = get-content -path c:\temp\UserList.csv
$DestinationRoot = "\\DellSAN.domain.on.ca\%username%$"
$uncpath = "\\filerc.domain.on.ca\%username%$"
#In your folder redirection policies you will get the ACTUAL path to the redirected users
# location i.e. \\server1\user$

foreach ($user in $usernames)
     {
     $source = $uncpath + $user + "\"
     $destination = $DestinationRoot + $user + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     }
UserProfileNetworkmappeddrivegro.jpg
UserList3.csv
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39263387
$usernames = import-csv -path c:\temp\UserList.csv
$DestinationRoot = "\\DellSAN.domain.on.ca\"
$uncpath = "\\filerc.domain.on.ca\"
#In your folder redirection policies you will get the ACTUAL path to the redirected users
# location i.e. \\server1\user$

foreach ($user in $usernames)
     {
     $source = $uncpath + $user.ADUsername+ "\"
     $destination = $DestinationRoot + $user.ADUsername + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     } 

Open in new window

0
 

Author Comment

by:Jerry Seinfield
ID: 39263492
Hi ve3ofa

You rock,

I will test the script, and keep you posted
0
 

Author Comment

by:Jerry Seinfield
ID: 39264113
Hi Ve3ofa

For the source path in filerc, the user folder is hidden so, Should I  add $ to your path ($source = $uncpath + $user.ADUsername+ "$\")

Another question, I got the final destination path as per below

 \\SAN.domain.on.ca\users$\CIFS_Users$\userAlias

With that said

How would look the final script?
0
 
LVL 79

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 39264306
$usernames = import-csv -path c:\temp\UserList.csv
$DestinationRoot = "\\SAN.domain.on.ca\users$\CIFS_Users$"
# I think you only need the last $  
$uncpath = "\\filerc.domain.on.ca\"


#In your folder redirection policies you will get the ACTUAL path to the redirected users
# location i.e. \\server1\user$

foreach ($user in $usernames)
     {
     $source = $uncpath + $user.ADUsername+ "\"
#
# here you say  you want it to be useralias but that is not in your csv    
# unless you have a share \\ \\SAN.domain.on.ca\users$\CIFS_Users$\userAlias
# and want to tag the ADUsername after it.
#
$destination = $DestinationRoot + $user.ADUsername + "\"
     # remove the -whatif when final
     move-item $source $destination -recurse -whatif
    #
     }
0
 

Author Closing Comment

by:Jerry Seinfield
ID: 39272955
Many thanks for all the help
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

8 Experts available now in Live!

Get 1:1 Help Now