PowerShell-尝试从所有邮件配置文件中删除PST
我使用以下脚本从Outlook配置文件中删除所有PST,然后删除PST文件。但是,看来这只能抓住默认的邮件配置文件。
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.getNamespace("MAPI")
$folderCheckVAR = Get-Item -Path "C:\temp" -ea Ignore
if ($folderCheckVAR.Exists)
{
Remove-Item "C:\temp\PST_Paths.csv" -ea Ignore
}
else
{
New-Item -Path "C:\" -Name "temp" -ItemType "directory" -ea Ignore
}
$all_psts = $Namespace.Stores | Where-Object {($_.ExchangeStoreType -eq '3') -and
($_.FilePath -like '*.pst') -and ($_.IsDataFileStore -eq $true)}
ForEach ($pst in $all_psts){
$outputVAR = [pscustomobject]@{
"FilePath" = $pst.FilePath
}
$outputVAR | export-csv -NoTypeInformation -Append -Path "c:\temp\Pst_Paths.csv"
$Outlook.Session.RemoveStore($pst.GetRootFolder())
}
if (Get-Process Outlook -ea SilentlyContinue) {
Get-Process Outlook | Foreach-Object { $_.CloseMainWindow() | Out-Null } | Stop-
Process -Force
}
#Delay between Outlook close and file deletion to ensure Outlook closed completely
Start-Sleep -Seconds 5
$csv = "C:\temp\PST_Paths.csv"
$pstPaths = Import-CSV $csv
foreach ($path in $pstPaths) {
Remove-Item $path.FilePath
}
这三行是抓住邮件配置文件并抓住PST的方法:
$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.getNamespace("MAPI")
$all_psts = $Namespace.Stores | Where-Object {($_.ExchangeStoreType -eq '3') -and
($_.FilePath -like '*.pst') -and ($_.IsDataFileStore -eq $true)}
有没有一种方法可以调整此方法可以在用户帐户上获得所有邮件配置文件,而不仅仅是默认值?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,Outlook是一个单身人士 - 只有一个Outlook.exe的实例可以在任何给定时间运行。您可以遍历所有配置文件,启动Outlook(通过创建
Outlook.Application.application
对象的实例),通过调用application.getNamespace(“ Mapi”)登录给定配置文件
/namespace.logon(“ profilename”)
,运行代码,然后通过调用application.quit
,然后重复下一个配置文件的循环。几乎没有效率,因为每个配置文件可能要求用户登录。如果Outlook已经在运行,请调用
namepace.logon
什么都不做 - 您需要先在其他配置文件中首先关闭它。在扩展的MAPI级别(仅C ++或Delphi)上,您可以使用 iProfadmin 接口。您可以使用
iProfadmin :: getProfiletable
获取配置文件列表,请通过调用iProfadmin :: AdminServices
打开每个配置文件,查找所有MSPST
或MSUPST
服务并使用imsgserviceadmin :: deletemsgservice
删除它们。您可以在如果不是C ++或Delphi中的扩展MAPI,则可以使用 profman> profman 图书馆(我也是它的作者) - 它包含配置文件管理接口,可以从任何语言中使用,包括PS。您可以修改示例在Profman网站上以删除PST服务。
Keep in mind that Outlook is a singleton - only one instance of outlook.exe can run at any given time. You can loop through all profiles, start Outlook (by creating an instance of the
Outlook.Application
object), log to the given profile by callingApplication.GetNamespace("MAPI")
/Namespace.Logon("ProfileName")
, run your code, then close Outlook by callingApplication.Quit
, then repeat the loop for the next profile. Hardly efficient, as each profile might require the user to log in.If Outlook is already running, calling
Namespace.Logon
will do nothing - you would need to close it first before starting in a different profile.On the Extended MAPI level (C++ or Delphi only), you can do that without starting Outlook or initializing a MAPI session using the IProfAdmin interface. You can get a list of profiles using
IProfAdmin::GetProfileTable
, open each profile by callingIProfAdmin::AdminServices
, find allMSPST
orMSUPST
services and delete them usingIMsgServiceAdmin::DeleteMsgService
. You can play with these interfaces in OutlookSpy (I am its author - click IProfAdmin button).If Extended MAPI in C++ or Delphi are not an option, you can use the ProfMan library (I am also its author) - it wraps the profile management interfaces and can be used from any language, including PS. You can modify the example at the ProfMan site to delete the PST services.