0

I have a batch file that does a robocoy from serverA to serverB and it works very well.

I have a list of server names. I'd like to load this list into a powershell array and then enumerate over this array and run the robocopy using the path.

This is my working robocopy batch file:

ROBOCOPY C:\Websites\ \\serverB\C$\Websites\ /MIR

What I'd like to do is replace "serverB" with the value from the array. I've never really used powershell before, so I don't even know where to begin besides installing powershell on the machine ;)

In theory, this is what I'd like to have (psudo-code):

$serverNames = ("serverA","serverB", "serverC")
foreach ($server in $serverNames ) 
{ 
    "ROBOCOPY C:\Websites\ \\" + $server + "\C$\Websites\ /MIR"
}

I guess, the question is, how do I execute what would normally be a batch command from within powershell?

Thanks for any help!

3 Answers 3

2
$serverNames = ("serverA","serverB", "serverC")
foreach ($server in $serverNames ) 
{ 
    ROBOCOPY.exe C:\Websites\ \\$server\C$\Websites\ /MIR
}

Make sure that Robocopy is in the path. Remeber that the current working is not part of the path in PS so if robocopy is in the current working directory, prepend it with .\ (.\Robocopy.exe...). For executeables you need to specify the .exe on the file name.

If your list of servers is not too long you can one line it too (this one assumes robocopy is in the current working dir):

("serverA","serverB", "serverC") | %{.\Robocopy.exe C:\Websites\ \\$_\Websites\ /MIR}
1

EBGreen's answer is the right one. However, if all you want to do is execute a string, you can

Invoke-Expression "ROBOCOPY C:\Websites\ \" + $server + "\C$\Websites\ /MIR"

Or

& "ROBOCOPY C:\Websites\ \" + $server + "\C$\Websites\ /MIR"

Or

cmd /c "ROBOCOPY C:\Websites\ \" + $server + "\C$\Websites\ /MIR"

0

@EBGreen,

I don't have enough points to comment I guess. I don't know what happened to my points from stackoverflow... I started at serverfault with over 100 points from stack, then they dropped to 11 or so.

I don't have access to v1, but in v2 at least: PS>get-alias set

CommandType Name Definition ----------- ---- ---------- Alias set Set-Variable

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .