0
votes

I have been fighting a silly problem where when I try to use variables in a particular command I get an error, however if I use the actual variable values instead it works. Why cannot I use variables here and what is the workaround? Help please. The variables all have the correct values in them. I verified everything works until the last line is executed...

(The last line in this code throws the error...)

#####################################################################
# Prompt for user input regarding deleted and destination mailboxes #
#####################################################################

# Ask for Deleted Mailbox SMTP Address 
$DeletedMailbox = Read-Host "Enter Primary Address of Deleted Mailbox: "

# Ask for Destination Mailbox SMTP Address
$DestinationMailbox = Read-Host "Enter Primary Address of Destination Recovery Mailbox: "

#############################################################
# Query for the Exchange GUID information of both Mailboxes #
#############################################################
$DeletedGUID = Get-Mailbox -SoftDeletedMailbox -Identity $DeletedMailbox |
               Select ExchangeGUID | FT -HideTableHeaders

$DestinationGUID = Get-Mailbox -Identity $DestinationMailbox |
                   Select ExchangeGUID | FT -HideTableHeaders

############################################################################
# Initiate the Restore Request from Deleted mailbox to destination mailbox #
############################################################################
New-MailboxRestoreRequest -SourceMailbox $DeletedGUID -TargetMailbox $DestinationGUID -AllowLegacyDNMismatch

Error it Throws....

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

1 Answers

2
votes

Apparently $DeletedGUID is a list, not a single element. Either pick an element from the list:

New-MailboxRestoreRequest -SourceMailbox $DeletedGUID[0] ...

or run New-MailboxRestoreRequest in a loop:

$DeletedGUID | ForEach-Object {
  New-MailboxRestoreRequest -SourceMailbox $_ ...
}

Also, replace

...| Select ExchangeGUID | FT -HideTableHeaders

with

Select -Expand ExchangeGUID

Format-* cmdlets are for output formatting, not for things you want to process further.