【问题标题】:Powershell Get-mailboxdatabase and Create Shared Mailbox ScriptPowershell 获取邮箱数据库并创建共享邮箱脚本
【发布时间】:2019-03-11 01:16:39
【问题描述】:

我有一些基本的 Powershell 知识,我正在尝试修改我们服务台上的现有脚本,以便在 Exchange 2010 中创建一个共享邮箱。

当前版本已设置,因此用户可以输入要分配邮箱的数据库。

我正在尝试做的修订版本是假设提取数据库并显示每个数据库的大小。那么这个想法是用户可以简单地输入一个数字值来表示一个数据库,而不是写出整个数据库。

所以在做了一些研究之后,我尝试了以下方法;

$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"

这有点工作,但它不显示邮箱数据库,正如在下面的示例中看到的那样,它做了双倍的 readhost 进入数据库

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: 

所以我找到了Show output before Read-Host,我尝试看看这是否有助于在输入值之前显示邮箱数据库。

改变了;

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

到;

$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

但出现以下错误

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 'testscript@domain.com' 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: 

有没有人能够帮助阐明我做错了什么以及为什么我得到了双倍的读取主机。

【问题讨论】:

    标签: database function powershell exchange-server-2010 read-host


    【解决方案1】:

    不久前想出了这个问题,并想在这里发布解决方案。

    我的错误是函数不正确,不应该命名

    function Get-MailboxDatabase
    

    当我使用现有的 cmdlet 名称 (DERP) 创建函数时,这会导致问题

    我将脚本更改为以下内容

    $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"
    

    在过去的几个月里,这对我们来说一直很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-29
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多