none
The property 'DisplayName' cannot be found on this object. Verify that the property exists and can be set RRS feed

  • Question

  • Hello !

    I've the error when i'm trying to execute the script below

    _____________________________________________________________________________________

    At D:\Scripts\gg\create folder in mailbox\AddFolderEWS\AddFoldersEWS.ps1:47 char:19
    +         $folder = New-Object Microsoft.Exchange.WebServices.Data.Folder($exchser ...
        + CategoryInfo          : ObjectNotFound: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

    The property 'DisplayName' cannot be found on this object. Verify that the property exists and can be set.
    At D:\Scripts\gg\create folder in mailbox\AddFolderEWS\AddFoldersEWS.ps1:48 char:9
    +         $folder.DisplayName = "Controls 2017";
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound

    You cannot call a method on a null-valued expression.
    At D:\Scripts\gg\create folder in mailbox\AddFolderEWS\AddFoldersEWS.ps1:49 char:9
    +         $folder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]:: ...
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    ______________________________________________________________________________________________

    the script related to AddMailboxFolderEws :

    ##Define the SMTP Address of the mailbox to impersonate

    $MailboxToImpersonate = "salim.hassissi@externe.bnpparibas.com"

    $dllpath = "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.WebServices.dll"

    Import-Module $dllpath

    ## Set Exchange Version

    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013

    ###################################################################################
    foreach ($muid in $list) {
        Add-MailboxPermission $MUID -User "salim.hassissi@externe.bnpparibas.com" -AccessRights FullAccess
        
        start-sleep 2
        
        $mailaddress = $null

        $mailaddress = (Get-Mailbox $muid).windowsemailaddress.tostring()
        $pwd="xxxxxxxxxxx"
        if ($mailaddress) {

    Import-Module -Name "C:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.WebServices.dll"
    ## Create Exchange Service Object

    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)

    # Set the Credentials

    $psCred = Get-Credential
    $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())
    $service.Credentials = $creds

    ## Set the URL of the CAS (Client Access Server)

    $service.AutodiscoverUrl($AccountWithImpersonationRights ,{$true})

    #Read data from the UserAccounts.txt
            $folder = New-Object Microsoft.Exchange.WebServices.Data.Folder($exchservice);
            $folder.DisplayName = "Controls 2017";
            $folder.Save([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox);
            
            Write-Host "Done for $($muid)"
            #$Rootfolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::msgFolderRoot,$mailboxname)
            start-sleep 2
            $sublijst="2017/01","2017/02","2017/03","2017/04","2017/05","2017/06","2017/07","2017/08","2017/09","2017/10","2017/11","2017/12"
            foreach ($subl in $sublijst)
            {
            $InboxFolder = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonatedMailboxName);
            $InboxFolder.DisplayName = $subl;
            $InboxFolder.Save($InboxFolder.ID);
            $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)
            Write-Host "Done for $($muid)"
            }

            }

        else {

            #Add-Content -Path "D:\Tools\Exchange\Fred\EWS\output.txt" -Value "Could not find a mailaddresss for $($muid)"

        }
        Remove-MailboxPermission $MUID -User "salim.hassissi@externe.bnpparibas.com" -AccessRights FullAccess -Confirm:$false

    thanks in advance for your help

    Tuesday, January 16, 2018 1:48 PM

Answers

  • What are you trying to do ? Always start with an explanation of what your trying to archive with the code you have posted

    Your code won't work eg

     $InboxFolder = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonatedMailboxName);

    Gets an FolderId class which you could then bind to the Inbox so the next line after this should have been

    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)


    This also make no sense

             $InboxFolder.DisplayName = $subl;
            $InboxFolder.Save($InboxFolder.ID);

    Even if you change to using the right varible in this case which would be $Inbox you can't call Save on that folder If a folder already exists in Exchange and you want to update a property your call the Update Method (Save is only used when create a New Subfolder), Also the Inbox Folder is a WellKnownFolder so can't be renamed in this way anyway so even if you change your code to bind to the folder correctly trying to change the name of this folder in this way won't work and will give an error

    Cheers
    Glen

    • Marked as answer by slimhss Wednesday, January 17, 2018 3:11 PM
    Wednesday, January 17, 2018 6:01 AM

All replies

  • What are you trying to do ? Always start with an explanation of what your trying to archive with the code you have posted

    Your code won't work eg

     $InboxFolder = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$ImpersonatedMailboxName);

    Gets an FolderId class which you could then bind to the Inbox so the next line after this should have been

    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolder)


    This also make no sense

             $InboxFolder.DisplayName = $subl;
            $InboxFolder.Save($InboxFolder.ID);

    Even if you change to using the right varible in this case which would be $Inbox you can't call Save on that folder If a folder already exists in Exchange and you want to update a property your call the Update Method (Save is only used when create a New Subfolder), Also the Inbox Folder is a WellKnownFolder so can't be renamed in this way anyway so even if you change your code to bind to the folder correctly trying to change the name of this folder in this way won't work and will give an error

    Cheers
    Glen

    • Marked as answer by slimhss Wednesday, January 17, 2018 3:11 PM
    Wednesday, January 17, 2018 6:01 AM
  • Thank u Glen For your support

    Wednesday, January 17, 2018 3:11 PM