[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
Medium Priority
?
1,009 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
[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
  • 6
  • 3
  • +1
16 Comments
 
LVL 43

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 44

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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 83

Expert Comment

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

Expert Comment

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

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 83

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 44

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 83

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 83

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 83

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

649 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