CSV Array Script Issues

by Sep 19, 2015

Hi All,

Just posting because I'm having a little trouble with a script I'm working on that will copy some of our user's Outlook Files folder to a remote server.  First, I'm storing their computer names and usernames in a csv file that has Computer and User as the headers.  Will post that below along with the scripts.  The script below works but the issue I am having is when calling the arrays during robocopy it is also trying to copy directories that don't actually exist.  Each line in the csv should ideally be grouped together and not used with any other lines, for example it is trying to copy \computer1c$usersuser2documentsoutlook files and \computer2c$usersuser1documentsoutlook files when they don't exist.  I need to make it so each line stays together and then moves to the next, so when working correctly it should only robocopy \computer1c$user1documentsoutlook files and then move on to \computer2c$users2documentsoutlook files and so on.  I've just started working with powershell over the last few weeks and this has me stumped hopefully someone can figure out what my problem is and help me improve this script 🙂

 

CSV:

Computer,User
computer1,user1
computer2,user2

Script:

$csv=import-csv c:archivebackuparchive.csv
$computers  = $csv | % { $_.computer }
$users  = $csv | % { $_."user" }
    Foreach ($computer in $computers) {
        pskill \$computer Outlook.exe
}
    Foreach ($user in $users) {
        New-Item -Force -ItemType directory -Path  \maserver"Mail Archives Vol I"$user
}
        Foreach ($user in $users) {
            Foreach ($computer in $computers) {
                robocopy "\$computerc$users$userdocumentsoutlook files" "\remoteserverMail Archives Vol I$user" /copyall /r:3
    }
}