PS从ID和messeageID移动

时间:2018-02-16 09:03:09

标签: powershell csv

我的脚本采用CSV文件,替换一些单词并生成新的CSV文件。然后它将消息移动到文件夹。

但是现在我正在尝试改进脚本,我希望它能看到2列。

  • 第1栏ID(1-40)
  • 第2栏messageID

我试图告诉脚本ID1,如果1ID,它会转到文件夹2电子邮件将转到文件夹2,依此类推。但似乎无法让它发挥作用。

import-csv c:\Report.csv | Select Mapi-Smtp-Message-Id | export-Csv -Path c:\messageid.csv

(Get-Content 'c:\temp\messageid.csv' | Select-Object -Skip 1) -replace "Mapi-Smtp-Message-Id", "messageID" | Set-Content 'c:\messageid.csv'

$MailboxName = "test@test.com"
$messageIds = Import-Csv -Path c:\messageid.csv |
    Select -expandproperty MessageId
    Select -expandproperty id

Connect-EXRMailbox -MailboxName $MailboxName
ForEach ($MessageId in $messageIds){
    $TargetFolder = "\inbox\folder1"
    Find-EXRMessageFromMessageId -MailboxName $MailboxName -MessageId $MessageId | ForEach-Object{
        Move-EXRMessage -MailboxName $MailboxName -ItemURI $_.ItemRESTURI -TargetFolderPath $TargetFolder
        Write-Host ("Moved Message " + $_.Subject + " to " + $TargetFolder)
    }
}

1 个答案:

答案 0 :(得分:0)

$messages = Import-Csv -Path c:\messageid.csv

# Gives us a list of properties of your $messages object
$messages | Get-Member

我假设您的CSV中的一列被称为id,正如您在问题中所暗示的那样

foreach ($message in $messages) {
    $messageId = $message.MessageId
    $id = $message.id

    $TargetFolder = "\inbox\folder$id"

    Find-EXRMessageFromMessageId -MailboxName $MailboxName -MessageId $messageId |
        ForEach-Object {
            Move-EXRMessage -MailboxName $MailboxName -ItemURI $_.ItemRESTURI -TargetFolderPath $TargetFolder
            Write-Host "Moved Message $($_.Subject) to $TargetFolder"
        }
    }
}