foreach $oldaccount in oldaccounts AND $newaccount in newaccounts?
Hey powershell guru's, hoping someone can help me out here, or tell me I am off my rocker.
We have an office move coming up for a subsidiary we acquired a few years. For every user that is moving we want them to login using their credentials in our domain and not their old domain. Their old domain had a naming standard of %lastname% and our domain has a naming standard of %firstname.lastname%.
Their accounts already exist in both directories, but I want to come up with a script to copy all of their home drive data and for bonus points, maybe assign security rights for their account in our domain.
As my title suggests, what I would like to do is: $oldaccounts = gc oldaccounts.txt $newaccounts = gc newaccounts.txt
foreach $oldaccount in $oldaccounts AND $newaccount in $newaccounts { robocopy \oldserver\home$oldaccount \newserver\home$newaccount }
If there isn't the same number of items in each variable then this would probably bomb?
EDIT: Thanks for the help everyone, here is the answer: $usernames = Import-Csv e:\Admin\Scripts\powershell\old_new\accounts.csv $exe = "robocopy" $param1 = "/COPY:DAT" $param2 = "/MIR" $param3 = "/R:0" $param4 = "/W:0"
ForEach ($entry in $usernames){
$source = ("\\dphcs0\home$\" + $entry.olduser)
$dest = ("\\fsrvcdc1\home\" + $entry.newuser)
$log = ("/LOG:dph_to_cdc_" + $entry.olduser + ".txt")
echo (robocopy $source $dest $param1 $param2 $param3 $param4 $log)
}
Posted Sep 20, 2011, 2:34 PM UTC · edited
I can help out a little with the bonus point question here. Would stick around to help a bit, but my sick one year old just woke up..
Thanks! Even if I can't do what I want for the home folder copy your script will still come in useful. Good luck with the little one, best thing you can do is be there to provide comfort.
You could add another statement inside of the foreach loop: if($oldaccount -ne $null -and $newaccount -ne $null) {robocopy ...}
Something like that might help you out a little.