我是 PowerShell 新手,我已成功通过 google 搜索方式将以下 PowerShell 脚本组合在一起:
#System Variable for backup Procedure
$date = Get-Date -Format d.MMMM.yyyy
New-PSDrive -Name "Backup" -PSProvider Filesystem -Root "\\server>\<path>"
$source = "\\server>\<path>"
$destination = "\\<server>\<path>\$date"
$path = test-Path $destination
电子邮件变量
$smtp = "smudmug.mug"
$from = "James Clegg <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33565e525a5f73565e525a5f1d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>>"
$to = "James Clegg <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41242c20282d01242c20282d6f222e2c" rel="noreferrer noopener nofollow">[email protected]</a>>"
$body = "Hi All, </br></br> Please find the back up log File for <Server> attached.</br></br> Back up complete on $date</br></br>Kind Regards,</br></br> James"
$subject = "Backup of <Server> is Complete $date"
备份进程已开始
if ($path -eq $true) {
write-Host "Folder Already exists"
Remove-PSDrive "Backup"
} elseif ($path -eq $false) {
cd backup:\
copy-Item -Recurse $source -passthru -Destination $destination
$backup_log = Dir -Recurse $destination | out-File "$destination\backup_log.txt"
$attachment = "$destination\backup_log.txt"
#Send an Email to User
send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Attachments $attachment -Body $body -BodyAsHtml
write-host "Backup Successful"
cd c:\
Remove-PSDrive "Backup"
}
cmd /c pause | out-null
有人可以帮助我,我需要添加什么才能允许复制正在使用的文件。
亲切的问候,
詹姆斯
最佳答案
复制在另一个应用程序中打开的文件并非易事。问题是,即使您可以复制该文件,也不能保证该文件处于可用状态。除了最简单的文本文件之外,所有文件都很容易损坏。考虑以下场景:
user backup
| |
(saves doc) |
| |
(write n chars) |
| (read file)
(write m chars) (write copy)
| |
期望的结果是什么?从用户的角度来看,文档应该包含保存之前或之前的状态。实际结果只是一些保存的更改,很难正确。如果文件的结构更复杂(例如电子表格),则结果可能会损坏且无法修复。
为了解决这种并发问题,出现了打开文件代理。在Windows中,有一个内置的VSS(卷影复制服务),也存在第3方产品。这个想法是观察文件的事件,并在所有更改都刷新到磁盘时制作一致的副本。这也需要应用程序的配合。有关 VSS 的详细讨论,read Technet文章。最好的选择是使用已经解决了该问题的现有备份解决方案。
关于email - 在 Powershell 中复制打开的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31064272/