Datacopier for DaRT

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datacopier for DaRT

Post by jvierra »

Here is how we would use a function to keep the code consistent and improve our ability to control the outcome of each step.

I have also attached this as a file.

Code: Select all

$jobScript = {
    Param ($user)
    
    function RunRoboCopy{
        Param($FromFolder,$ToFolder,$Options)
        
        $script:StepNo++
        Start-Process Robocopy -ArgumentList @(
        $FromFolder,
        $ToFolder,
        $Options
        ) | Out-Null
        if ($LASTEXITCODE) {
            # exit code not 0
            Write-Warning "LASTEXITCODE was $LASTEXITCODE"
        }
        Write-Output 'Step ' + $StepNo
    }
    
    $Options = '/BYTES /E /R:0 /W:0'
    $Options2 = '/BYTES /R:0 /W:0'
    $StepNo = 0
    ##Destkop
    RunRoboCopy "C:\users\$user\desktop" "\\FileShare\$user\Data\C\Users\$user\Desktop"

    ##Documents
    RunRobocopy "C:\users\$user\Documents" "\\FileShare\$user\Data\C\Users\$user\Documents" $Options + '/XD "My Pictures" "My Videos" "My Music"'
    
    ##Downloads
    RunRobocopy "C:\users\$user\Downloads" "\\FileShare\$user\Data\C\Users\$user\Downloads" $Options
    
    ##Favorites
    RunRobocopy "C:\users\$user\Favorites" "\\FileShare\$user\Data\C\Users\$user\Favorites" $Options
    
    ##Links
    RunRobocopy "C:\users\$user\Links" "\\FileShare\$user\Data\C\Users\$user\Links" $Options
    
    ##Music
    RunRobocopy "C:\users\$user\Music" "\\FileShare\$user\Data\C\Users\$user\Music" $Options
    
    ##Pictures
    RunRobocopy "C:\users\$user\Pictures" "\\FileShare\$user\Data\C\Users\$user\Pictures" $Options
    
    ##Videos
    RunRobocopy "C:\users\$user\Videos" "\\FileShare\$user\Data\C\Users\$user\Videos" $Options
    
    ##MicrosoftFeeds
    RunRobocopy "C:\Users\$user\AppData\Local\Microsoft\Feeds" "\\FileShare\$user\Data\C\Users\$user\AppData\Local\Microsoft\Feeds" $Options
    
    ##DOMStore
    RunRobocopy "C:\Users\$user\AppData\Local\Microsoft\Internet Explorer\DOMStore" "\\FileShare\$user\Data\C\Users\$user\AppData\Local\Microsoft\Internet Explorer\DOMStore" $options
    
    ##SiteList
    RunRobocopy "C:\Users\$user\AppData\Local\Microsoft\Internet Explorer\EmieSiteList" "\\FileShare\$user\Data\C\Users\$users\AppData\Local\Microsoft\Internet Explorer\EmieSiteList" $options
    
    ##EmieUserList
    RunRobocopy "C:\Users\$user\AppData\Local\Microsoft\Internet Explorer\EmieUserList" "\\FileShare\$user\Data\C\Users\$users\AppData\Local\Microsoft\Internet Explorer\EmieUserList" $options
    
    ##Mail
    RunRobocopy "C:\Users\$user\AppData\Local\Microsoft\Windows Mail" "\\FileShare\$user\Data\C\Users\$users\AppData\Local\Microsoft\Windows Mail" $Options
    
    ##Firefox
    RunRobocopy "C:\Users\$user\AppData\Roaming\Mozilla\Firefox" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Mozilla\Firefox" $Options + '/Xf *.lock'
    
    ##MicrosoftCredentials
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Credentials" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Credentials" $Options
    
    ##MicrosoftCrypto
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Crypto" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Crypto" $Options
    
    ##MicrosoftProtect
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Protect" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Protect" $Options
    
    ##MicrosoftWindowsLibraries
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Windows\Libraries" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Windows\Libraries" $Options
    
    ##Office365BuildBlocks
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Document Building Blocks\1033\16" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Document Building Blocks\1033\16" $Options2 + '/IF "Building Blocks.dotx"'
    
    ##Office365Template
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Templates" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Templates" $Options2 + '/IF Normal.dotm NormalEmail.dotm *.oft'
    
    ##OutlookRibbonQATSettings
    RunRobocopy "C:\Users\$user\AppData\Local\Microsoft\Office" "\\FileShare\$user\Data\C\Users\$users\AppData\Local\Microsoft\Office" $Options2 + '/IF *.officeUI'
    
    ##OutlookSettings
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Outlook" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Outlook" $Options + '/IF *.xml *.srs *.dat *.otm outlprnt /XF *.pst'
    
    ##Signatures
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Signatures" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Signatures" $Options
    
    ##Stationery
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Stationery" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Stationery" $Options
    
    ##CustomForms
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\Forms" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\Forms" $Options2 + '/IF *.htm'
    
    ##Dictionary
    RunRobocopy "C:\Users\$user\AppData\Roaming\Microsoft\UProof" "\\FileShare\$user\Data\C\Users\$user\AppData\Roaming\Microsoft\UProof" $Options2 + '/IF *.dic'
}

This took me a bout two minutes to do. You now can completely control what happens on each step.
Attachments
RCSteps.ps1
(5.01 KiB) Downloaded 118 times
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Datacopier for DaRT

Post by mxtrinidad »

Awesome!

The good thing here is ... you always have room for improvements.
Just make sure to do all the necessary testing make sure you get the result you expect.

:)
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Datacopier for DaRT

Post by apowershelluser »

Hey jvierra,

When I run this, I get one cmd window for each RunRoboCopy, is that what is expected?

Here is where I am.
NewJob.png
NewJob.png (170.62 KiB) Viewed 2691 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datacopier for DaRT

Post by jvierra »

The code sample I posted does not change anything your wrote. It just adds some output and simplifies the management.

To run a command with Start-Process add "-NoNewWindow" to the command.
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Datacopier for DaRT

Post by apowershelluser »

That makes sense. The write-output doesn't show up either btw.

You helped me a lot when you said 'Please understand that you will be stepping the progress bar on every timer tick. You need to only step when the job results return a step completion.'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datacopier for DaRT

Post by jvierra »

You have to receive the job in the update script to get the output. You also need to check for errors.

Run the job in PowerShell CLI and use receive-job to see what is returning. Until you understand jobs and how the JobTracker uses them you will be constantly baffled by what is happening.
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Datacopier for DaRT

Post by apowershelluser »

I'm with you and as you can see from my screenshot above I've been working in the ISE

Is there a benefit to just using powershell.exe cmd window over the ISE?

I attempted receive-job earlier but it came up blank, maybe I mistyped something.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datacopier for DaRT

Post by jvierra »

Ok. I did a bit of further testing and have found that RoboCopy breaks the job manager. I recommend that you use one job for each copy or use a workflow. This should avoid the issues caused by RC.

The other thing is to just show a timer window that shows the elapsed time and update it on each tick.

Max says don't use a job just use Start-Process. The $jobscript can be easily run as an button event and just a a "DoEvents" into the function.
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Datacopier for DaRT

Post by apowershelluser »

I'll work on this throughout the week and follow back with my findings. Thank you so much for your time.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Datacopier for DaRT

Post by jvierra »

Here is a method that does not use jobs and will not break. It is a project zipped.

You can wrap the form in a runspace and it will free up the main form to continue to work while the runspace executes the code.
Attachments
RunspaceProgressBar.zip
(83.2 KiB) Downloaded 108 times
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked