我尝试将用户帐户中的所有文件夹复制到pst文件中。脚本适用于除收件箱之外的所有文件夹。当我在复制结束时尝试从收件箱中复制数据时我有错误
使用“1”参数调用“CopyTo”的异常:“无法移动或复制文件夹。无法复制文件夹。顶级文件夹无法复制到其子文件夹之一。或者,您可能没有适当的权限对于该文件夹。要检查文件夹的权限,请右键单击该文件夹,然后单击快捷菜单上的“属性”。“
下面的脚本
$start = Get-Date
#checking if outlook is running
$isRunning = (@(Get-Process -ea silentlycontinue OUTLOOK).count -gt 0)
#showing dialog to choose direcotry where pst should be saved
$app = new-object -com Shell.Application
$folder = $app.BrowseForFolder(0, "Select Folder", 0, "C:\")
if ($folder.Self.Path -ne "") {
$path = $folder.Self.Path+"\"+$env:USERNAME+".pst"
} else {
Write-Host "directory not selected. Exiting.."
[Environment]::Exit(1)
}
#reference to outlook
Add-Type -Assembly "Microsoft.Office.Interop.Outlook" | out-null
$outlook = New-Object -ComObject outlook.application
$ns = $outlook.GetNamespace("MAPI")
#adding personal archive file
$ns.AddStoreEx($path, 1)
Write-Host created $path
$dst = $ns.Folders.GetLast()
Write-Host folder name: $dst.Name
#list of directory types to copy. more details https://msdn.microsoft.com/en-us/library/office/ff861868.aspx
$folderTypes = 9,10,3,16,6,12,4,5,30,13,28
#iterating trough list of directories and for each dir make copy in pst file. next counting of objects in archive and pst
foreach ($id in $folderTypes) {
$src = $ns.GetDefaultFolder($id)
$tmp = $src.copyTo($dst)
Write-Host copied to $tmp.name
}
#deattaching personal store from outlook
$ns.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod, $null, $ns, ($dst))
Write-Host $dst.name closed
#if outlook wasn't running on start we must close it
if (-not $isRunning) {
Write-Host closing outlook
$outlook.Quit()
}
$end = Get-Date
$diff = New-TimeSpan -start $start -end $end
Write-Host done
"time elapsed {0:g}" -f $diff
发生此错误的任何想法? 谢谢你的推荐
答案 0 :(得分:0)
确保Windows注册表集中没有DisableCrossAccountCopy键。以下是CopyTo方法描述的内容:
在Windows注册表中的HKCU \ Software \ Microsoft \ Office \ 14.0 \ Outlook中设置REG_MULTI_SZ值DisableCrossAccountCopy会产生禁用此方法的副作用。
此外,我建议立即在代码中释放底层的COM对象。完成使用后,使用System.Runtime.InteropServices.Marshal.ReleaseComObject释放Outlook对象。如果您的加载项尝试枚举存储在Microsoft Exchange Server上的集合中超过256个Outlook项目,这一点尤为重要。然后在Visual Basic中将变量设置为Nothing(C#中为null)以释放对该对象的引用。请在Systematically Releasing Objects文章中详细了解相关内容。