PowerShell & Robocopy

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of javier saviñon javier saviñon 8 months, 4 weeks ago.

  • Author
    Posts
  • #16549
    Profile photo of Ian
    Ian
    Participant

    I'm writing a robocopy script in PowerShell to backup everything in C:\ excluding the directories and files in excludedfiles.txt and excludeddirs.txt.

    $username = [Environment]::UserName
    $src = Get-Content '\The BUS\src.txt'
    $dest = Get-Content '\The BUS\dest.txt'
    $excludedfiles = Get-Content '\The BUS\excludedfiles.txt'
    $excludedirs = Get-Content '\The BUS\excludedirs.txt'
    
    Robocopy.exe $src $dest /MIR /IS /XD $excludedirs /XF $excludedfiles /R:3 /W:3 /NP /L /LOG+:"\The BUS\log\log.txt" /A-:SH /TEE
    Copy-Item '\The BUS\files\*' '\The BUS\'

    I'm having issues with getting powershell to read a variable from the excludeddir.txt file.

    The textfile excludeddir.txt includes

    C:\The BUS
    C:\inetpub
    C:\Intel
    C:\LOGS
    C:\MSOCache
    C:\PerfLogs
    C:\Program Files
    C:\Program Files (x86)
    C:\ProgramData
    C:\Temp
    C:\boot
    C:\Recovery
    C:\Windows
    C:\System Volume Information
    C:\Users\$username\AppData\Local\Application Data
    C:\Users\$username\AppData\Local\Microsoft\Internet Explorer
    C:\Users\$username\AppData\Local\Microsoft\Windows
    C:\Users\$username\AppData\Local\Temporary Internet Files
    C:\Users\$username\AppData\Local\ElevatedDiagnostics
    C:\Users\$username\AppData\Local\Temp
    C:\Users\$username\AppData\Local\History
    C:\Users\$username\Cookies
    C:\Users\$username\Local Settings
    C:\Users\$username\Net Hood
    C:\Users\$username\PrintHood
    C:\Users\$username\SendTo
    C:\Users\$username\Templates
    C:\Users\admaint
    C:\Users\Administrator
    C:\Users\ADMINI~1
    C:\Users\Default
    C:\Users\Default User
    C:\Users\DefaultAppPool
    C:\Users\Public
    C:\Users\All Users
    C:\Users\esantillan
    C:\Users\isantillan.old
    C:\Users\sccm_content_manager
    C:\$Recycle.Bin

    :(:(

  • #16553
    Profile photo of Simon Wåhlin
    Simon Wåhlin
    Participant

    Robocopy needs each folder that contains a space to enclosed in quotes and to run an exe-file from powershell each argument should be a string.

    Try something like this (Altho I haven't been able to test it so maybe you should add /L to only do the logging and no actual copying):

    $username = [Environment]::UserName
    $src = Get-Content '\The BUS\src.txt'
    $dest = Get-Content '\The BUS\dest.txt'
    $excludedfiles = "`"$((Get-Content 'C:\temp\excludedfiles.txt') -join '" "')`""
    $excludedirs = "`"$((Get-Content 'C:\temp\excludeddirs.txt') -join '" "')`""
     
    robocopy.exe $src $dest "/MIR" "/IS" "/XD" $excludedirs "/XF" $excludedfiles "/R:3" "/W:3" "/NP" "/L" "/LOG+:`"\The BUS\log\log.txt`"" "/A-:SH" "/TEE"
    Copy-Item '\The BUS\files\*' '\The BUS\'
    
  • #16567
    Profile photo of Ian
    Ian
    Participant

    Simon Wåhlin wrote:Robocopy needs each folder that contains a space to enclosed in quotes and to run an exe-file from powershell each argument should be a string.

    Try something like this (Altho I haven't been able to test it so maybe you should add /L to only do the logging and no actual copying):

    $username = [Environment]::UserName
    $src = Get-Content '\The BUS\src.txt'
    $dest = Get-Content '\The BUS\dest.txt'
    $excludedfiles = "`"$((Get-Content 'C:\temp\excludedfiles.txt') -join '" "')`""
    $excludedirs = "`"$((Get-Content 'C:\temp\excludeddirs.txt') -join '" "')`""
     
    robocopy.exe $src $dest "/MIR" "/IS" "/XD" $excludedirs "/XF" $excludedfiles "/R:3" "/W:3" "/NP" "/L" "/LOG+:`"\The BUS\log\log.txt`"" "/A-:SH" "/TEE"
    Copy-Item '\The BUS\files\*' '\The BUS\'

    Hello Simon, all that gives me is what i have in the text file in quotes. It's still reading the $username variable as text and not as a variable that gives me the current username.

  • #16568
    Profile photo of Ian
    Ian
    Participant

    How do i make PowerShell recognize that $username in the text file (excludeddirs.txt) is a variable and not a string?

    ——————————————————————————-
    ROBOCOPY :: Robust File Copy for Windows
    ——————————————————————————-

    Started : Thursday, June 26, 2014 2:05:26 PM
    Source : C:\
    Dest : C:\The BUS\dest\

    Files : *.*

    Exc Files : Config.Msi
    NTUSER.DAT
    UsrClass.dat
    *.sys
    *.content
    *.LOG1
    *.LOG2

    Exc Dirs : C:\The BUS
    C:\inetpub
    C:\Intel
    C:\LOGS
    C:\MSOCache
    C:\PerfLogs
    C:\Program Files
    C:\Program Files (x86)
    C:\ProgramData
    C:\Temp
    C:\boot
    C:\Recovery
    C:\Windows
    C:\System Volume Information
    C:\Users\$username\AppData\Local\Application Data
    C:\Users\$username\AppData\Local\Microsoft\Internet Explorer
    C:\Users\$username\AppData\Local\Microsoft\Windows
    C:\Users\$username\AppData\Local\Temporary Internet Files
    C:\Users\$username\AppData\Local\ElevatedDiagnostics
    C:\Users\$username\AppData\Local\Temp
    C:\Users\$username\AppData\Local\History
    C:\Users\$username\Cookies
    C:\Users\$username\Local Settings
    C:\Users\$username\Net Hood
    C:\Users\$username\PrintHood
    C:\Users\$username\SendTo
    C:\Users\$username\Templates
    C:\Users\admaint
    C:\Users\Administrator
    C:\Users\ADMINI~1
    C:\Users\Default
    C:\Users\Default User
    C:\Users\DefaultAppPool
    C:\Users\Public
    C:\Users\All Users
    C:\Users\esantillan
    C:\Users\isantillan.old
    C:\Users\sccm_content_manager
    C:\$Recycle.Bin

    Options : *.* /L /TEE /S /E /DCOPY:DA /COPY:DAT /PURGE /MIR /NP /IS /A-:SH /R:3 /W:3

  • #16573
    Profile photo of Jason Hofferle
    Jason Hofferle
    Participant

    Get-Content won't perform variable expansion, so you could try something like this:

    $excludedFiles = gc .\excludedfiles.txt | % { $_ -replace '\$Username', "$username"}

  • #16589
    Profile photo of Ian
    Ian
    Participant

    I was able to get it working by replacing the $username within excludeddirs.txt to "zzz" and then replacing the "zzz" with $env:USERNAME after calling Get-Content '\The BUS\excludeddirs.txt'

    $excludeddirs = $excludeddirs -replace "zzz", $env:USERNAME

    thanks everyone!

    /ENDTHREAD

  • #36519
    Profile photo of javier saviñon
    javier saviñon
    Participant

    Hi, i need your help to run PS Script that should do a backup using robocopy options but is not, basically i need that the script create a FOLDER named with the date of the current day, then i need that robocopy run with some options, please see my code and tell me what im doing wrong:
    $Date=(Get-date -Format d).Replace("/","-")
    robocopy "D:\Test" "F:\BK_Server\$Date" /S /E /COPYALL /V /ETA /R:10 /W:30 /LOG:"F:\BK_Server\log-$date.txt"

    Thanks a lot.

You must be logged in to reply to this topic.