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 有file1.txt 所以备份函数的第一个参数是file1.txt。函数备份有两个参数,但第一个参数获取两个传递的值。这里有什么问题?

4

1 回答 1

1

问题在于backup您可能已经猜到了调用 - 方法的方式。

在 PowerShell 中,您不需要括号,而是用空格“”分隔参数。此外,逗号,用于创建数组。

因此,在上面的示例中,您将equal 设置为带有和作为对象$source的数组。$dir$backupfolder

尝试:

backup $dir $backupfolder
于 2013-03-19T16:48:12.833 回答