我有一些基本的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: 有没有人能帮我解释一下我做错了什么,以及为什么我得到了双倍的读主机。
发布于 2019-08-28 12:35:11
我很久以前就解决了这个问题,并想在这里发布解决方案。
我的错误是函数是不正确的,不应该被命名
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"在过去的几个月里,这对我们来说运行得很好。
https://stackoverflow.com/questions/55094043
复制相似问题