Mariange
Mariange

Reputation: 1

iPhone Activesync for Outlook contact "other" phone numbers not syncing

I found a fix that I am trying to run but having issues. When I enter the second line in Powershell I get the error below. I have a number of contact records that sync via sf.com in outlook, not sure if that is the issue. If you can help me fix the command, so I can change these records from "Other" to a phone field that iphone(Activesynce) will sync, like mobile or pager. I don't want it to replace the current mobile telephone number just categorize it. Thanks!

$outlook = new-object -com outlook.application
$contacts = $outlook.Session.GetDefaultFolder(10)
$contacts.Items | % { if($_.MobileTelephoneNumber -eq "") { $_.MobileTelephoneNumber = $_.OtherTelephoneNumber; $_.OtherTelephoneNumber = ""; $_.save() } }

ERROR

You cannot call a method on a null-valued expression. At line:1 char:1 + $OutlookContacts = $Outlook.session.GetDefaultFolder(10).items + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

Upvotes: 0

Views: 154

Answers (2)

Rupert
Rupert

Reputation: 1

I found that the code for testing the condition was not correct, as you need to test for the mobile phone number field being either null or empty (not just empty string) and then only move the other phone number field into mobile phone number when BOTH the "Other phone number" field is populated and "Mobile Number" is empty.

This was my code that I found worked:

$outlook = new-object -com outlook.application
$contacts = $outlook.Session.GetDefaultFolder(10)
$contacts.Items |
% {
    # Look for items with empty / null mobile number and populated other number
    #NB: Other number does not sync to iPhone Contacts from Microsoft 365
    if ( ( ( [string]::IsNullOrEmpty( $_.MobileTelephoneNumber ) ) ) -and ( !( [string]::IsNullOrEmpty( $_.OtherTelephoneNumber ) ) ) )
    {
        # Prints out each record that matches the condition
        "Subject: '$($PSItem.Subject)': $($PSItem.OtherTelephoneNumber)"

        # Uncomment lines below to put other number into mobile number
        # $_.MobileTelephoneNumber = $_.OtherTelephoneNumber
        # $_.OtherTelephoneNumber = ''
        # $_.save()
    }
# Remove the "| Select-Object -First 20" to have this apply to all records
} | Select-Object -First 20

If you need to apply this logic to anything other than the default contacts folder (as I did, as I have multiple Microsoft 365 accounts set-up in Outlook), then you will also need code to select the correct folder to apply the fix to. Replace the "$contacts = $outlook.Session.GetDefaultFolder(10)" line above with these lines instead where you can customise "Name of Account":

$namespace = $outlook.GetNameSpace("MAPI")

# If you have more than one Microsoft 365 account or a shared folder, you put the name of it in here
$subfolder = $namespace.Folders | Where-Object { $_.name -eq "Name of Account" }

# NB: Putting brackets around the expression makes powershell print out the assigned Object
# The contacts are in a subfolder called "Contacts" for the default entry for this account
($contacts = $subfolder.Folders | Where-Object { $_.name -eq "Contacts" })

Upvotes: 0

postanote
postanote

Reputation: 16116

Tha terror is pretty specific.

Null means Null. / It has nothing to work with.

Don't guess at it (well, we all do it, sometimes... ;-} - but don't make it a habit.), as you'll just unnecessarily frustrate yourself, as such lead to lots of unnecessary hair pulling and I have no hair left, sooo, you know what I mean.

Always dev you code in steps to ensure you are getting back what you'd expect. Thus no need to move to the next step until the active line/block is valid.

Example (using variable squeezing to assign the results and output to the screen)

($outlook = new-object -com outlook.application)

# Results
<#
Application        : Microsoft.Office.Interop.Outlook.ApplicationClass
Class              : olApplication
Session            : Microsoft.Office.Interop.Outlook.NameSpaceClass
Parent             : 
Assistant          : 
Name               : Outlook
Version            : 16.0.0.12325
...
PickerDialog       : System.__ComObject
#>


($contacts = $outlook.Session.GetDefaultFolder(10))

# Results
<#
Application            : Microsoft.Office.Interop.Outlook.ApplicationClass
Class                  : 2
Session                : Microsoft.Office.Interop.Outlook.NameSpaceClass
Parent                 : System.__ComObject
DefaultItemType        : 2
DefaultMessageClass    : IPM.Contact
...
#>

$contacts.Items

# Results
<#
Application                  : Microsoft.Office.Interop.Outlook.ApplicationClass
Class                        : 40
Session                      : System.__ComObject
Parent                       : System.__ComObject
Actions                      : System.__ComObject
Attachments                  : System.__ComObject
BillingInformation           : 
Body                         : 
Categories                   : 
...
#>

$contacts.Items | 
% { 
    if($_.MobileTelephoneNumber -eq '') 
    {
        "MobileTelephoneNumber: $($PSItem.MobileTelephoneNumber)"
        "OtherTelephoneNumber: $($PSItem.OtherTelephoneNumber)"
        <#
        $_.MobileTelephoneNumber = $_.OtherTelephoneNumber 
        $_.OtherTelephoneNumber = '' 
        $_.save() 
        #>
    } 
} | Select-Object -First 20

# Results
<#
MobileTelephoneNumber: 
OtherTelephoneNumber: 
...
MobileTelephoneNumber: 
OtherTelephoneNumber: (800) 555-1212 ,,,4472
...
#>

Upvotes: 0

Related Questions