2014-01-16 176 views
0

我不確定發生了什麼。我對Power Shell很新。 我有一個文件夾結構,其中包含圖像。我正在尋找所有X天的圖像,並將這些圖像複製到另外兩個位置。我得到的錯誤是:Copy-Item:無法將參數綁定到參數'Path',因爲它爲空

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 = "[email protected]" 
$AuthUser = "user" 
$AuthPass = "pass" 
$XHeaderInfo = "X-Header: This email was sent from Laservault." 
$EmailTo = "[email protected]" 
$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     
} 
} 

我要去哪裏錯了?

+0

不應該在foreach循環開始是這樣的:'的foreach( $ TheImage in $ TheImages)'? – iCodez

回答

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" 
相關問題