Attempting to modify multiple users Inbox rules from a two column csv

I'm trying to remove all Mailbox forwarding rules from my O365 Exchange tenant and replace them with Inbox Rules instead.

I have a file named AllForwards.csv with O365 usernames in one column, and the SMTP address they are currently forwarding to in another column.

The column titles are 'Name' and 'ForwardingSmtpAddress' respectively so it looks something like this:

Name ForwardingSmtpAddress
Joseph.Low jlo@gmail.com
mary.blithe MarB@yahoo.com
Harry.Eye HE@aol.com

The script I've written works well when there is only one entry, Joseph.Low for example and nothing else.

Here it is:
Connect-ExchangeOnline


Function Disconnect-ExchangeOnline {
Get-PSSession | Where-Object {$_.ConfigurationName -eq “Microsoft.Exchange”} | Remove-PSSession
}


$Forwards = Import-csv "c:\temp\AllForwards.csv"
$Name = $Forwards.Name
$SMTP = $Forwards.ForwardingSmtpAddress

foreach($Line in $Forwards)
{

New-InboxRule -Name RedirectAllInbox -Mailbox $Name -RedirectTo $SMTP

}

foreach ($Line in $Forwards)
{

Set-Mailbox $Name -ForwardingAddress $NULL -ForwardingSmtpAddress $NULL

}

Disconnect-ExchangeOnline
Get-PSSession | Remove-PSSession
Stop-Transcript

But when I have more than one line entry I receive this error when attempting to add the rules:

Cannot process argument transformation on parameter 'Mailbox'. Cannot convert
the "System.Collections.ArrayList" value of type
"System.Collections.ArrayList" to type
"Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
+ CategoryInfo : InvalidData: (:) [New-InboxRule], ParameterBindi
n...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,New-InboxRu
le
+ PSComputerName : outlook.office365.com

And this error when attempting to remove the Mailbox rules:

Cannot process argument transformation on parameter 'Identity'. Cannot convert
the "System.Collections.ArrayList" value of type
"System.Collections.ArrayList" to type
"Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
+ CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin.
..mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
+ PSComputerName : outlook.office365.com

I'm missing something pretty basic I'm sure, can someone help me out with this please? My eyes and brain are burning. 

TIA

Parents
No Data
Reply
  • Figured it out!

    It had to do with the pulling of my objects from imported CSV.

    For those running into this issue here's the two scripts and you can compare to note the changes to get it to work.

    BAD and errors
    $Forwards = Import-csv "c:\temp\AllForwards.csv"
    $Name = $Forwards.Name
    $SMTP = $Forwards.ForwardingSmtpAddress
    foreach($Line in $Forwards){
    New-InboxRule -Name RedirectAllInbox -Mailbox $Name -RedirectTo $SMTP
    }

    foreach ($Line in $Forwards){
    Set-Mailbox $Name -ForwardingAddress $NULL -ForwardingSmtpAddress $NULL
    }


    GOOD and no errors
    $Forwards = Import-csv "c:\temp\AllForwards.csv"
    foreach($Forward in $Forwards){
    $Name = $Forward.Name
    $SMTP = $Forward.ForwardingSmtpAddress
    New-InboxRule -Name RedirectAllInbox -Mailbox $Name -RedirectTo $SMTP
    }

    foreach ($Forward in $Forwards){
    $Name = $Forward.Name
    Set-Mailbox $Name -ForwardingAddress $NULL -ForwardingSmtpAddress $NULL
    }

    Hope that helps someone that is having the same issue working with variables and how to utilize them.

Children
No Data