这是我对PowerShell脚本的第一次非非常基础的尝试,因此请对任何不好的礼节表示歉意。
我需要将大约30GB的视频数据从USB连接的存储设备传输到本地网络共享。当我开始这个小项目时,我很快发现在脚本编写过程中需要考虑执行此任务时我自然执行的过程,所以我的问题是,如何进行全部布置并实现最终目标并允许这样做。
这是我到目前为止所拥有的;
### (The purpose of this script is to automate the offloading of the Driver Cameras during FP1 and FP2 on a Friday.
### Please remember that the cameras still need to be cleared after fireups on a Thursday"
Write-Host -NoNewLine "This script will move the footage captured on the SD cards of the FWD and RWD cameras and copy them to a defined network share" -ForegroundColor Green `n
Start-Sleep -Seconds 10
# Execute the copy from the foward facing camera to the network and remove the local files once complete
Write-Host "FWD Camera copy in progress" -ForegroundColor White -BackgroundColor Magenta `n
Start-Sleep -Seconds 5
Get-ChildItem -Path "SourcePath" -Recurse |
Move-Item -destination "DestinationPath" -Verbose
# Execute the copy from the rearward facing camera to the network and remove the local files once complete
Write-Host "RWD Camera copy in progress" -ForegroundColor White -BackgroundColor Magenta `n
Start-Sleep -Seconds 5
Get-ChildItem -Path "SourcePath" -Recurse |
Move-Item -destination "DestinationPath" -Verbose
Write-Host "Sending email confirmation" -ForegroundColor Green `n
Send-MailMessage -smtpserver ServerIP -To "EmailAddress" -From "EmailAddress" -Subject "Camera offload" -body BodyHere -bodyasHTML
Write-Host "All tasks have completed" -ForegroundColor Green `n
Read-Host "Press any key to exit..."
exit
我要添加的是容错能力,并允许通过电子邮件动态传达这一点。在下面找到这些条件;
最后,还有其他我遗漏的常识参考文献,需要将其包括在内吗?
在此先感谢您的帮助。
答案 0 :(得分:0)
这个话题有点宽泛,但是让我尝试解决您的问题以帮助您开始。当然,我不会给您完整的代码,只是解释使用什么以及为什么。
将存储设备连接到运行脚本的计算机的电缆有可能会断开连接,并且仅移动了许多项目,我是否可以添加一些东西来辅助这样做?
首先,正如vonPryz在评论中所说,请使用robocopy!
它应该能够在网络中断后幸存下来(例如,检查this post)。通常,在删除内容之前,我首先要确保已成功复制内容。例如,您可以使用以下内容:
(Get-ChildItem -Recurse).FullName.Replace("C:\old\path","D:\new\path") | Test-Path
当然,以上内容只会检查文件是否存在,而不是文件内容是否相同。要比较文件是否相同,可以使用Get-FileHash
。
如果文件传输失败,我如何重新启动并进行跟踪?我可以添加一个循环以确认所有项目都已移动吗?
上面已部分回答。 Robocopy具有内置此功能。是的,您可以添加一个循环进行检查。
如何引用故障代码以动态更新发送给最终用户的电子邮件的内容?
检查示例from here:
robocopy b:\destinationdoesnotexist C:\documents /MIR
if ($lastexitcode -eq 0)
{
write-host "Success"
}
else
{
write-host "Failure with exit code:" $lastexitcode
}
此外,还有article on MS side listing all exit codes可能有助于处理异常。您要做的就是将$LASTEXITCODE
添加到电子邮件正文。