0

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

Jdg50
  • 448
  • 2
  • 9
  • 18
Mariange
  • 1
  • 2

1 Answers1

0

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
...
#>
postanote
  • 15,138
  • 2
  • 14
  • 25