Script to move selected user folders - HELP PLEASE

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Andy Feeney Andy Feeney 2 years ago.

  • Author
    Posts
  • #20602
    Profile photo of Andy Feeney
    Andy Feeney
    Participant

    Good Evening,

    I wonder if someone would look over this issue for me, this is my first complex script

    Long story short, built new 2012 RDS Farm and need to move selected user files (Desktop, Documents) from the old terminal servers to a central repository where it can be GP mapped as a drive so the users can choose what they want to keep and remove after a month or so.

    So, with PS I have extracted a list users from AD and created a folder for each user with sub-folders named as each of the old terminal servers on a central repository – EXAMPLE

    fred.bloggs
    terminal01
    terminal02
    john.bloggs
    terminal01
    terminal02

    Now I need to get the user files from the selected folders on the old terminal servers and copy them to the correct user folder and respective old terminal server – EXAMPLE

    \\terminal01\C$\Users\fred.blogs\Desktop\* copies over to \\newrepository01\old_ts_data\fred.blogs\terminal01\Desktop

    I have written a PS script to import a csv with a full list of our AD users in first.last format all in one column with a heading of USER – EXAMPLE

    USER
    fred.blogs
    john.bloggs
    peter.bloggs

    the script then sets the old and the new path and Ive used Join-Path and Copy-Item to get the contents over
    This all works nicely except its not creating the folder "Desktop" and, more annoyingly, only getting the last name in the list and I really don't understand why.

    I am probably doing something overcomplicated or just missing something stupid.
    Can someone please look at this and see if they can fathom it out for me. (have replaced real path names to make the above examples work)

    $listPath = "C:\Temp\test.csv"
    $username = ($line.USER)
    $old = "\\terminal01\C$\Users\$username\Desktop\*"
    $new = "\\newrepository\G$\old_ts_data\$username\terminal01\"
    $list = import-csv $listPath
    foreach($line in $list)
    {
    $path = Join-Path $old -childpath $user.username
    copy-item $path $new -recurse -force
    }

    Kindest Regards

    Andy Feeney

  • #20606
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    One thing that jumps out is that you can't initialize your variables outside of the loop body like that. Your $old, $new and $username variables are not what you think they are, inside the loop, at this point.

    Also, you're referring to a non-existent variable $user inside the loop. I think this is closer to what you're trying to do:

    $listPath = "C:\Temp\test.csv"
    $list = import-csv $listPath
    
    foreach($line in $list)
    {
        $username = $line.USER
        $old = "\\terminal01\C$\Users\$username\Desktop"
    
        if (Test-Path -LiteralPath $old -PathType Container)
        {
            $new = "\\newrepository\G$\old_ts_data\$username\terminal01\Desktop\"
                
            if (-not (Test-Path -LiteralPath $new -PathType Container))
            {
                try
                {
                    $null = New-Item -Path $new -ItemType Directory -ErrorAction Stop
                }
                catch
                {
                    Write-Error -ErrorRecord $_
                    continue
                }
            }
    
            Copy-Item $old\* $new -Recurse -Force
        }
    }
    

    I cleaned up the variables to match the sample CSV you posted, and added some sanity checks and error handling. See if this works, and if not, let me know what's happening.

  • #20607
    Profile photo of Andy Feeney
    Andy Feeney
    Participant

    Hi Dave,

    Thanks for helping, really appreciated.
    Ive ran the script with the correct paths (previously changed for examples)
    It errors with the following

    At line:30 char:2
    +
    + ~
    Missing type name after '['.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingTypename

    If I remove the / in it runs with no errors but leaves a \* after the prompt and doesn't copy any files.

    Thank you for you help on this

    Kindest Regards

    Andy Feeney

  • #20610
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    The pre and /pre tags are what are used to display code on the forums here. You shouldn't be copying (or even seeing) that, but maybe they show up in email or something. If you click the link to view this thread on the forums, you'll be able to see and copy just the code that you need.

  • #20622
    Profile photo of Andy Feeney
    Andy Feeney
    Participant

    Morning Dave,

    Ok... hands up.. im an idiot, I realised when I looked at the reply on here.
    Subsequently removed the Pre and testing today, will let you know how i get on

    Kindest Regards

    Andy

  • #20623
    Profile photo of Andy Feeney
    Andy Feeney
    Participant

    Hi Dave,

    Just want to let you know that worked perfectly, have added some more to it to pull Desktop content and Document content in one go.
    Just need to work out how to exclude "My Music"

    Really very grateful for your help with this.

    Best Regards

    Andy

You must be logged in to reply to this topic.