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

511
jklmnb•

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..

2
simulated_conscienceOP•

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.

1
jklmnb•

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.

1
assangeleakinglol•

Can't you just line them up in a CSV file?

2
simulated_conscienceOP•

step 3 profit?

1
assangeleakinglol•

Loop through each row and use $a[1] as oldprofile and $a[2] as newprofile? I'm not sure if it would work for you, but you know how your data is formated. You could probably sort them in excel pretty easy if not.

1
SuperCow1127•

What you really want to do here is have a more explicit mapping between $oldaccounts and $newaccounts. How do you know the order will be the same? How about if there's a new account but no old account for a user? Vice versa?

Another commenter suggested this, and it's really the best option. Make a CSV that looks like the below (you can just copy and paste each file into Excel columns and save to .csv), then use Import-Csv instead of Get-Content.

olduser,newuser
plant,robert.plant
page,jimmy.page
bonham,john.bonham

Then, you can do the following:

$usernames = Import-Csv usernames.csv
ForEach ($user in $usernames) { echo ($user.olduser + " " + $user.newuser) }
 
2
simulated_conscienceOP•

Yes! This did exactly what I wanted, and I learned something new :)

P.S. it had to be: ForEach ($user in $usernames) { echo ($user.olduser + " " + $user.newuser) }

3
SuperCow1127•

What am I missing in your P.S.?

2
simulated_conscienceOP•

oh actually nothing, I guess I am missing the ability to read and copy/paste.

Unless you edited in the + " " +, originally I think it was + " " $user.newuser

1
PeachWombat•

There's a Join-Object function that might help. Care must be taken to assure key uniqueness.

1