复制项:无法将参数绑定到参数“路径”,因为它为空 [英] Copy-Item : Cannot bind argument to parameter 'Path' because it is null

查看:1886
本文介绍了复制项:无法将参数绑定到参数“路径”,因为它为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道发生了什么。我是Power Shell的新手。
我有一个包含图像的文件夹结构。我正在寻找所有的图像是X天,并将这些图像复制到另外两个位置。我得到的错误是:

 复制项目:无法绑定参数参数'路径',因为它为空。 
在C:\Documents和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\\\
ewpictures
$ LiveSite =\\10.0.100.3\Variety Distributors.com\ catalog\\\
ewpictures
$ 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:此电子邮件是从Laservault发送的。
$ EmailTo =me@domain.com
$ EmailSubject =条形码图像摘要。
$ EmailBodySent =TEST附加到此电子邮件是一个日志文件,其中将从$ ImageLocation复制到$ TestSite和$ LiveSite的图像以及发送到AS2以发送到订单流。
$ EmailBodyNotSent =TEST $ ImageLocation中没有匹配$ WhenFileWritten几天的文件,所以没有发送到$ TestSite,$ LiveSite或AS2。

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

foreach($ TheImage中的TheImages){
$ FileCount = $ TheImage).count

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

write-host $ FilesCountimages 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
}
}



我会在哪里出错?

解决方案

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

  foreach($ TheImage中的TheImages){

您应该有:

  foreach($ TheImage in $ TheImages ){

---编辑



另一个问题是 Get-ChildItem 不返回任何内容。这是一个小问题,因为 -include -exclude 参数。您正在按如下方式定义变量:

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

问题是< c $ c> -include 和 -exclude 参数需要一个字符串数组( String []

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



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

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


I am not sure what is going on. I am very new to Power Shell. I have a folder structure containing images in them. I am looking to get all the images that are X days old and copy those images to two other locations. The error I am getting is:

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

My code is as follows:

$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                
}
}

Where am I going wrong?

解决方案

It's a simple problem. You flipped your variables in your foreach statement. You have:

foreach ($TheImages in $TheImage) {

You should have:

foreach ($TheImage in $TheImages) {

--- Edit

The other issue is with the Get-ChildItem not returning anything. It's a small problem because of the -include and -exclude parameters you are feeding into it. You are defining the variable like this:

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

The problem is that the -include and -exclude parameters are expecting a string array (String[]) what you are giving them is a straight string.

We can prove this, if you create a file with the name "a.jpeg,a.jpg", the get-childitem cmdlt will return the file.

The solution is to change your $IncludeFileTypes and $ExcludeFileTypes variables to a string array, and it should return the results that you are expecting:

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

这篇关于复制项:无法将参数绑定到参数“路径”,因为它为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆