locked
Correct Syntax RRS feed

  • Question

  • Dear Community,

    I am running following command in order to enable AD user but it is not functioning. Please suggest, where is Syntax error.

    $chkEmpID=Get-ADUser -Filter * -Properties employeeid | where {$_.employeeid -eq $emp.EMPLID}
    
     $usrnam = $chkEmpID
    
     Enable-ADAccount $usrnam
    
     Get-ADUser $usrnam | Move-ADObject -TargetPath 'OU=CreatedByScript,DC=demo,DC=com'
    
     Start-Sleep -s 30
      
     $chkEmail = $null
    
    
    
     $chkEmail = Get-Mailbox -Identity $usrnam
    
     
     if ($chkEmail -ne $null)
     {
    
     Get-Mailbox $usrnam | Set-Mailbox -HiddenFromAddressListsEnabled $true
    
     Set-MailboxAutoReplyConfiguration -Identity $usrnam -InternalMessage $null -ExternalMessage $null -AutoReplyState disabled
    
     Clear-ADAccountExpiration -Identity $usrnam
     
    

    Wednesday, April 18, 2018 12:03 PM

Answers

  • Try to change

    $chkEmail = Get-Mailbox -Identity $usrnam

    to

    $chkEmail = Get-Mailbox -Identity $usrnam.name
    

    does that work?

    • Marked as answer by Sohaib118 Thursday, April 19, 2018 5:26 AM
    Wednesday, April 18, 2018 12:51 PM
  • Please post only the full fist error message.  Do not remove the error details.

    Get-Mailbox -Identity $user.DistinguishedName


    \_(ツ)_/

    • Marked as answer by Sohaib118 Thursday, April 19, 2018 5:28 AM
    Wednesday, April 18, 2018 6:08 PM
  • The Name property is the Relative Distinguished Name of the user (the Common Name), which does not uniquely identify the user. It need only be unique in the OU or container. DistinguishedName is unique.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by Sohaib118 Monday, April 23, 2018 7:10 AM
    Wednesday, April 18, 2018 8:39 PM

All replies

  • Are you getting any error messages?
    Wednesday, April 18, 2018 12:14 PM
  • I am getting below error:

    Cannot process argument transformation on parameter 'Identity'. Cannot convert value 
    "CN=shahzaib.zahoor,OU=CreatedByScript,DC=demo,DC=com" to type 
    "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". Error: "Cannot convert hashtable to an object of the 
    following type: Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not 
    supported in restricted language mode or a Data section."
        + CategoryInfo          : InvalidData: (:) [Get-Mailbox], ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Mailbox
        + PSComputerName        : exmbx01.demo.com

    Wednesday, April 18, 2018 12:33 PM
  • Try to change

    $chkEmail = Get-Mailbox -Identity $usrnam

    to

    $chkEmail = Get-Mailbox -Identity $usrnam.name
    

    does that work?

    • Marked as answer by Sohaib118 Thursday, April 19, 2018 5:26 AM
    Wednesday, April 18, 2018 12:51 PM
  • Nopes: now getting below error :(

    Cannot process argument transformation on parameter 'Identity'. Cannot convert value 
    "CN=shahzaib.zahoor,CN=Users,DC=demo,DC=com" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". 
    Error: "Cannot convert hashtable to an object of the following type: 
    Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported in 
    restricted language mode or a Data section."
        + CategoryInfo          : InvalidData: (:) [Get-Mailbox], ParameterBindin...mationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Mailbox
        + PSComputerName        : exmbx01.demo.com
     
    Cannot process argument transformation on parameter 'Identity'. Cannot convert value 
    "CN=shahzaib.zahoor,CN=Users,DC=demo,DC=com" to type "Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter". 
    Error: "Cannot convert hashtable to an object of the following type: 
    Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter. Hashtable-to-Object conversion is not supported in 
    restricted language mode or a Data section."
        + CategoryInfo          : InvalidData: (:) [Set-MailboxAutoReplyConfiguration], ParameterBindin...mationExceptio 
       n
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-MailboxAutoReplyConfiguration
        + PSComputerName        : exmbx01.demo.com
     
    Clear-ADAccountExpiration : Directory object not found
    At C:\ADScripts\CreateUser - New.ps1:341 char:2
    +  Clear-ADAccountExpiration -Identity $usrnam
    +  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CN=shahzaib.zah...,DC=demo,DC=com:ADUser) [Clear-ADAccountExpiration] 
       , ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException 
       ,Microsoft.ActiveDirectory.Management.Commands.ClearADAccountExpiration

    Wednesday, April 18, 2018 1:04 PM
  • Please post only the full fist error message.  Do not remove the error details.

    Get-Mailbox -Identity $user.DistinguishedName


    \_(ツ)_/

    • Marked as answer by Sohaib118 Thursday, April 19, 2018 5:28 AM
    Wednesday, April 18, 2018 6:08 PM
  • The Name property is the Relative Distinguished Name of the user (the Common Name), which does not uniquely identify the user. It need only be unique in the OU or container. DistinguishedName is unique.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by Sohaib118 Monday, April 23, 2018 7:10 AM
    Wednesday, April 18, 2018 8:39 PM
  • This worked. i just realized that $usrnam.name has to be included in below cmdlet as well:

    Get-Mailbox $usrnam.name | Set-Mailbox -HiddenFromAddressListsEnabled $true
    
     Set-MailboxAutoReplyConfiguration -Identity $usrnam.name -InternalMessage $null -ExternalMessage $null -AutoReplyState disabled
    
     Clear-ADAccountExpiration -Identity $usrnam.name

     
    Thursday, April 19, 2018 5:28 AM
  • Thanks JRV. This seems to working as well.
    Thursday, April 19, 2018 5:29 AM