Copy-Item:无法将参数绑定到参数'Path',因为它为null

时间:2014-01-16 18:59:03

标签: powershell copy

我不确定发生了什么。我是Power Shell的新手。 我有一个包含图像的文件夹结构。我希望获得X天以前的所有图像并将这些图像复制到其他两个位置。我得到的错误是:

Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Documents and Settings\Administrator.VDICORP\Desktop\ProcessOSImages.ps1:37 char:24
+         copy-item -path <<<<  $TheImage -destination $TestSite 
+ CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

我的代码如下:

$TodaysDate = get-date
$FileAgeInDays = "-2"
$ImageLocation = "\\vdifiles\Print-Room\FileSrv\Barcode-Order"
$TestSite = "\\10.0.100.3\www2.varietydistributors.com\catalog\newpictures"
$LiveSite = "\\10.0.100.3\Variety Distributors.com\catalog\newpictures"
$WhenFileWritten = $TodaysDate.AddDays($FileAgeInDays)
$IncludeFileTypes = "*.jpeg,*.jpg"
$ExcludeFileTypes = "*.psd,*.tif,*.pdf"
$LogFile = "C:\Temp\FilesCopied.log"

$EmailServer = "mx1.domain.com"
$EmailFrom = "alerts@domain.com"
$AuthUser = "user"
$AuthPass = "pass"
$XHeaderInfo = "X-Header: This email was sent from Laservault."
$EmailTo = "me@domain.com"
$EmailSubject = "Barcode Images Summary."
$EmailBodySent = "TEST Attached to this email is a log file of what images were copied from $ImageLocation to $TestSite and $LiveSite as well as sent to AS2 to send over to Order Stream."
$EmailBodyNotSent = "TEST There were no files in $ImageLocation that matched $WhenFileWritten days old. So, nothing was sent to $TestSite, $LiveSite or AS2."

$TheImages = get-childitem $ImageLocation -include $IncludeFileTypes -exclude $ExcludeFileTypes -recurse -force | where-object {$_.CreationTime -le "$WhenFileWritten" -and $_.PsIsContainer -ne $true}

foreach ($TheImages in $TheImage) {
$FileCount = ($TheImage).count

if (!($FileCount -eq 0)) {
    copy-item -path $TheImage -destination $TestSite 
    copy-item -path $TheImage -destination $LiveSite 

    write-host $FilesCount "images were copied."
    write-output $TheImage >> $LogFile
    \\vdifiles\blat$\blat.exe -attach $LogFile -to $EmailTo -s $EmailSubject -i $EmailFrom -body $EmailBodySent -server $EmailServer -u $AuthUser -pw $AuthPass -f $EmailFrom -x $XHeaderInfo
    remove-item $LogFile
} else {
    \\vdifiles\blat$\blat.exe -to $EmailTo -s $EmailSubject -i $EmailFrom -body $EmailBodyNotSent -server $EmailServer -u $AuthUser -pw $AuthPass -f $EmailFrom -x $XHeaderInfo                
}
}

我哪里错了?

1 个答案:

答案 0 :(得分:1)

这是一个简单的问题。您在foreach语句中翻转了变量。你有:

foreach ($TheImages in $TheImage) {

你应该:

foreach ($TheImage in $TheImages) {

---编辑

另一个问题是Get-ChildItem没有返回任何内容。这是一个小问题,因为您正在加入-include-exclude参数。您正在定义这样的变量:

$IncludeFileTypes = "*.jpeg,*.jpg"
$ExcludeFileTypes = "*.psd,*.tif,*.pdf"

问题是-include-exclude参数期望一个字符串数组(String[])你给它们的是一个直字符串。

我们可以证明这一点,如果您创建名为"a.jpeg,a.jpg"的文件,get-childitem cmdlt将返回该文件。

解决方案是将$IncludeFileTypes$ExcludeFileTypes变量更改为字符串数组,它应该返回您期望的结果:

$IncludeFileTypes = "*.jpeg","*.jpg"
$ExcludeFileTypes = "*.psd","*.tif","*.pdf"