Exchange Online help with powershell

New Contributor

So long story short.  We have 9 domains in our org and when people transfer from department to department we are required to disable their old account and create a new one or sometimes we have contractors that become FTE's.  Most of the time the department allows them to take their mail but because it's a new account I have to migrate the mailbox in the recycle bin to the new one under the new account.  I have scripted this process and everything is working except when I try to catch and use the Exchange GUID in a variable.  After I grab it, I do a Write-Host and it prints just fine but when it gets to New-MailboxRestoreRequest to use the GUID, I get the following: 




I can copy and paste the output of the Write-Host in place of the variables and it works.  I just don't want to have to do that every time. 

Here is the script with identifying information stripped. Thanks in advance for the help. 


$Session = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri http://exchangeServer/PowerShell/ -authentication Kerberos
Import-PSSession $Session -disablenamechecking
set-adserversettings -viewentireforest $true

#Set your variables

#Get Old and New GUIDS
$oldEmailGUID=get-exomailbox -SoftDeletedMailbox -UserPrincipalName $upn'@'$oldDomain -PropertySets all | select name,primarysmtpaddress,exchangeguid,legacy*,whencreated,whenchanged
Write-Host $oldEmailGUID.ExchangeGuid
$newEmailGUID=get-exomailbox -UserPrincipalName $upn'@'$newDomain -PropertySets all | select name,primarysmtpaddress,exchangeguid,legacy*,whencreated,whenchanged
Write-Host $newEmailGUID.ExchangeGuid
Start-Sleep -Seconds 5

#Copy Old mailbox to New Mailbox
New-MailboxRestoreRequest -Name $upn -SourceMailbox $oldEmailGUID -TargetMailbox $newEmailGUID -AllowLegacyDNMismatch -CompletedRequestAgeLimit 7 -ConflictResolutionOption keepall –LargeItemLimit 5
#-BadItemLimit 2000

Start-Sleep -Seconds 5

#Add X500 forwarding address **must first remove from old account through AD

#Get Old x500

$oldEmailX500=Get-exomailbox -SoftDeletedMailbox -UserPrincipalName $upn'@'$oldDomain -PropertySets all | select name,primarysmtpaddress,exchangeguid,legacy*,whencreated,whenchanged
Write-Host $oldEmailX500.LegacyExchangeDN
$fullX500ValueCheck='@{Add = X500:'+$oldEmailX500.LegacyExchangeDN+'}'
Write-Host $fullX500ValueCheck

#Add X500 forwarding address **must first remove from old account through AD
Set-remoteMailbox -Identity $upn'@'$newDomain -EmailAddresses @{Add = 'X500:'+$oldEmailX500.LegacyExchangeDN} -verbose

##### Show Restore Status ####

#get restore guid
$mailboxRestoreRequestStatsGUID=Get-MailboxRestoreRequest -Name $upn
Write-host $mailboxRestoreRequestStatsGUID.RequestGuid

#Check Status **update guid from previous step
Get-MailboxRestoreRequestStatistics -Identity $mailboxRestoreRequestStatsGUID.RequestGuid | select targetmailboxidentity,status,statusdetail,failuretype,message,estimatedtransfersize,bytestransferred,EstimatedTransferItemCount,itemstransferred,percentcomplete,*stalled*,overallduration

2 Replies
best response confirmed by JoshuaSiegel (New Contributor)
That's likely because the cmdlet expects a string value, and you're not passing such. Try using the .ToString() method on it, or $oldEmailGUID.ExchangeGuid.Guid
Thanks! That did the trick!!!