none
Request for guidance for Enable-mailbox script RRS feed

  • Question

  • I have a scenario wherein I would like to find if firstname and last name exist in org AD, in case yes, it should use middle name in SMTP policy (customized email address) and in case no, then use simple SMTP policy. Can you please guide me :
    
    Import-Module ActiveDirectory -ErrorAction Stop
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    #Create Mailbox
    
    Import-Module ActiveDirectory -ErrorAction Stop
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    #Create Mailbox
    
    $OU = "OU=QCS-Sync,OU=Users,OU=Corpnet,DC=domain,DC=com"
    $sam = Get-User -ResultSize "Unlimited" -OrganizationalUnit "OU=QCS-Sync,OU=Users,OU=Corpnet,DC=domain,DC=com" | Where-Object {$_.RecipientType -eq "User"} | select SamAccountName
    $FirstName = Get-User -identity $_.sam | select FirstName 
    $LastName = Get-User -identity $_.sam | select LastName
    $Middle = Get-User -identity $_.sam | select Initials
    $EmailAddWMid = ($FirstName +"."+ $Middle +"."+ $LastName +"@domain.com")
    If ($Middle -eq "")
    {
    Get-User -ResultSize "Unlimited" -OrganizationalUnit "OU=QCS-Sync,OU=Users,OU=Corp,DC=domain,DC=com" | Where-Object {$_.RecipientType -eq "User"} | Enable-Mailbox -Database DB2
    }
    Else
    {
    Get-User -ResultSize "Unlimited" -OrganizationalUnit "OU=QCS-Sync,OU=Users,OU=Cor,DC=domain,DC=com" | Where-Object {$_.RecipientType -eq "User"} | Enable-Mailbox -Database DB2 | Set-mailbox -IgnoreDefaultScope -EmailAddressPolicyEnabled $false -PrimarySmtpAddress $EmailAddWMid
    }
    ​I thank you a ton for your guidance. I have been trying since a week now and I am afraid I am not so good in powershell.​
    • Moved by Bill_Stewart Monday, November 23, 2015 10:45 PM This is not "fix script I found on the Internet for me" forum
    Thursday, September 17, 2015 11:45 PM

All replies

  • $OU='OU=QCS-Sync,OU=Users,OU=Corpnet,DC=domain,DC=com'
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    
    Get-User -ResultSize "Unlimited" -OrganizationalUnit $ou |
        ForEach-Object{
            if($_.Middle){
                $newmail = '{0}.{1}.{2}@domain.com' -f $_.FirstName, $_.Initials, $_.LastName
                $_ | Set-Mailbox -IgnoreDefaultScope -EmailAddressPolicyEnabled $false -PrimarySmtpAddress $newmail
                $_ | Enable-Mailbox
            }
        }

    \_(ツ)_/




    • Edited by jrv Friday, September 18, 2015 1:04 AM
    Friday, September 18, 2015 1:02 AM
  • Thank you JRV for your response.

    I tried running it, however it does nothing and does not even give any error back.

    If it is not possible to store in variable, can I use Read-Host to input email address manually, only if firstname last name already exist in domain.

    Friday, September 18, 2015 12:06 PM
  • Sorry.

    This line:

      if($_.Middle){

    should be this:

      if($_.Initials){


    \_(ツ)_/

    Friday, September 18, 2015 3:55 PM
  • worked  great !! thanks so much, you are a savior for me... one last help if you can do, no hurries for this, whenever you get some free time, Can you add a logic here as when user name with same firstname and lastname exist, it should respond for that user and then I can use below code to select the option:

    Write-Host "Choose a Task : "

    Write-Host " [1] : Enable Mailbox for a User"

    Write-Host " [2] : Enable Mailbox for all Users in an Organizational Unit" 

    Write-Host ""

    $Choose = Read-Host

    Switch ($Choose) {

    1 {

     $UserName = Read-Host "Enter the User or Group Alias you want to create a Mailbox"

     $DBName = Read-Host "Enter the DB Name you want to create the Mailbox in" 

     $SMTPADD = Read-Host "Enter Email Customized Email Address " 

     Write-Host "Creating mailbox for User : "$UserName" to DB :"$DBName 

     Sleep-Sleep -s 20

     Enable-Mailbox -Identity $UserName -PrimarySMTPAddress $SMTPADD -Database $DBName

      }



    2 {

     $DBName = Read-Host "Enter the DB Name you want to create the Mailboxes in" 

     $OU = 'OU=QCS-Sync,OU=Users,OU=Corpnet,DC=domain,DC=com'

     Write-Host "Creating All Mailboxes in OU : "$OU" ,in DataBase "$DBName -ForegroundColor Green

     Sleep-Sleep -s 30

     Get-User -OrganizationalUnit $OU | where {$_.RecipientType -eq "User"} | Enable-Mailbox -Database $DBname

      }
    }

     
    Friday, September 18, 2015 5:07 PM
  • Request to review the script:

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
    $newpath  = $path + "\import_create_ad_users.csv"
    $log      = $path + "\create_ad_users.log"
    $OU='OU=QCS-Sync,OU=Users,OU=Corp,DC=domain,DC=com'
    Function Start-Commands
    {
      Create-Mbxs
    }

    Function Create-Mbxs
    {
      "Processing started (on " + $date + "): " | Out-File $log -append
      "--------------------------------------------" | Out-File $log -append
      Import-CSV $newpath | ForEach-Object {
        If (($_.Implement.ToLower()) -eq "yes")
        {
          If (($_.GivenName -eq "$_.FirstName") -AND ($_.LastName -eq "$_.LastName"))
          {
    Write-Host "[INFO]`t User with same name exist in Organization, Creating user with Middle name Initial for Primary SMTP"
    "[INFO]`t User with same name exist in Organization, Creating user with Middle name Initial for Primary SMTP"| Out-File $log -append
    Get-User -ResultSize "Unlimited" -OrganizationalUnit $ou | where {$_.RecipientType -eq "User"} |
    ForEach-Object{
            if($_.Initials){
                $newmail = '{0}.{1}.{2}@domain.com' -f $_.FirstName, $_.Initials, $_.LastName
       $_ | Enable-Mailbox -Database DB2
                $_ | Set-Mailbox -IgnoreDefaultScope -EmailAddressPolicyEnabled $false -PrimarySmtpAddress $newmail
                }
        }
    }
          Else
          {
    Get-User -ResultSize "Unlimited" -OrganizationalUnit "OU=QCS-Sync,OU=Users,OU=Corpnet,DC=domain,DC=com" | Where-Object {$_.RecipientType -eq "User"} | Enable-Mailbox -Database CBT-DB2
    Write-Host "[INFO]`t No Existing firstname and lastname in Org, Creating normal user"
    "[INFO]`t No Existing firstname and lastname in Org, Creating normal user" | Out-File $log -append
    }

    Saturday, September 19, 2015 5:01 PM