简体   繁体   English

在 Power shell 中复制打开的文件

[英]Copying open files in Power shell

i am new to PowerShell and i have managed to google my way to getting the following PowerShell Script together:我是 PowerShell 的新手,我设法通过谷歌搜索了以下 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

Email Variables电子邮件变量

 $smtp = "smudmug.mug"
 $from = "James Clegg <email@email.com>"
 $to = "James Clegg <email@email.com>"
 $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"

Backup Process started备份过程开始

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

Can someone please help me which what i need to add to allow files in use to be copied.有人可以帮助我需要添加哪些内容以允许复制正在使用的文件。

Kind regards,亲切的问候,

James詹姆士

Copying files that are open in another an application is not trivial.复制在另一个应用程序中打开的文件并非易事。 The problem is that even if you could copy the file, there is no guarantee that the file is in usable a state.问题是即使您可以复制文件,也不能保证文件处于可用状态。 All but the most simple text files are easily corrupted.除了最简单的文本文件外,其他所有文件都很容易损坏。 Consider the following scenario:考虑以下场景:

   user           backup
    |                |
 (saves doc)         |
    |                |
(write n chars)      |
    |             (read file)   
(write m chars)   (write copy)
    |                |

What is the desired outcome?想要的结果是什么? From user's point of view, the document should contain either state before save or before it.从用户的角度来看,文档应该包含保存之前或之前的状态。 The actual outcome, only some saved changes, is hardly correct.实际结果,只有一些保存的更改,几乎不正确。 If the file is more complex a structure, like a spreadsheet, the result can be corrupted beyond repair.如果文件的结构更复杂,例如电子表格,则结果可能会损坏而无法修复。

In order to work around this kind of concurrency issue, ther are open file agents.为了解决这种并发问题,有打开文件代理。 In Windows, there is a built-in VSS (Volume Shadow Copy Service), 3rd party products exist too.在 Windows 中,有一个内置的 VSS(卷影复制服务),也存在 3rd 方产品。 The idea is to watch the file for activity and make a consistent copy when all changes are flushed to the disk.这个想法是观察文件的活动并在所有更改刷新到磁盘时制作一致的副本。 This requires co-operation from the application too.这也需要应用程序的合作。 For a detailed discussion about VSS, read Technet article.有关 VSS 的详细讨论,请阅读 Technet文章。 Your best bet is to use an existing backup solution that already has tackled the issue.最好的办法是使用已经解决了该问题的现有备份解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM