trainee
trainee

Reputation: 13

Powershell Backup Script for multiple folders

I'm having problems with a script which should function as a Backup replacement. This is only a part of the hole thing, the other stuff works but is dependent on it.

$datum = get-date -uformat "%Y-%m-%d"
$backupsrv = "\\192.168.0.5\"
$logpath = "$backupsrv\logs\$datum"
$test1 = "d:\test1","$backupsrv\b2d\test1","Test1"
$test2 = "c:\test2","$backupsrv\b2d\test2","Test2"
$programs = ($test1,$test2)

if (!(test-path -path $logpath))
{new-item $logpath -type directory}

function backup{
    param
    (
        [Parameter(Position=0,Mandatory=$true)]
        [String] $Source,

        [Parameter(Position=1,Mandatory=$true)]
        [String] $Target,

        [Parameter(Position=2,Mandatory=$true)]
        [String] $Name
    )

    if (!(test-path -path $target))
    {new-item $target -type directory}

    $LogFile = "$logpath\$name.log"

    robocopy "$Source" "$Target" /e /mir /np /ns /z /r:3 /w:30 /xf thumbs.db >>$logfile
}
foreach ($program in $programs){
    backup $program}

I always get an error with the parameter processing.

Could anyone help me with this ? Thanks!!

Upvotes: 1

Views: 1261

Answers (1)

MDMoore313
MDMoore313

Reputation: 3311

It thinks you are passing in a string[] (which you are actually) instead of three separate strings. This actually works:

foreach ($program in $programs){
    backup $program[0] $program[1] $program[2]
}

You could set your function up to accept a string array if you wanted to.

Upvotes: 1

Related Questions