我在PowerShell中的自定义功能不像我期望的那样工作

时间:2015-03-11 12:52:30

标签: powershell

function CopyFiles($source, $destination)
{
    Write-Host "Source is " $source
    Write-Host "Destination is " $destination
    Copy-Item -Force -Recurse –Verbose $source -Destination $destination
}

调试时,我的源代码似乎是将源和目标组合成一个字符串。是C:\快捷方式* \ Server \ Folder \ User \ person \ Desktop

目的地看起来空了

我正在使用看起来像

的代码进行函数调用
CopyFiles $source, $serverPath

任何人都可以解释我做错了什么或我如何解决它?

1 个答案:

答案 0 :(得分:3)

首先,你的字符串引用错误。它应该是:

function CopyFiles($source, $destination)
{
    Write-Host "Source is $source"
    Write-Host "Destination is $destination"
    Copy-Item -Force -Recurse –Verbose $source -Destination $destination
}

其次,您正在错误地调用该函数。它应该是

CopyFiles $source $serverPath

您执行此操作的方式实际上是将$source函数参数作为字符串数组传递。这就是为什么你看到它们打印在一起而目的地是空的,你实际上并没有传递$destination参数