扫描C盘并复制文件

Scan C disk and copy files

非常感谢您的帮助。

  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\"

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

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

这个有效:

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

首先你必须对 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

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

但是,您的问题也可以通过使用如下代码片段从注册表中查询 ost/pst 文件来解决:

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

这将 return 登录用户在 outlook 中打开的所有 ost/pst 个文件。

这样的代码片段会将它们全部复制到网络共享并将日志打印到文件中。

$FilesToCopy = (Get-ChildItem HKCU:\Software\Microsoft\Office.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 个字符) - 使此方法更可靠,更适合制作脚本。

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

#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

所以我创建了一个更快的脚本,因为我排除了一些不保存 .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