Learner
Learner

Reputation: 990

Passing parameter to Powershell function not working

$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?

Upvotes: 0

Views: 535

Answers (1)

Frode F.
Frode F.

Reputation: 54981

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

Upvotes: 1

Related Questions