• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5162
  • Last Modified:

Powershell script to call robocopy

I have a powershell v1 script that I created to backup some files across our network to a file server at a different site. The script basically takes an array as input, and calls robocopy on each value in the array. It works great for the most part, but one bit of it is giving me some trouble. The script is attached below.

The part that's giving me the actual trouble is where robocopy is called. Specifically, the switches. When I use the variable, it puts quotes around the switches, so robocopy is seeing it as an argument, rather than the switches, and is bombing as a result (Invalid Parameter #3 : "/e /zb /r:2 /w:5 /np /nfl /tee /ndl /log+:D:\Backups\Logs\Fileservers.log").

I've tried it as both a Here-string (as seen below) and a regular string, with the same result both times. If I add $switches to the end so that it displays the actual value of $switches, it shows up with no quotes.

What am I doing wrong?
#Set Variables
$Switches = @"
/e /zb /r:2 /w:5 /np /nfl /tee /ndl /log+:D:\Backups\Logs\Fileservers.log
"@
$CurrentDayOfWeek = get-date -UFormat "%A"
$FSCopyTo = "D:\Backups\$CurrentDayOfWeek\"
$FSCopyFrom = @(
	"\\netstor2\backups\Data Management Backups\Database\Alpha\BMTALPHA1\CareTrack",
	"\\netstor2\scan_pes_evals",
	"\\chstor1\home",
	"\\chstor1\I",
	"\\chstor1\vol1",
	"\\chstor1\q",
	"\\chstor1\mh-res",
	"\\chalpha2\k",
	"\\accdc1\sys",
	"\\chpaydb\harpers$",
	"\\chweb2\InetPub",
	"\\chapps1\PMA-Source\PMA",
	"\\chmepam\v",
	"\\sol65\solomon\solomon\usr_rpts",
	"\\sol65\solomon\solomon\frx67",
	"\\sol65\etsacct",
	"\\sol65\acctdata",
	"\\chweb1\InetPub",
	"\\print-serv2\d$\mssql",
	"\\print-serv2\recruittrak",
	"\\chapps2\bestserv")
 
#If destination doesn't exist, create it
if (!(Test-Path -Path $FSCopyTo))
{
New-Item $FSCopyTo -type directory
}
 
#Erase the log file
if (Test-Path -Path "D:\Backups\Logs\fileservers.log")
{
clear-content -path "D:\Backups\Logs\fileservers.log"
}
else
{
New-Item -Path "D:\Backups\Logs\fileservers.log" -type File
}
 
#Run Robocopy on each path listed in $FSCopyFrom
ForEach ($server in $FSCopyFrom) {
robocopy "$server" "$FSCopyTo" $switches
}

Open in new window

0
tilbard
Asked:
tilbard
  • 3
  • 2
1 Solution
 
Shift-3Commented:
This should do it.


#Set Variables
$Switches = @"
/e /zb /r:2 /w:5 /np /nfl /tee /ndl /log+:D:\Backups\Logs\Fileservers.log
"@
$CurrentDayOfWeek = get-date -UFormat "%A"
$FSCopyTo = "D:\Backups\$CurrentDayOfWeek\"
$FSCopyFrom = @(
        "\\netstor2\backups\Data Management Backups\Database\Alpha\BMTALPHA1\CareTrack",
        "\\netstor2\scan_pes_evals",
        "\\chstor1\home",
        "\\chstor1\I",
        "\\chstor1\vol1",
        "\\chstor1\q",
        "\\chstor1\mh-res",
        "\\chalpha2\k",
        "\\accdc1\sys",
        "\\chpaydb\harpers$",
        "\\chweb2\InetPub",
        "\\chapps1\PMA-Source\PMA",
        "\\chmepam\v",
        "\\sol65\solomon\solomon\usr_rpts",
        "\\sol65\solomon\solomon\frx67",
        "\\sol65\etsacct",
        "\\sol65\acctdata",
        "\\chweb1\InetPub",
        "\\print-serv2\d$\mssql",
        "\\print-serv2\recruittrak",
        "\\chapps2\bestserv")
 
#If destination doesn't exist, create it
if (!(Test-Path -Path $FSCopyTo))
{
New-Item $FSCopyTo -type directory
}
 
#Erase the log file
if (Test-Path -Path "D:\Backups\Logs\fileservers.log")
{
clear-content -path "D:\Backups\Logs\fileservers.log"
}
else
{
New-Item -Path "D:\Backups\Logs\fileservers.log" -type File
}
 
#Run Robocopy on each path listed in $FSCopyFrom
ForEach ($server in $FSCopyFrom) {
$command = "cmd /c echo robocopy " + [char]34 + $server + [char]34 + " " + [char]34 + $FSCopyTo + [char]34 + " " + $switches
invoke-expression $command
}

Open in new window

0
 
tilbardAuthor Commented:
That doesn't generate the error, however it doesn't seem to work still. When I run that script, it just immediately spews out the correct robocopy commands, but doesn't actually seem to run robocopy. If I cut/past the various commands it generates and run them manually from the same POSH window, it does work. Any idea what's going on?
0
 
Shift-3Commented:
I had cmd /c echo on line 48 for testing.  Remove that to execute the commands.
0
 
tilbardAuthor Commented:
Thanks! Thought I'd tried that, but I guess not. Worked perfectly!
0
 
Shift-3Commented:
I'm glad it worked out.

For future reference, EE does have a PowerShell zone, though it's inexplicably still named MSH/Monad.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now