Powershell Get-mailboxdatabase and Create Shared Mailbox Script

1.2k Views Asked by At

I have some basic Powershell knowledge and i am trying to revise an existing script on our Service Desk to make a shared mailbox in Exchange 2010.

The current version was setup so the user can input the database to assign the mailbox to.

The revised version i am trying to do is suppose to pull the Databases and display the size of each database. Then the idea is the user can simply input a number value to represent a database, rather than writing out the whole database.

So after doing some research i tried out the following;

$mailboxname=Read-Host “Enter mailbox name”
$alias=Read-Host “Enter Email Alias”
$User=$alias + "@domain.com"

Get-MailboxDatabase -Server "Server" -Status | Where-Object {$_.name -like "Database*"} | Sort-Object -Descending -Property @{Expression = "name"; Descending = $true} | Select Name,Databasesize  

$script:ChosenDatabase=Get-MailboxDatabase

function Get-MailboxDatabase 

{   
$database=Read-Host "Enter database using a value of 1 to 4 to add the mailbox to"

Switch ($database)
    {
        1 {$Chosendatabase="Database-1"}
        2 {$Chosendatabase="Database-2"}
        3 {$Chosendatabase="Database-3"}
        4 {$Chosendatabase="Database-4"}    
}
    return $Chosendatabase
    }



New-mailbox -shared -Name $mailboxname -alias $alias -UserPrincipalName $User -OrganizationalUnit "Domain.com/Resources-OU" -Database $Chosendatabase

Get-mailbox -Identity $User | ft DisplayName,Database


read-host "hit enter to close window"

This kinda works, but it doesn't show the Mailbox Database and as can be seen in the example below it did a double up of the readhost to enter the database

Enter mailbox name: testscript2
Enter Email Alias: testscript2
Enter database using a value of 1 to 4 to add the mailbox to: 2
Enter database using a value of 1 to 4 to add the mailbox to: 2

Name                      Alias                ServerName       ProhibitSendQuota                       


----                      -----                ----------       -----------------                       


testscript2            testscript2          Server      unlimited                               





DisplayName                                                           Database                          


-----------                                                           --------                          


testscript2                                                          Database-2                         




hit enter to close window: 

So i found Show output before Read-Host, which i tried out to see if this will help show the mailboxdatabase before inputting a value.

Changed;

Get-MailboxDatabase -Server "Server" -Status | Where-Object {$_.name -like "Database*"} | Sort-Object -Descending -Property @{Expression = "name"; Descending = $true} | Select Name,Databasesize

To;

$getDB=Get-MailboxDatabase -Server "Server" -Status | Where-Object {$_.name -like "Database*"} | Sort-Object -Descending -Property @{Expression = "name"; Descending = $true} | Select Name,Databasesize | Out-String; 

Write-Host $getDB

But got the following errors

Enter mailbox name: testScript
Enter Email Alias: testscript

Name                                                                  DatabaseSize                      


----                                                                  ------------                      


Database-4                                                 762.8 GB              


Database-3                                                 376.3 GB              


Database-2                                                 249.3 GB              


Database-1                                                 829.8 GB             





Cannot process argument transformation on parameter 'Database'. Cannot convert the 

"System.Collections.ArrayList" value of type 
"System.Collections.ArrayList" to type "Microsoft.Exchange.Configuration.Tasks.DatabaseIdParameter".
    + CategoryInfo          : InvalidData: (:) [New-Mailbox], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-Mailbox
    + PSComputerName        : Domain.com

The operation couldn't be performed because object '[email protected]' couldn't be found on 

'Domain.com'.
    + CategoryInfo          : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : 8D2D2EF6,Microsoft.Exchange.Management.RecipientTasks.GetMailbox
    + PSComputerName        : Domain.com

hit enter to close window: 

Is anybody able to help shed some light on what i am doing wrong and why I am getting a double of the read-host.

1

There are 1 best solutions below

0
On

Figured this problem out awhile ago and thought to post the solution here.

My mistake was the function was incorrect and shouldn't of been named

function Get-MailboxDatabase

This caused the issue as i was creating a function using an existing cmdlet name (DERP)

I changed my script to the following

$data = Get-MailboxDatabase -Server "Server" -Status | Where-Object {$_.name -like "DATABASE*"} | Sort-Object -Property @{Expression = "name"} | Select Name,Databasesize | ft | Out-String

function WORK
{   

Write-host $data 
Write-host "Pick the database with the lowest size"
Write-host


$database=Read-Host "Enter the database using a value of 1 to 4 to add the mailbox to"

Switch ($database)
    {
        1 {$Chosendatabase="DATABASE-1"}
        2 {$Chosendatabase="DATABASE-2"}
        3 {$Chosendatabase="DATABASE-3"}
        4 {$Chosendatabase="DATABASE-4"}    
}
    return $Chosendatabase


}   

$date=Get-Date -format d
$mailboxname=Read-Host “Enter the mailbox name”
$alias=Read-Host “Enter Email Alias”
$User=$alias + "@domain.com"
$ticket=Read-Host "Enter the Ticket number"
$notes="Mailbox created - $ticket - $date"


Read-Host "hit enter to Continue"


$script:ChosenDatabase = WORK



New-mailbox -shared -Name $mailboxname -alias $alias -UserPrincipalName $User -OrganizationalUnit "domain.com/Resources-OU" -Database $Chosendatabase

Set-user -identity $alias -notes "$Notes"

##This command is to make sure a copy of sent emails are stored on the shared mailbox as well as the senders mailbox    
Set-MailboxSentItemsConfiguration -Identity $alias -SendAsItemsCopiedTo SenderAndFrom -SendOnBehalfOfItemsCopiedTo SenderAndFrom


##bring back confirmation the script has done as tended
Get-mailbox -Identity $User | ft DisplayName,Database

Get-mailboxsentitemsconfiguration -Identity $alias


read-host "hit enter to close window"

This has been working fine for us for the past few months