2014-01-16 36 views
1

我會將此添加到我的其他帖子,但我無法找到它。我一直在試圖從一個UNC(源)複製圖像(JPG和JPEG)到三天內的兩個UNC(目的地)。我只需要複製圖像,而不是它們包含的文件夾。圖像不從一個UNC移動到另一個UNC

我沒有收到任何錯誤,從腳本收到的電子郵件告訴我沒有圖像被複制。目的地沒有任何圖像複製到它們,並且源圖像中的圖像比今天的日期早三天。我完全失去了。我一直在谷歌上試圖找出這個目前無濟於事。任何建議,將不勝感激。

$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 = (get-date).AddDays(-3) 
$IncludeFileTypes = "*.jpeg,*.jpg" 
$LogFile = "C:\Temp\FilesCopied.log" 

$EmailServer = "emailserver.me.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 = "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 = "There were no files in $ImageLocation that matched $WhenFileWritten days old. So, nothing was sent to $TestSite, $LiveSite or AS2." 

$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse | where-object {$_.Extension -eq $IncludeFileTypes -and $_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false} 

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

if ($FileCount -gt 0) { 
    copy-item -path $Images.FullName -destination "Microsoft.PowerShell.Core\FileSystem::$TestSite" -force 
    copy-item -path $Images.FullName -destination "Microsoft.PowerShell.Core\FileSystem::$LiveSite" -force 

    write-host "$FileCount files were copied." 
    write-output $Images >> $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

你的原帖:Copy-Item : Cannot bind argument to parameter 'Path' because it is null

我抄我的答案從那裏這裏:


的問題是與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" 
0

where-object沒有找到任何匹配的任何文件。

修改$IncludeFileTypes = "*.jpeg,*.jpg"$IncludeFileTypes = @("*.jpeg","*.jpg")

使用-include參數(-filter只需要一個文件類型,作爲字符串)然後通過文件類型的陣列到get-childitem命令並從where-object命令$_.Extension檢查。

例如

$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse -include $IncludeFileTypes | where-object {$_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false} 

您還檢查了錯誤的變量的數量,讓您的文件數量,它不應該是foreach循環中。

獲取文件計數,如果大於零,則遍歷列表(如果有文件),將其複製。

嘗試:

$FileCount = $TheImages.count 
if ($FileCount -gt 0) { 
foreach ($Images in $TheImages) { 

而且還 write-output $TheImages >> $LogFile


write-output $Images >> $LogFile

+0

現在,我沒有收到電子郵件,也沒有將文件從源UNC複製到目標UNC。這是我的: – user3013729

+0

$ TheImages = get-childitem「Microsoft.PowerShell.Core \ FileSystem :: $ ImageLocation」-include $ IncludeFileTypes -exclude $ ExcludeFileTypes -recurse |對象{$ _。LastWriteTime -gt $ WhenFileWritten和$ _。PsIsContainer -eq $ false} – user3013729

+0

$ IncludeFileTypes =「* .jpeg」,「*。jpg」 $ ExcludeFileTypes =「* .psd」,「* .tif「,」*。pdf「 – user3013729

0

相反,你在聲明列表錯了,應該是這樣的:

$IncludeFileTypes = @("*.jpeg","*.jpg") 

嘗試使用-Include這樣的搜索:

$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse -Include $IncludeFileTypes | where-object {$_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false} 

,並檢查變量之後空:

if($TheImages -ne $null) 
{ 
#move... 
#send email... 
#etc 
} 

希望這能解決您的問題。

相關問題