图像没有从一个UNC移动到另一个UNC

时间:2014-01-16 21:12:16

标签: powershell copy unc

我会将此添加到我的其他帖子中,但我找不到它。我一直在尝试将图像(JPG和JPEG)从一个UNC(源)复制到两个三天前的UNC(目的地)。我只需要复制图像,而不是复制它们所包含的文件夹。

我没有收到任何错误,从脚本收到的电子邮件告诉我没有复制图像。目的地没有复制任何图像,并且源中的图像比今天的日期早三天。我完全迷失了。我整天都在Google上试图弄清楚到目前为止无济于事。任何建议将不胜感激。

$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 = "fromme@me.com"
$AuthUser = "user"
$AuthPass = "pass"
$XHeaderInfo = "X-Header: This email was sent from Laservault."
$EmailTo = "tome@me.com"
$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
}   
}

3 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

where-object找不到任何文件的匹配项。

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

然后使用get-childitem参数将-include命令传递给-filter命令($_.Extension只接受一个文件类型,作为字符串)并删除where-object项检查来自$TheImages = get-childitem "Microsoft.PowerShell.Core\FileSystem::$ImageLocation" -recurse -include $IncludeFileTypes | where-object {$_.LastWriteTime -gt $WhenFileWritten -and $_.PsIsContainer -eq $false} 命令。

e.g。

foreach

您还在检查错误变量的计数以获取文件计数,并且它不应位于$FileCount = $TheImages.count if ($FileCount -gt 0) { foreach ($Images in $TheImages) { 循环内。

获取文件计数,然后如果它大于零,则复制文件列表,复制它们。

尝试:

write-output $TheImages >> $LogFile

还有 write-output $Images >> $LogFile

而不是
{{1}}

答案 2 :(得分: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
}

希望这能解决您的问题。