locked
Robocopy powershell script with changing source. RRS feed

  • Question

  • I have to move a large amount of backups from one storage to another during off hours. I have written a script to move the files no problem but I want to improve it by making the source point at a text file where I can input the new source address daily so I don't have to get into the script everyday instead. Here is my current script.

    $source = "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Share" 
    $destination = "\\IP\Archive\7-12-2018\1"
    $source2 = "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Users" 
    $destination2 = "\\IP\Archive\7-12-2018\2" 
    $date = get-date -format M.d.yyyy 
    $logFile = "robocopy.log" 
    $logName = $date+$logFile 
    $LogDir = "\\IP\Archive\7-12-2018\"
    
    
    New-Item -Type File -Name $logName -Path $logDir -Force  
     
    ROBOCOPY $source $destination /s /COPYALL /Z /R:5 /W:30 /NFL /NDL /NP /XJ | /LOG:file $logDir\$logName
    ROBOCOPY $source2 $destination2 /s /COPYALL /Z /R:5 /W:30 /NFL /NDL /NP /XJ | /LOG+:file $logDir\$logName

    I was going to replace the source with:

    $source = get-content -path \\IP\c$\winnt\scripts\Test.txt

    but when testing this and trying to print source it would show me an error like:

    Can't find file This is just a test.

    "This is just a test." is the contents of the file. Do you know why it is telling me it can't find the file before printing the contents or is there a better way to go about something like this?


    _Sam_

    Tuesday, April 2, 2019 6:30 PM

Answers

  • ....  I meant something like this ....

    If your config file looks like this:

    Source,Destination,LogDir
    "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Share","\\IP\Archive\7-12-2018\1","\\IP\Archive\7-12-2018"
    "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Users","\\IP\Archive\7-12-2018\2","\\IP\Archive\7-12-2018"
    You code could look like this ...
    $ConfigList = Import-Csv -Path .\RobocopyConfig.csv -Delimiter ','
    $LogFileName = "{0}_robocopy.log" -f $(Get-Date -format M.d.yyyy)
    foreach ($Config in $ConfigList) {
        $LogFile = Join-Path -Path $($Config.LogDir) -ChildPath $LogFileName
        Robocopy.exe $($Config.Source) $($Config.Destination) /s /COPYALL /Z /R:5 /W:30 /NFL /NDL /NP /XJ /LOG+:$LogFile
    }
    


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, April 2, 2019 7:16 PM

All replies

  • If you like to have a config file for different copyjobs with different source folders and different target folders you will have to use a CSV file or XML file or something like this where you could put source and target next to each other.

    BTW: you don't have to create the log file in advance. robocopy will do that for you.  ;-)


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, April 2, 2019 6:45 PM
  • YOu can save a copy job as a job file in RC.  These can be stored on the source folders and you aonly need to retrive the job file and execute it.

    See RC help for creating and maintaining RC "job" files.

    RoboCopy /Job:<jobfile>

    You can also keep all job files in the same folder and just enumerate the files and pass to RC.

    Get-ChildItem *.RCJ | ForEach-Object{ RoboCopy $_.Name }


    \_(ツ)_/

    Tuesday, April 2, 2019 7:03 PM
  • ....  I meant something like this ....

    If your config file looks like this:

    Source,Destination,LogDir
    "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Share","\\IP\Archive\7-12-2018\1","\\IP\Archive\7-12-2018"
    "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Users","\\IP\Archive\7-12-2018\2","\\IP\Archive\7-12-2018"
    You code could look like this ...
    $ConfigList = Import-Csv -Path .\RobocopyConfig.csv -Delimiter ','
    $LogFileName = "{0}_robocopy.log" -f $(Get-Date -format M.d.yyyy)
    foreach ($Config in $ConfigList) {
        $LogFile = Join-Path -Path $($Config.LogDir) -ChildPath $LogFileName
        Robocopy.exe $($Config.Source) $($Config.Destination) /s /COPYALL /Z /R:5 /W:30 /NFL /NDL /NP /XJ /LOG+:$LogFile
    }
    


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, April 2, 2019 7:16 PM
  • Thanks I will try that.

    _Sam_

    Tuesday, April 2, 2019 8:20 PM
  • Hi,

    Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,
    Lee


    Just do it.

    Wednesday, April 3, 2019 5:55 AM
  • Hi,

    have you tried to put some real path to your text file like:

    "\\IP\phdvb_flr_cifs\disk-2\partition-1\Site\Share"

    This works perfectly fine for me with your code. You dont need to create any XML or job file to fulfill what you need.


    Mark as answer if it solves your issue. Leos

    Wednesday, April 3, 2019 7:06 AM