0

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
0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

You'll need to apply @Ansgar's [0] suggestion to both of your GUID variable calls.
Assuming that both Get-Mailbox statements produce multiple results, yes. Another option would be to pick just one of the results (select -First 1), so the variable doesn't contain a collection in the first place.
Probably not in this scenario, but I've seen scenarios where Powershell will determine whether to create an array or populate a single value into a variable depending on the results. In these scenarios I usually force the return results into an array and then return the first value if I'm sure that is always what I'll want... @(Get-Mailbox -SoftDeletedMailbox -Identity $DeletedMailbox | Select -Expand ExchangeGUID)[0]
It looks like the Get-Mailbox function always returns an ArrayList object... technet.microsoft.com/en-us/library/bb123685(v=exchg.160).aspx
That may be, but the pipeline should normally unroll the ArrayList, so that for an input ArrayList with only one item just that single item would remain.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.