none
Powershell script issue RRS feed

  • General discussion

  • Hi guys!

    Having little problem with Powershell script what checks mailbox through MS Outlook COM API.

    Problem is that when you execute the script, it always prompts for the profile change, but only one default profile is in use, settings are that Use only this profile, and removed all other profiles. 

    Issue occurs in Windows 10, same script works perfectly in Windows 7.

    Can anyone give some advice?

    Thank you in advance!

    $config = @{
    	MailboxName = @('testmailbox@testmail.com', 'Assetmanagement Analytics') # CHANGE THIS, alternative names of additional mailbox
    	TestFolder = 'Test\Test\Processed' # CHANGE THIS, list contents of this folder
    }
    
    
    # ----------------------------------------------------------------------
    # SUBROUTINES
    # ----------------------------------------------------------------------
    
    function Get-MailFolder ([System.__ComObject]$Root, [string]$Path) {
    
    	$current = $Root
    
    	ForEach ($name In ($Path -split '\\')) {
    		$next = $current.Folders | Where-Object {$_.Name -eq $name}
    		If ($next) {
    			$current = $next
    		}
    		Else {
    			$current = $null
    			break
    		}
    	}
    
    	$current
    
    }
    
    
    # ----------------------------------------------------------------------
    # CONNECT MAILBOX
    # ----------------------------------------------------------------------
    
    Write-Host 'Connecting to the mailbox...'
    # THE NEXT LINE CAUSES "Choose Profile" TO POP UP WITH Outlook 365
    $outlook = New-Object -com Outlook.Application
    $mapi = $outlook.GetNamespace('MAPI')
    
    # identify correct mailbox
    $mbfound = $FALSE
    ForEach ($mbname In $config.MailboxName) {
    	$mailbox = Get-MailFolder $mapi $mbname
    	If ($mailbox) {
    		$mbfound = $TRUE
    		break
    	}
    }
    
    # terminate the script if mailbox wasn't found
    If (!$mbfound) {
    	Write-Host 'Mailbox not found. Terminating...'
    	exit 9
    }
    
    # get the handle of the mailbox's source folder
    $source = Get-MailFolder $mailbox $config.TestFolder
    If (!$source) {
    	Write-Host "Mailfolder $($config.TestFolder) not found. Terminating the execution."
    	exit 9
    }
    
    
    # ----------------------------------------------------------------------
    # CHECK MAILBOX
    # ----------------------------------------------------------------------
    
    Write-Host 'Checking messages...'
    $messages = @($source.Items)
    If ($messages.Count -eq 0) {
    	Write-Host 'No message(s) found...'
    	exit 0
    }
    
    
    # ----------------------------------------------------------------------
    # LIST MESSAGES
    # ----------------------------------------------------------------------
    
    Write-Host 'Listing messages...'
    $source.Items | %{
        $message = $_
    	Write-Host "$($message.Subject): $($message.ReceivedTime)"
    }
    
    
    # ----------------------------------------------------------------------
    # FINISH
    # ----------------------------------------------------------------------
    
    Write-Host 'COMPLETE.'
    exit 0


    • Changed type Bill_Stewart Tuesday, November 7, 2017 10:10 PM
    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:10 PM This is not "fix/debug/rewrite script I found on the Internet" forum
    Monday, September 25, 2017 5:39 AM

All replies

  • Then you will have to fix your Office installation.  If it is prompting then Outlook is corrupted in some way.

    \_(ツ)_/

    Monday, September 25, 2017 6:06 AM
  • The profile prompt is not showing when Outlook is running, but when it's closed, prompts for a profile. I've tested it in a 3 diff computers, i will try to get the O365 version.
    Monday, September 25, 2017 6:58 AM
  • Doesn't matter.  I have tested it on many systems for almost two decades.  If you are getting a profile challenge then either your profile is damaged or the install is damaged. 

    It has been tested on )365, Office 2016/2013/2010 and before


    \_(ツ)_/

    Monday, September 25, 2017 7:17 AM
  • This has been tested on several computers and Office repair and reinstallation has been performed, still the same. O365 16.0.7369.2120 version is being used.
    Monday, September 25, 2017 9:16 AM
  • Keep in mind that this is not an official support forum; there is no SLA.

    If you need further assistance, we would recommend opening an official support ticket with Microsoft or hire a consultant.


    -- Bill Stewart [Bill_Stewart]


    Monday, September 25, 2017 2:14 PM