Powershell Get-mailboxdatabase和创建共享邮箱脚本

时间:2019-03-11 01:16:40

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

我具有一些Powershell的基本知识,我正在尝试修改Service Desk上的现有脚本以在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: 

有人能帮助我弄清楚我做错了什么以及为什么我得到了两倍的读取主机。

1 个答案:

答案 0 :(得分:0)

不久前就发现了这个问题,并想在此处发布解决方案。

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

(os.sep)

这引起了问题,因为我正在使用现有cmdlet名称(DERP)创建函数

我将脚本更改为以下内容

import arcpy
inFolder = "aaa"
resultsFolder = "{:s.xml}"
clipFeature = "aaa\\{:s.xml}"

# List feature classes
arcpy.env.workspace = inFolder
featureClassList = arcpy.ListFeatureClasses()

# Loop through each feature class and clip
for featureClass in featureClassList:

    # Make the output path by concatenating strings
    outputPath = resultsFolder + featureClass
    # Clip the feature class
    arcpy.Clip_analysis(featureClass, clipFeature, outputPath)

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