问题
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?
回答1:
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"
来源:https://stackoverflow.com/questions/21170505/copy-item-cannot-bind-argument-to-parameter-path-because-it-is-null