【问题标题】:copy-item cannot bind argument to parameter 'path' because it is null复制项无法将参数绑定到参数“路径”,因为它为空
【发布时间】:2013-10-05 00:57:10
【问题描述】:

这个让我难过。我已经搜索过“copy-item cannot bind argument to parameter 'path' because it is null”,并找到了几篇帖子,其中的答案涉及修复语法以使第一个参数不为空。据我所知,我的第一个参数不为空,并且类型也正确,但我仍然遇到同样的错误。我也有一个非常相似的脚本在另一台服务器上运行而没有问题,这就是我开发它的模板。

脚本位于名为“weekly-archive.ps1”的文件中,并由“.\weekly-archive.ps1”在 PowerShell 控制台中调用。

我在 Windows Server 2008 R2 上运行 PowerShell 2.0

这里是有问题的脚本段,在使用前添加了额外的东西来打印变量:

$pathtosource = "\\domainname\backup\servers\windowsimagebackup\"
$pathtodest = "G:\backup\servers\"
$images = Get-ChildItem $pathtodest
foreach ($_ in $images)
{
    $sourcepathname = $pathtosource + $_
    $destpathname = $pathtodest + $_
    $logresult += "`tSaving '" + $sourcepathname + "' to '" + $destpathname + "' at " + (Get-Date).ToShortTimeString() + ".`n"
    $sourcepathname
    $sourcepathname.GetType()
    $pathtodest
    $pathtodest.GetType()
    Copy-Item $sourchpathname $pathtodest -recurse
    $count += 1
}

这是 $images 中第一个 $_ 的结果输出,表明两个参数都不为空,两个参数实际上都是字符串:

PS D:\Administration> .\weekly-archive.ps1
\\domainname\backup\servers\windowsimagebackup\DC-1

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
G:\backup\servers\
True     True     String                                   System.Object
Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\Administration\weekly-archive.ps1:80 char:12
+         Copy-Item <<<<  $sourchpathname $pathtodest -recurse
    + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

我还尝试使用“-path”和“-destination”标志,因为 AFAIK 是可选的。如果我用文字字符串替换脚本中 copy-item 行中的 '$sourcepathname' ,则不会出现错误。

最后,直接在 PowerShell 控制台中输入的以下行完美运行:

$sourcepathname = "\\domainname\backup\servers\windowsimagebackup\DC-1"
$pathtodest = "G:\backup\servers\"
copy-item $sourcepathname $pathtodest -recurse

所以,我对“$sourcepathname”的使用显然有问题,但我找不到。请不要犹豫,证明我的无知......

【问题讨论】:

    标签: powershell powershell-2.0 copy-item


    【解决方案1】:

    脚本的 copy-item 行有错字。你有$sourchpathname,它应该是$sourcepathname。

    【讨论】:

    • 啊。我知道我检查了几次拼写。很抱歉因为这么明显的错误打扰大家。 (偷偷溜走……)
    • 您可以打开严格模式来查找和避免此类问题。见technet.microsoft.com/en-us/library/hh849692.aspx
    • 感谢@Lars 指出严格模式。试了一下,看起来会有帮助。
    猜你喜欢
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    相关资源
    最近更新 更多