【问题标题】:Scan C disk and copy files扫描C盘并复制文件
【发布时间】:2017-06-08 08:20:19
【问题描述】:

我希望能得到一些帮助。

  1. Powershell 脚本应关闭有效的 Outlook 进程。

  2. 以及扫描 C 盘以查找有效的 .pst 文件。

  3. 将这些文件复制到“\fileserver01\temp\test\”

  4. 导出到 csv/excel 列表中这些文件的位置和上次写入时间。

  5. 运行脚本时可能会为用户隐藏错误消息,因为它抱怨在运行扫描时无法完全访问一些文件夹。

代码:

Get-Process outlook | Foreach-Object { $_.CloseMainWindow() }

Get-ChildItem -path c:\ -recurse -include *.pst | `
Copy-Item -destination "\\fileserver01\temp\test\" | `
Select-object fullname,lastwritetime|export-csv "\\fileserver01\temp\test\"

我应该如何解决我清单上的最后几件事? 谢谢

【问题讨论】:

  • 对于 1. 在您的列表中,您可以使用 Stop-Process -Name Outlook*
  • 别忘了标记正确的答案。

标签: powershell copy backup restore


【解决方案1】:

首先,您必须对 UNC 路径使用双反斜杠。 其次,copy-item不向管道输出任何东西,你必须使用-Passthru参数。

Get-ChildItem -path z:\ -recurse -include *.pst  -PipelineVariable source | 
    Copy-Item -Destination "\\path\temp" -Verbose -PassThru | 
    Select-Object @{n="Source";e={$source.versioninfo.filename}},fullname,lastwritetime | export-csv "\\path\temp\copy_result.csv" -Append -Verbose

【讨论】:

  • 非常感谢!我结束了这个。我还剩下一件有点奇怪的东西。在 csv/excel 文件中,它说全名是目标位置。我希望输入源代码。不过,lastwritetime 是正确的。
  • 我更新了我的示例以使用管道变量。现在它在 .csv 中输出源、目标(全名)和 lastwritetime。
  • 谢谢。但是,它似乎不起作用。我在 Powershell 2.0 中编码
  • 哦,在那种情况下……不知道。我是针对 4.0 版的。唯一改变的是管道变量和 Select-Object 中脚本块的使用。唯一的其他选择是将每个步骤放在另一个变量中并对其进行迭代,或者将源需求放在 .csv 文件中。
【解决方案2】:

我认为问题在于复制文件后,对象从管道中消失了。

这行得通:

Get-ChildItem -Path C:\ -Include *.pst -ErrorAction SilentlyContinue | Select-Object FullName, LastWriteTime | Export-Csv -Path "\fileserver01\temp\test\MyCSV.csv"

【讨论】:

    【解决方案3】:

    这并不能直接回答您提出的问题,因为@Adamar 的回答似乎就是这样做的。

    但是,您的问题也可以通过使用 sn-p 从注册表中查询 ost/pst 文件来解决,如下所示:

    (Get-ChildItem HKCU:\Software\Microsoft\Office\16.0\Outlook\Search).Property | ? {$_ -match "(o|p)st$"}
    

    这将返回登录用户在 Outlook 中打开的所有 ost/pst 文件。

    这样的 sn-p 会将它们全部复制到网络共享并将日志打印到文件中。

    $FilesToCopy = (Get-ChildItem HKCU:\Software\Microsoft\Office\16.0\Outlook\Search).Property | ? {$_ -match "(o|p)st$"}
    $FilesToCopy | ForEach { Copy-Item -Path $_ -Destination "\\network\share" ; $_ | Out-File "\\network\share\log.txt" -Append }
    

    这节省了很多时间来索引所有C:驱动器-还有一个问题是Get-ChildItem没有正确索引很长的目录名称(大于260个字符长度)-使这种方法有点更可靠,更适合制作脚本。

    【讨论】:

    • 这是否也适用于未在 Outlook 中导入的 ost/pst?由于索引在工作中完全禁用 (VDI),因此无法检查自己。
    • 谢谢康纳。如果我注意到这需要很多时间,我会记住这一点。我是这个论坛的新手,我很高兴得到你们的支持。
    • @Adamar 据我所知,这仅列出了任何 Outlook 配置文件中的 PST,您需要执行完整的 FS 索引才能找到其他人。
    【解决方案4】:

    这是最终代码。 感谢大家的支持。

    #Kill Oulook process
    Get-Process outlook -ErrorAction SilentlyContinue |   Foreach-Object { $_.CloseMainWindow() } 
    
    #Scan for .pst files on the C disk
    Get-ChildItem -path c:\ -recurse -include *.pst -ErrorAction SilentlyContinue |
    
    #Copy located .pst files to the destination
    Copy-Item -Destination "\\networkpath\home\$env:username\ComputerChange\" -Verbose -PassThru -ErrorAction SilentlyContinue | 
    
    #Log where files were located and when they were last written to.
    Select-Object fullname,lastwritetime | export-csv \\networkpath\home\$env:username\ComputerChange\PSTlog.csv -Verbose
    
    
    Write-Host "PST Files have successfully been copied, press any key to close" -ErrorAction SilentlyContinue
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
    end
    

    【讨论】:

      【解决方案5】:

      所以我创建了一个更快的脚本,因为我排除了一些 .PST 文件不保存的系统文件夹和程序文件文件夹。 我敢打赌,你们中的一些专家可以找出为什么这段代码不起作用?

      #Exclude systemfolders etc
      $folders=get-childitem c:\ | where-object{$_.mode -like "d-*" -AND $_.name -notlike "windows" -AND $_.name -notlike "drivers" -AND $_.name -notlike "program files*"}
      
      #Search thru each root folder for PST-files
      $allfiles=$folders | ForEach-Object{get-childitem -Path $_.fullname -include "*.pst" -recurse -ErrorAction silentlycontinue};
      
      $env:username
      $foldertocreate="\\destination\$env:username\"
      
      #Check if folder with username exists in the \\destination folder otherwise create folder with username.       
      if((Test-Path -Path $foldertocreate -PathType Container)) {write-host "Folder already created"}
      
              else {write-host "Creating Folder", New-Item -ItemType Directory -Force -Path $foldertocreate }                              
      
      #Copy .PST files which is in $allfiles to the folder created in fileshare> $foldertocreate.
      
      #Copy .PST files in $allfiles to the destination folder created.
      robocopy $allfiles $foldertocreate
      
      Write-Host "Press any key to close" -ErrorAction SilentlyContinue $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") 
      end
      

      【讨论】:

        猜你喜欢
        • 2014-09-19
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 1970-01-01
        • 2022-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多