Powershell 使用EWS查找文件夹ID

Powershell 使用EWS查找文件夹ID,powershell,outlook,exchangewebservices,Powershell,Outlook,Exchangewebservices,我想将电子邮件从一个文件夹移动到另一个文件夹。我知道我需要找到文件夹ID。这就是我的问题所在。我看到收件箱有一个FindFolders方法,但我只需要在收件箱中找到文件夹的ID [void] [Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll") $s = New-Object Microsof

我想将电子邮件从一个文件夹移动到另一个文件夹。我知道我需要找到文件夹ID。这就是我的问题所在。我看到收件箱有一个
FindFolders
方法,但我只需要在收件箱中找到文件夹的ID

[void] [Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll")

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$s.AutodiscoverUrl($email)

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

$items = $inbox.FindItems($inbox.TotalCount)
# this doesn't work
$TargetFolder = $inbox.FindFolders('MyFolder')

foreach ($item in $items.Items) {
    $item.Move($TargetFolder)
}
我还使用Outlook中的VB查找了
文件夹ID
。但这也不起作用。我想我需要通过PowerShell找到身份证

$TargetFolder = '00000000DBDD150E618BD0489CDE09859DC24F7A0100949BEDD21F6B4245BEEA6999720A0B090013516500830000'
$item.Move($TargetFolder)
我使用名称空间添加了
,并将其转换为
Data.FolderID
。但是,我得到一个错误,它希望它是一个众所周知的文件夹。我创建了一个客户主文件夹

$TargetFolder = '00000000DBDD150E618BD0489CDE09859DC24F7A0100949BEDD21F6B4245BEEA6999720A0B090013516500830000'
$id = [Data.FolderId]::new($TargetFolder)
$item.Move($id.UniqueId)
我发现以下错误:

无法将值为“
00000000 DBDD150E618BD0489CDE09859DC24F7A0100949BEDD21F6B4245BEEA69720A0B090013516500830000
”的参数“
destinationFolderName
”转换为“
Move
”类型“
Microsoft.Exchange.WebServices.Data.WellKnownFolderName
”:

无法将值“
00000000 DBDD150E618BD0489CDE09859DC24F7A0100949BEDD21F6B4245BEEA6999720A0B090013516500830000
”转换为类型“
Microsoft.Exchange.WebServices.Data.WellKnownFolderName

错误:“无法将标识符名称
00000000 DBDD150E618BD0489CDE09859DC24F7A0100949BEDD1F6B4245BEEA6999720A0B090013516500830000
与有效的枚举器名称匹配。请指定以下枚举器名称之一,然后重试:

日历、联系人、删除项、草稿、收件箱、日记、便笺、发件箱、发送项、任务、MsgFolderRoot、PublicFoldersRoot、Root、JunkEmail、SearchFolders、语音邮件、, 可恢复项根、可恢复项删除、可恢复项版本、可恢复项喷射、归档根、归档MsgFolderRoot、归档删除项、, ArchiveRecoverableItemsRoot、ArchivereCoverableItemsDeletes、ArchiveRecoverableItemsVersions、ArchivereCoverableItemsSpurges、同步问题、冲突、本地故障、, 服务器故障、RecipientCache、QuickContacts、会话历史记录、ToDoSearch“”

当我用我的登录方式尝试你的代码时,我仍然会出错

Using namespace "Microsoft.Exchange.WebServices"

[CmdletBinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$MailAddress
)

# need to download this!!!!!!!!!!!!
[void] [Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll")

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$s.AutodiscoverUrl($MailAddress)
$objExchange = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgFolderRoot)  ###Inbox

<#
    Define Extended properties
    PR_FOLDER_TYPE: Contains a constant that indicates the folder type.
        https://msdn.microsoft.com/en-us/library/office/cc815373.asp
    PR_MESSAGE_SIZE_EXTENDED: Contains the sum, in bytes, of the sizes of all properties on a message object.
    int64 version of PR_MESSAGE_SIZE
        https://msdn.microsoft.com/en-us/library/office/cc839933.aspx
    PR_DELETED_MESSAGE_SIZE_EXTENDED: Could not find official reference.
    PR_FOLDER_PATH: Could not find official reference.
#>
$PR_FOLDER_TYPE = [Data.ExtendedPropertyDefinition]::new(13825,[Data.MapiPropertyType]::Integer)
$PR_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(3592,[Data.MapiPropertyType]::Long);
$PR_DELETED_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(26267,[Data.MapiPropertyType]::Long);
$PR_FOLDER_PATH = New-Object Data.ExtendedPropertyDefinition(26293, [Data.MapiPropertyType]::String);

$folderIDCount = [Data.FolderId]::new([Data.WellKnownFolderName]::MsgFolderRoot,$MailAddress)

# Define the FolderView used for Export. Should not be any larger then 1000 folders due to throttling
$folderView = [Data.FolderView]::new(1000)

# Deep Traversal will ensure all folders in the search path are returned
$folderView.Traversal = [Data.FolderTraversal]::Deep;
$ewsPropertySet = [Data.PropertySet]::new([Data.BasePropertySet]::FirstClassProperties)

# Add Properties to the  Property Set
$ewsPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED);
$ewsPropertySet.Add($PR_FOLDER_PATH);
$folderView.PropertySet = $ewsPropertySet;

# Exclude any Search Folders in the filter
$searchFilter = [Data.SearchFilter+IsEqualTo]::new($PR_FOLDER_TYPE,"1")

# The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox
do {
    $filterResult = $objExchange.FindFolders($folderIDCount, $searchFilter, $folderView)
    foreach ($singleFolder in $filterResult.Folders) {
        # Try to get the FolderPath Value and then covert it to a usable String
        $folderPathValue = $null
        $singleFolder.TryGetProperty($PR_FOLDER_PATH, [ref]$folderPathValue) | Out-Null

        # Output folder object to pipeline
        $singleFolder | Select-Object Id,DisplayName,@{Name="FolderPath";Expression={$folderPathValue}},FolderClass,ParentFolderId
    }
    $folderView.Offset += $filterResult.Folders.Count
} while($filterResult.MoreAvailable)
我可以连接
$objExchange
,我可以获得这些值和更多:

Id              : AAMkADk5ZDNmODk2LTk0NzAtNDZkNi05Mjk1LTNhMjNlYmYzNzg1ZAAuAAAAAADb3RUOYYvQSJzeCYWdwk96AQD+Gq9HeAtSSLFM1nhOxNIuAAAAMAqcAAA=
ParentFolderId  : AAMkADk5ZDNmODk2LTk0NzAtNDZkNi05Mjk1LTNhMjNlYmYzNzg1ZAAuAAAAAADb3RUOYYvQSJzeCYWdwk96AQD+Gq9HeAtSSLFM1nhOxNIuAAAAMAqbAAA=
ChildFolderCount: 20

这里的信息将比要求的要多一些,但我想展示如何在用于与邮箱交互的cmdlet中获取这些信息

[CmdletBinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$MailAddress
)

# Create a reference to Exchange 2010 to match our current environment.
$objExchange = Connect-ExchangeService -MailAddress $MailAddress -DefaultCredentials

<#
    Define Extended properties  
    PR_FOLDER_TYPE: Contains a constant that indicates the folder type.
        https://msdn.microsoft.com/en-us/library/office/cc815373.asp
    PR_MESSAGE_SIZE_EXTENDED: Contains the sum, in bytes, of the sizes of all properties on a message object. int64 version of PR_MESSAGE_SIZE
        https://msdn.microsoft.com/en-us/library/office/cc839933.aspx
    PR_DELETED_MESSAGE_SIZE_EXTENDED: Could not find official reference.
    PR_FOLDER_PATH: Could not find official reference.
#>
$PR_FOLDER_TYPE = [Data.ExtendedPropertyDefinition]::new(13825,[Data.MapiPropertyType]::Integer)
$PR_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(3592,[Data.MapiPropertyType]::Long);  
$PR_DELETED_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(26267,[Data.MapiPropertyType]::Long);  
$PR_FOLDER_PATH = New-Object Data.ExtendedPropertyDefinition(26293, [Data.MapiPropertyType]::String);  

$folderIDCount = [Data.FolderId]::new([Data.WellKnownFolderName]::MsgFolderRoot,$MailAddress)  

# Define the FolderView used for Export. Should not be any larger then 1000 folders due to throttling  
$folderView = [Data.FolderView]::new(1000)

# Deep Traversal will ensure all folders in the search path are returned  
$folderView.Traversal = [Data.FolderTraversal]::Deep;  
$ewsPropertySet = [Data.PropertySet]::new([Data.BasePropertySet]::FirstClassProperties)

# Add Properties to the  Property Set  
$ewsPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED);  
$ewsPropertySet.Add($PR_FOLDER_PATH);  
$folderView.PropertySet = $ewsPropertySet;

# Exclude any Search Folders in the filter
$searchFilter = [Data.SearchFilter+IsEqualTo]::new($PR_FOLDER_TYPE,"1")  

#The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox  
do {  
    $filterResult = $objExchange.FindFolders($folderIDCount, $searchFilter, $folderView)       
    foreach($singleFolder in $filterResult.Folders){              
        #Try to get the FolderPath Value and then covert it to a usable String
        $folderPathValue = $null     
        $singleFolder.TryGetProperty($PR_FOLDER_PATH, [ref]$folderPathValue) | Out-Null

        # Output folder object to pipeline
        $singleFolder | Select-Object Id,DisplayName,@{Name="FolderPath";Expression={$folderPathValue}},FolderClass,ParentFolderId
    } 
    $folderView.Offset += $filterResult.Folders.Count
}while($filterResult.MoreAvailable)
如果您查看,他们需要的不仅仅是一个字符串来获得您要查找的结果

使用指定的搜索筛选器和指定的文件夹视图搜索已知文件夹

这是一个函数,我必须获取邮箱中的所有文件夹

[CmdletBinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$MailAddress
)

# Create a reference to Exchange 2010 to match our current environment.
$objExchange = Connect-ExchangeService -MailAddress $MailAddress -DefaultCredentials

<#
    Define Extended properties  
    PR_FOLDER_TYPE: Contains a constant that indicates the folder type.
        https://msdn.microsoft.com/en-us/library/office/cc815373.asp
    PR_MESSAGE_SIZE_EXTENDED: Contains the sum, in bytes, of the sizes of all properties on a message object. int64 version of PR_MESSAGE_SIZE
        https://msdn.microsoft.com/en-us/library/office/cc839933.aspx
    PR_DELETED_MESSAGE_SIZE_EXTENDED: Could not find official reference.
    PR_FOLDER_PATH: Could not find official reference.
#>
$PR_FOLDER_TYPE = [Data.ExtendedPropertyDefinition]::new(13825,[Data.MapiPropertyType]::Integer)
$PR_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(3592,[Data.MapiPropertyType]::Long);  
$PR_DELETED_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(26267,[Data.MapiPropertyType]::Long);  
$PR_FOLDER_PATH = New-Object Data.ExtendedPropertyDefinition(26293, [Data.MapiPropertyType]::String);  

$folderIDCount = [Data.FolderId]::new([Data.WellKnownFolderName]::MsgFolderRoot,$MailAddress)  

# Define the FolderView used for Export. Should not be any larger then 1000 folders due to throttling  
$folderView = [Data.FolderView]::new(1000)

# Deep Traversal will ensure all folders in the search path are returned  
$folderView.Traversal = [Data.FolderTraversal]::Deep;  
$ewsPropertySet = [Data.PropertySet]::new([Data.BasePropertySet]::FirstClassProperties)

# Add Properties to the  Property Set  
$ewsPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED);  
$ewsPropertySet.Add($PR_FOLDER_PATH);  
$folderView.PropertySet = $ewsPropertySet;

# Exclude any Search Folders in the filter
$searchFilter = [Data.SearchFilter+IsEqualTo]::new($PR_FOLDER_TYPE,"1")  

#The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox  
do {  
    $filterResult = $objExchange.FindFolders($folderIDCount, $searchFilter, $folderView)       
    foreach($singleFolder in $filterResult.Folders){              
        #Try to get the FolderPath Value and then covert it to a usable String
        $folderPathValue = $null     
        $singleFolder.TryGetProperty($PR_FOLDER_PATH, [ref]$folderPathValue) | Out-Null

        # Output folder object to pipeline
        $singleFolder | Select-Object Id,DisplayName,@{Name="FolderPath";Expression={$folderPathValue}},FolderClass,ParentFolderId
    } 
    $folderView.Offset += $filterResult.Folders.Count
}while($filterResult.MoreAvailable)
很多EWS参数都需要一个typedID,而不仅仅是一个字符串。在上一个示例中,我认为这应该会触发一个错误,声明类似的内容。无论哪种方式,如果您有一个字符串,都可以这样强制转换,以获得EWS可以使用的内容

$id = [Data.FolderId]::new($targetFolderID)
您可能会注意到这里的类型名称较短。我在脚本中使用了
名称空间“Microsoft.Exchange.WebServices”
,以减少行长

额外阅读
我很难理解EWS。通过许多示例展示了它是如何实现的。作者也是这里的一名成员,经常回答EWS的问题。

这里的信息比要求的要多一些,但我想展示如何在我为交互而制作的cmdlet中获取这些信息用我的邮箱

[CmdletBinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$MailAddress
)

# Create a reference to Exchange 2010 to match our current environment.
$objExchange = Connect-ExchangeService -MailAddress $MailAddress -DefaultCredentials

<#
    Define Extended properties  
    PR_FOLDER_TYPE: Contains a constant that indicates the folder type.
        https://msdn.microsoft.com/en-us/library/office/cc815373.asp
    PR_MESSAGE_SIZE_EXTENDED: Contains the sum, in bytes, of the sizes of all properties on a message object. int64 version of PR_MESSAGE_SIZE
        https://msdn.microsoft.com/en-us/library/office/cc839933.aspx
    PR_DELETED_MESSAGE_SIZE_EXTENDED: Could not find official reference.
    PR_FOLDER_PATH: Could not find official reference.
#>
$PR_FOLDER_TYPE = [Data.ExtendedPropertyDefinition]::new(13825,[Data.MapiPropertyType]::Integer)
$PR_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(3592,[Data.MapiPropertyType]::Long);  
$PR_DELETED_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(26267,[Data.MapiPropertyType]::Long);  
$PR_FOLDER_PATH = New-Object Data.ExtendedPropertyDefinition(26293, [Data.MapiPropertyType]::String);  

$folderIDCount = [Data.FolderId]::new([Data.WellKnownFolderName]::MsgFolderRoot,$MailAddress)  

# Define the FolderView used for Export. Should not be any larger then 1000 folders due to throttling  
$folderView = [Data.FolderView]::new(1000)

# Deep Traversal will ensure all folders in the search path are returned  
$folderView.Traversal = [Data.FolderTraversal]::Deep;  
$ewsPropertySet = [Data.PropertySet]::new([Data.BasePropertySet]::FirstClassProperties)

# Add Properties to the  Property Set  
$ewsPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED);  
$ewsPropertySet.Add($PR_FOLDER_PATH);  
$folderView.PropertySet = $ewsPropertySet;

# Exclude any Search Folders in the filter
$searchFilter = [Data.SearchFilter+IsEqualTo]::new($PR_FOLDER_TYPE,"1")  

#The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox  
do {  
    $filterResult = $objExchange.FindFolders($folderIDCount, $searchFilter, $folderView)       
    foreach($singleFolder in $filterResult.Folders){              
        #Try to get the FolderPath Value and then covert it to a usable String
        $folderPathValue = $null     
        $singleFolder.TryGetProperty($PR_FOLDER_PATH, [ref]$folderPathValue) | Out-Null

        # Output folder object to pipeline
        $singleFolder | Select-Object Id,DisplayName,@{Name="FolderPath";Expression={$folderPathValue}},FolderClass,ParentFolderId
    } 
    $folderView.Offset += $filterResult.Folders.Count
}while($filterResult.MoreAvailable)
如果您查看,他们需要的不仅仅是一个字符串来获得您要查找的结果

使用指定的搜索筛选器和指定的文件夹视图搜索已知文件夹

这是一个函数,我必须获取邮箱中的所有文件夹

[CmdletBinding()]
param(
    [parameter(Mandatory=$true)]
    [string]$MailAddress
)

# Create a reference to Exchange 2010 to match our current environment.
$objExchange = Connect-ExchangeService -MailAddress $MailAddress -DefaultCredentials

<#
    Define Extended properties  
    PR_FOLDER_TYPE: Contains a constant that indicates the folder type.
        https://msdn.microsoft.com/en-us/library/office/cc815373.asp
    PR_MESSAGE_SIZE_EXTENDED: Contains the sum, in bytes, of the sizes of all properties on a message object. int64 version of PR_MESSAGE_SIZE
        https://msdn.microsoft.com/en-us/library/office/cc839933.aspx
    PR_DELETED_MESSAGE_SIZE_EXTENDED: Could not find official reference.
    PR_FOLDER_PATH: Could not find official reference.
#>
$PR_FOLDER_TYPE = [Data.ExtendedPropertyDefinition]::new(13825,[Data.MapiPropertyType]::Integer)
$PR_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(3592,[Data.MapiPropertyType]::Long);  
$PR_DELETED_MESSAGE_SIZE_EXTENDED = New-Object Data.ExtendedPropertyDefinition(26267,[Data.MapiPropertyType]::Long);  
$PR_FOLDER_PATH = New-Object Data.ExtendedPropertyDefinition(26293, [Data.MapiPropertyType]::String);  

$folderIDCount = [Data.FolderId]::new([Data.WellKnownFolderName]::MsgFolderRoot,$MailAddress)  

# Define the FolderView used for Export. Should not be any larger then 1000 folders due to throttling  
$folderView = [Data.FolderView]::new(1000)

# Deep Traversal will ensure all folders in the search path are returned  
$folderView.Traversal = [Data.FolderTraversal]::Deep;  
$ewsPropertySet = [Data.PropertySet]::new([Data.BasePropertySet]::FirstClassProperties)

# Add Properties to the  Property Set  
$ewsPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED);  
$ewsPropertySet.Add($PR_FOLDER_PATH);  
$folderView.PropertySet = $ewsPropertySet;

# Exclude any Search Folders in the filter
$searchFilter = [Data.SearchFilter+IsEqualTo]::new($PR_FOLDER_TYPE,"1")  

#The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox  
do {  
    $filterResult = $objExchange.FindFolders($folderIDCount, $searchFilter, $folderView)       
    foreach($singleFolder in $filterResult.Folders){              
        #Try to get the FolderPath Value and then covert it to a usable String
        $folderPathValue = $null     
        $singleFolder.TryGetProperty($PR_FOLDER_PATH, [ref]$folderPathValue) | Out-Null

        # Output folder object to pipeline
        $singleFolder | Select-Object Id,DisplayName,@{Name="FolderPath";Expression={$folderPathValue}},FolderClass,ParentFolderId
    } 
    $folderView.Offset += $filterResult.Folders.Count
}while($filterResult.MoreAvailable)
很多EWS参数都需要一个typedID,而不仅仅是一个字符串。在上一个示例中,我认为这应该会触发一个错误,声明类似的内容。无论哪种方式,如果您有一个字符串,都可以这样强制转换,以获得EWS可以使用的内容

$id = [Data.FolderId]::new($targetFolderID)
您可能会注意到这里的类型名称较短。我在脚本中使用了
名称空间“Microsoft.Exchange.WebServices”
,以减少行长

额外阅读
我很难理解EWS。通过许多例子说明了它是如何完成的。作者也是这里的一名成员,经常回答EWS的问题。

好的。所以我尝试了一种新的方法,从
msgFolderRoot
开始深入查找文件夹

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$s.AutodiscoverUrl($MailAddress)
#$objExchange = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgFolderRoot)  ###Inbox

$objExchange = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgFolderRoot)  ###Inbox

$fv = [Microsoft.Exchange.WebServices.Data.FolderView]100
$fv.Traversal = 'Deep'
$objExchange.Load()

$folders = $objExchange.FindFolders($fv)|select DisplayName,ID
$folders = $objExchange.FindFolders($fv)

foreach ($folder in $folders) {
    if ($folder.DisplayName -eq 'LiteratumLicenseInventoryReportArchive') {
        $folderMoveTo = $folder;
    }
}

$folderMoveTo
然后我通过搜索收件箱找到电子邮件,然后将文件夹转换为文件夹ID并移动该电子邮件

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

# discover the url from your email address
$s.AutodiscoverUrl($email)

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) 

# create a property set (to let us access the body & other details not available from the FindItems call)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

$items = $inbox.FindItems($inbox.TotalCount)

foreach ($item in $items.Items) {
    $item.load($psPropertySet)
    if ($item.Subject.Contains('Subject Test')) {
        $convertedFolder = [Data.FolderId]::new($folderMoveTo.Id)
        $item.Move($convertedFolder)
        break 
    }
}

好的。所以我尝试了一种新的深入查找文件夹的方法,从
msgFolderRoot
开始

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$s.AutodiscoverUrl($MailAddress)
#$objExchange = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgFolderRoot)  ###Inbox

$objExchange = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgFolderRoot)  ###Inbox

$fv = [Microsoft.Exchange.WebServices.Data.FolderView]100
$fv.Traversal = 'Deep'
$objExchange.Load()

$folders = $objExchange.FindFolders($fv)|select DisplayName,ID
$folders = $objExchange.FindFolders($fv)

foreach ($folder in $folders) {
    if ($folder.DisplayName -eq 'LiteratumLicenseInventoryReportArchive') {
        $folderMoveTo = $folder;
    }
}

$folderMoveTo
然后我通过搜索收件箱找到电子邮件,然后将文件夹转换为文件夹ID并移动该电子邮件

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$s.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

# discover the url from your email address
$s.AutodiscoverUrl($email)

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) 

# create a property set (to let us access the body & other details not available from the FindItems call)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

$items = $inbox.FindItems($inbox.TotalCount)

foreach ($item in $items.Items) {
    $item.load($psPropertySet)
    if ($item.Subject.Contains('Subject Test')) {
        $convertedFolder = [Data.FolderId]::new($folderMoveTo.Id)
        $item.Move($convertedFolder)
        break 
    }
}

稍后我将查看该博客。我尝试将ID转换为data.FolderID。但是对于move方法,它应为“Microsoft.Exchange.WebServices.data.WellKnownFolderName”类型“。但是,我认为move方法将接受我创建的自定义文件夹。我有一项任务,每5分钟为我将邮件移动到自定义文件夹。我知道这应该管用。让我检查一下我的代码和你的代码,看看我是否遗漏了什么。我想这是因为我的自定义文件夹不是收件箱的子文件夹,而是处于并行级别。我找到了上面讨论它的链接。我将探索它,然后回来与答案。。。他们还绑定$rootFolder=[Microsoft.Exchange.WebServices.Data.Folder]::bind($service[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot)。。。我认为这种方法也适用于一般文件夹。我上面的代码也适用于
msgFolderRoot
。是的,这对你来说是个问题。让我知道这个实现是如何工作的。我稍后会查看博客。我尝试将ID转换为data.FolderID。但是对于move方法,它希望输入“Microso”