Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将参数传递给Powershell函数不起作用_Powershell - Fatal编程技术网

将参数传递给Powershell函数不起作用

将参数传递给Powershell函数不起作用,powershell,Powershell,$localpath有file1.txt,因此,备份函数的第一个参数是file1.txt。 函数备份有两个参数,但第一个参数获取两个传递的值。这里有什么问题 问题在于,您调用的是您可能猜到的备份方法 在PowerShell中,您不需要parantises,您可以用空格“”分隔参数。另外,逗号,用于创建数组 因此,在上面的示例中,您将$source设置为一个数组,其中$dir和$backupfolder作为对象 尝试: $backupfolder = "\\server\somefolder1\s

$localpath有file1.txt,因此,备份函数的第一个参数是file1.txt。
函数备份有两个参数,但第一个参数获取两个传递的值。这里有什么问题

问题在于,您调用的是您可能猜到的
备份方法

在PowerShell中,您不需要parantises,您可以用空格“”分隔参数。另外,逗号
用于创建数组

因此,在上面的示例中,您将
$source
设置为一个数组,其中
$dir
$backupfolder
作为对象

尝试:

$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\
}
backup $dir $backupfolder