2014-07-22 1 views
1

받은 편지함/SMS 하위 폴더로 모든 문자 메시지를 이동 "문자 메시지"또는 "멀티미디어 메시지"양식 ...을 사용하는 메시지를받은 편지함/SMS 하위 폴더로 옮깁니다.파워 쉘 + EWS 스크립트는 ...</p> <p>모두 확인합니다 나는 나를 (가장 간단한 방법 사용) PowerShell 스크립트 예제를 게시 할 수있는 것보다 더 많은 경험이 풍부한 사람을 바라고

기본적으로 명령 줄을 제외하고 기본적으로 Outlook 2010 규칙의 기능을 재현하려고합니다.

이미이 작업을 수행하는 미리 만들어진 cmdlet가있는 경우. 임씨가하려고하는 것의 예를 들자면 그것은 이상적 일 것입니다.

답변

0

당신은 할 수 있어야하는이

$MailboxName = $args[0] 

## 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 

[email protected]' 
    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) 
function FolderIdFromPath{ 
    param (
      $FolderPath = "$(throw 'Folder Path is a mandatory Parameter')" 
     ) 
    process{ 
     ## Find and Bind to Folder based on Path 
     #Define the path to search should be seperated with \ 
     #Bind to the MSGFolder Root 
     $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName) 
     $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid) 
     #Split the Search path into an array 
     $fldArray = $FolderPath.Split("\") 
     #Loop through the Split Array and do a Search for each level of folder 
     for ($lint = 1; $lint -lt $fldArray.Length; $lint++) { 
      #Perform search based on the displayname of each folder level 
      $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1) 
      $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint]) 
      $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView) 
      if ($findFolderResults.TotalCount -gt 0){ 
       foreach($folder in $findFolderResults.Folders){ 
        $tfTargetFolder = $folder     
       } 
      } 
      else{ 
       "Error Folder Not Found" 
       $tfTargetFolder = $null 
       break 
      }  
     } 
     if($tfTargetFolder -ne $null){ 
      return $tfTargetFolder.Id.UniqueId.ToString() 
     } 
    } 
} 


$TextMessageClass = "IPM.Note.Mobile.SMS" 
$MMSMessageClass = "IPM.Note.Mobile.MMS" 

$sfItemSearchFilter1 = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass,$TextMessageClass) 
$sfItemSearchFilter2 = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.ItemSchema]::ItemClass,$MMSMessageClass) 

#SearchFilter collection Or 
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::Or); 
$sfCollection.Add($sfItemSearchFilter1) 
$sfCollection.Add($sfItemSearchFilter2) 

# 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) 

# Get Folder to Move Item to 

$fldId = FolderIdFromPath -FolderPath "\Inbox\SMS" 
$SubFolderId = new-object Microsoft.Exchange.WebServices.Data.FolderId($fldId) 
$SubFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$SubFolderId) 
$Itemids = @() 
if($SubFolder -ne $null){ 
    #Define ItemView to retrive just 1000 Items  
    $ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
    $fiItems = $null  
    do{  
     $fiItems = $service.FindItems($Inbox.Id,$sfCollection,$ivItemView)  
     #[Void]$service.LoadPropertiesForItems($fiItems,$psPropset) 
     foreach($Item in $fiItems.Items){   
      Write-Host ("Moving " + $Item.Subject) 
      $Itemids += $Item 
     }  
     $ivItemView.Offset += $fiItems.Items.Count  
    }while($fiItems.MoreAvailable -eq $true) 
    #Total Items Processed Varible 
    $nmbProcessed = 0 
    if($Itemids.Count -gt 0){ 
     write-host ("Move " + $Itemids.Count + " Items") 
     #Create Collection for Move Batch 
     $type = ("System.Collections.Generic.List"+'`'+"1") -as "Type" 
     $type = $type.MakeGenericType("Microsoft.Exchange.WebServices.Data.ItemId" -as "Type") 
     $BatchItemids = [Activator]::CreateInstance($type) 
     #Varible to Track BatchSize 
     $batchSize = 0 
     foreach($iiID in $Itemids){ 
      $nmbProcessed++ 
      $BatchItemids.Add($iiID.Id) 
      if($iiID.Size -ne $null){ 
       $batchSize += $iiID.Size 
      } 
      #if BatchCount greator then 50 or larger the 10 MB Move Batch 
      if($BatchItemids.Count -eq 50 -bor $batchSize -gt (10*1MB)){ 
       $Result = $null 
       $Result = $service.MoveItems($BatchItemids,$SubFolder.Id) 
       [INT]$collectionCount = 0 
       [INT]$Rcount = 0 
       [INT]$Errcount = 0 
       if($Result -ne $null){ 
        foreach ($res in $Result){ 
         if ($res.Result -eq [Microsoft.Exchange.WebServices.Data.ServiceResult]::Success){ 
          $Rcount++ 
         } 
         else{ 
          $Errcount++ 
         } 
         $collectionCount++ 
        } 
       } 
       else{ 
        Write-Host -foregroundcolor red ("Move Result Null Exception") 
       } 
       Write-host ($Rcount.ToString() + " Items Moved Successfully " + "Total Processed " + $nmbProcessed + " Total Folder Items " + $Itemids.Count) 
       if($Errcount -gt 0){ 
        Write-Host -foregroundcolor red ($Errcount.ToString() + " Error failed Moved") 
       } 
       $BatchItemids.Clear() 
       $batchSize = 0 
      } 
     } 
     if($BatchItemids.Count -gt 0){ 
      $Result = $service.MoveItems($BatchItemids,$SubFolder.Id) 
      [INT]$Rcount = 0 
      [INT]$Errcount = 0 
      foreach ($res in $Result){ 
       if ($res.Result -eq [Microsoft.Exchange.WebServices.Data.ServiceResult]::Success){ 
        $Rcount++ 
       } 
       else{ 
        $Errcount++ 
       } 
      } 
      Write-host ($Rcount.ToString() + " Items Moved Successfully") 
      if($Errcount -gt 0){ 
       Write-Host -foregroundcolor red ($Errcount.ToString() + " Error failed Moved") 
      } 
     } 
    } 
} 

건배 같은 글렌