Daniel
Daniel

Reputation: 107

Powershell script, load from CSV?

So i got this code:

$MailboxName = "[email protected]"
$MessageId = "<[email protected]>"
$TargetFolder = "\Inbox\random"
Connect-EXRMailbox -MailboxName $MailboxName
Find-EXRMessageFromMessageId -MailboxName $MailboxName -MessageId $MessageId | ForEach-Object{
    Move-EXRMessage -MailboxName $MailboxName -ItemURI $_.ItemRESTURI -TargetFolderPath $TargetFolder
    Write-Host ("Moved Message " + $_.Subject + " to " + $TargetFolder)
}

It works fine, and will move that one email who fits the messageID

But im now trying to make it so it looks at a .csv file insted of 1 file. In the csv file there is alot of messageIDs.

If i use this code:

import-csv d:\test.csv | Select MessageId 
$MailboxName = "[email protected]"
$MessageId = $_.MessageId 
$TargetFolder = "\indbakke\Hotels" 
Connect-EXRMailbox -MailboxName $MailboxName
Find-EXRMessageFromMessageId -MailboxName $MailboxName -MessageId $MessageId | ForEach-Object{
    Move-EXRMessage -MailboxName $MailboxName -ItemURI $_.ItemRESTURI -TargetFolderPath $TargetFolder
    Write-Host ("Moved Message " + $_.Subject + " to " + $TargetFolder)
}

Then it will just print whats in the .csv file,

Can anyone help me out, how do i get my small script working with a CSV file, so it takes messageID from the csv file. ?

Upvotes: 0

Views: 144

Answers (1)

Petr Snizek
Petr Snizek

Reputation: 26

You need to iterate over messageIds from imported csv. In your code, you just import the file, and that's it. $MessageId is empty.

$messageIds = Import-Csv -Path d:\test.csv |
    Select -expandproperty MessageId

ForEach ($MessageId in $messageIds)
{
...
}

Upvotes: 1

Related Questions