【问题标题】:Use Exchange PowerShell to count old emails in inbox folder使用 Exchange PowerShell 计算收件箱文件夹中的旧电子邮件
【发布时间】:2014-12-11 02:00:26
【问题描述】:

是否有人知道一个 PowerShell 脚本,它使用 EWS 查看用户的 Exchange 2010 SP3 收件箱并获取电子邮件消息的数量和超过 60 天的邮件大小?我不敢相信没有人能够做到这一点。我看到很多人问如何做到这一点。请帮忙:-)

【问题讨论】:

    标签: powershell-2.0 exchangewebservices


    【解决方案1】:

    http://gsexdev.blogspot.com.au/2012/10/reporting-on-item-age-count-and-size-in.htmlhttp://gsexdev.blogspot.com.au/2014/07/creating-mailbox-folder-growth-map-with.html 有几个例子

    特别是你想做这样的事情应该可以工作

    ## Get the Mailbox to Access from the 1st commandline argument
    
    $MailboxName = $args[0]
    $DateFrom = (Get-Date).AddDays(-60)
    
    ## Load Managed API dll  
    Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"  
    
    ## Set Exchange Version  
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2  
    
    ## Create Exchange Service Object  
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)  
    
    ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials  
    
    #Credentials Option 1 using UPN for the windows Account  
    $psCred = Get-Credential  
    $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())  
    $service.Credentials = $creds      
    
    #Credentials Option 2  
    #service.UseDefaultCredentials = $true  
    
    ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates  
    
    ## Code From http://poshcode.org/624
    ## Create a compilation environment
    $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler=$Provider.CreateCompiler()
    $Params=New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable=$False
    $Params.GenerateInMemory=$True
    $Params.IncludeDebugInformation=$False
    $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
    
    $TASource=@'
      namespace Local.ToolkitExtensions.Net.CertificatePolicy{
        public class TrustAll : System.Net.ICertificatePolicy {
          public TrustAll() { 
          }
          public bool CheckValidationResult(System.Net.ServicePoint sp,
            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
            System.Net.WebRequest req, int problem) {
            return true;
          }
        }
      }
    '@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    
    ## We now create an instance of the TrustAll and attach it to the ServicePointManager
    $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
    
    ## end code from http://poshcode.org/624
    
    ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use  
    
    #CAS URL Option 1 Autodiscover  
    $service.AutodiscoverUrl($MailboxName,{$true})  
    "Using CAS Server : " + $Service.url   
    
    #CAS URL Option 2 Hardcoded  
    
    #$uri=[system.URI] "https://casservername/ews/exchange.asmx"  
    #$service.Url = $uri    
    
    ## Optional section for Exchange Impersonation  
    
    #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName) 
    
    # Bind to the Inbox Folder
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)   
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    #Define ItemView to retrive just 1000 Items    
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(250)  
    $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)  
    $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Size)
    $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived)
    $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeCreated)
    $ivItemView.PropertySet = $psPropset
    
    $sfItemSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,$DateFrom) 
    
    $rptObj = "" | Select Mailbox,TotalNumber,TotalSize
    $rptObj.Mailbox = $MailboxName
    $rptObj.TotalNumber = 0
    $rptObj.TotalSize = [Int64]0
    
    $fiItems = $null    
    do{    
        $fiItems = $service.FindItems($Inbox.Id,$sfItemSearchFilter,$ivItemView)    
        #[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)  
        foreach($Item in $fiItems.Items){
                $rptObj.TotalNumber += 1
                $rptObj.TotalSize += [Int64]$Item.Size
        }    
        $ivItemView.Offset += $fiItems.Items.Count    
    }while($fiItems.MoreAvailable -eq $true)
    
    $rptObj | select Mailbox,TotalNumber,@{label="TotalSize(MB)";expression={[math]::Round($_.TotalSize/1MB,2)}}  
    

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 2012-11-26
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2011-10-11
      • 2020-03-10
      • 2020-12-16
      • 2021-04-27
      相关资源
      最近更新 更多