0
$backupfolder = "\\server\somefolder1\somefolder2\"
$localpath="C:\Documents and Settings\myname\My Documents\FilestoCopytoServer\folder1\"

foreach ($dir in $localpath)
{
    write-host $dir
    if(test-path $dir.fullname -pathtype container)
        {
            write-host "it's a directory"
            write-host "check if this directory is in the remote machine or not"
            $direxists = test-path $remotepath\$dir -pathtype container
                if($direxists)
                {
                write-host "remote machine has this directory"
                recurseandcopyfiles($dir)

            }
             else
            {
            write-host "directory does not exist so create directory"

            createdir($remotepath,$dir)


            }

    }
    else
    {
        write-host "it's not a container so copy files"
        write-host $dir "and backup folder is " $backupfolder

        backup( $dir,$backupfolder)


      }
}

function backup($source,$destination)
{
write-host "inside backup"
write-host "source is " $source "and destination" is $destination
#this is showing I'm getting following: 
# source is  file1.txt \\server\somefolder1\somefolder2\
}

$localpath has file1.txt so, the first parameter to backup function is file1.txt. Function backup has two parameters but the first parameter gets both the passed values. What's the problem here?

1 Answer 1

1

The problem is that way you're calling the backup-method as you probably guessed.

In PowerShell, you don't need the parantheses and you seperate arguments with a space " ". Also, comma , is used to create an array.

So in your sample above you set $source equal an array with $dir and $backupfolder as objects.

Try:

backup $dir $backupfolder
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.