2014-01-16 35 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 
} 

希望这能解决您的问题。

相关问题