PowerShell-尝试从所有邮件配置文件中删除PST

发布于 2025-01-21 13:08:47 字数 1778 浏览 0 评论 0 原文

我使用以下脚本从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)}

有没有一种方法可以调整此方法可以在用户帐户上获得所有邮件配置文件,而不仅仅是默认值?

I am using the below script to remove all PSTs from an Outlook profile, then delete the PST files. However, it seems that this can only grab the default mail profile.

    $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
    }

These three lines are what grab the mail profile and grab the PSTs:

$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)}

Is there a way that I can adjust this be able to get all mail profiles on the user account, not just the default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2025-01-28 13:08:47

请记住,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 calling Application.GetNamespace("MAPI") / Namespace.Logon("ProfileName"), run your code, then close Outlook by calling Application.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 calling IProfAdmin::AdminServices, find all MSPST or MSUPST services and delete them using IMsgServiceAdmin::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文