0

我有一些基本的 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: 

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

改变;

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: 

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

4

1 回答 1

0

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

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

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"

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

于 2019-08-28T04:35:11.367 回答