Exchange cmdlet error change in PS 3 vs PS 4

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of wenniss wenniss 2 years, 4 months ago.

  • Author
    Posts
  • #17798
    Profile photo of wenniss
    wenniss
    Participant

    Hello all,

    I have a fairly simple cmdlet that was built for some of my Help Desk staff to activate new AD accounts, and it seems to run without issue on Powershell 3, but now that we have a few enterprising folk who have moved up to Powershell 4 they're receiving and error now and I'm not sure as to why so I thought I'd turn to the experts.

    Function New-User
    {    
        [CmdletBinding()]
        Param(
        [Parameter(Mandatory=$True,Position=1)]
        [string]$UserName,
    
        [Parameter(Mandatory=$False)]
        [string]$DataBase
        )
        
        
        Enable-ADAccount $UserName
        Get-ADUser $UserName | Move-ADObject -TargetPath "OU=Desktop,OU=Users,DC=Contoso,DC=com"
        Enable-Mailbox $UserName -Alias $UserName -Database $DataBase -ErrorAction SilentlyContinue
        
    
    }
    

    Since all users do not receive mailboxes, if nothing is filled out in the Database parameter the intention was to just continue along without showing the error message which worked fine. But now, for those that are on Powershell 4.0 when they run the command without specifying anything under Database, they get the following message :

    Cannot process argument transformation on parameter 'Database'. Cannot convert value "" to type
    "Microsoft.Exchange.Configuration.Tasks.DatabaseIdParameter". Error: "Parameter values of type
    Microsoft.Exchange.Configuration.Tasks.DatabaseIdParameter can't be empty. Specify a value, and try again.
    Parameter name: identity"
    + CategoryInfo : InvalidData: (:) [Enable-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Enable-Mailbox
    + PSComputerName : mail.contoso.com

    I'm assuming that since the error message would be a terminating error, -ErrorAction isn't respected and it displays the message. But really my question is what's different between Powershell 3 vs. 4 where the error results for Exchange cmdlets have changed? The Exchange environment is 2010 if it makes a difference.

  • #17800
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Your assumption is correct. The difference isn't between 3 and 4, it's how the cmdlet is producing the information that's causing the shell to react. The 2010 cmdlets explicitly dislike v4; they're built against v3.

    I'd try modifying your function so that, if $Database isn't specified, you don't include the -Database parameter. A hashtable and splatting can be a nice way to do that:

    $params = @{'UserName'=$Username;'Alias'=$UserName;'ErrorAction'='SilentlyContinue'}
    If ($psboundparameters.containskey('Database')) { 
      $params.add('Database',$Database)
    }
    Enable-Mailbox @params
    

    Something like that. I don't know what parameter is in the first position; I assumed it's -UserName but if it's -Identity or something else, you'll need to adjust the code.

  • #17812
    Profile photo of wenniss
    wenniss
    Participant

    Now that is going to give me some interesting reading to do, thank you very much Don!

You must be logged in to reply to this topic.