在powershell中将不同的输出放入一个数组/哈希表时出现问题
我正在尝试使用在powershell中将不同的输出放入一个数组/哈希表时出现问题,powershell,exchange-server,Powershell,Exchange Server,我正在尝试使用get-MailboxDatabaseCopyStatus-Server$ExchSvr将几个Exchange服务器上的几个Exchange DB的状态信息放入powershell中的一个整洁的表中 因此,对于每台服务器,您都会得到以下信息: 名称 地位 DB1 安装 DB2 健康的 ... ... 您可以从在嵌套哈希表中组织状态信息开始: # Discover all servers $ExchServers = Get-ExchangeServer # Create the
get-MailboxDatabaseCopyStatus-Server$ExchSvr
将几个Exchange服务器上的几个Exchange DB的状态信息放入powershell中的一个整洁的表中
因此,对于每台服务器,您都会得到以下信息:
名称
地位
DB1
安装
DB2
健康的
...
...
您可以从在嵌套哈希表中组织状态信息开始:
# Discover all servers
$ExchServers = Get-ExchangeServer
# Create the "outer" table
$statusTable = @{}
# Fetch mailbox db copy status per server, store each copy status as an entry in an inner table
foreach($server in $ExchServers){
# Create new inner table for this particular server
$statusTable[$server.Name] = @{}
foreach($dbCopyStatus in Get-MailboxDatabaseCopyStatus -Server $server){
# Populate inner table
$statusTable[$server.Name][$dbCopyStatus.Name] = $dbCopyStatus.Status
}
}
现在,我们只需要将数据转换为所需的格式—每个数据库一个对象,每个托管服务器具有单独的属性:
$outputPerDB = foreach($db in Get-MailboxDatabase){
# create a table to hold the properties on the object we're about to create
$properties = [ordered]@{ Name = $db.Name }
foreach($server in $ExchServers){
# grab property name from server, grab status from status table
$properties[$server.Name] = $statusTable[$server.Name][$db.Name]
}
# turn property table into custom object
[pscustomobject]$properties
}
现在,生成的对象将按预期进行格式化:
PS ~> $outputPerDB |Format-List
Name ExchSrv1 ExchSrv2
---- -------- --------
DB1 Mounted Healthy
DB2 Healthy Mounted
假设您在一组数据库中循环,然后在该数据库中的所有$exchsvr中循环,您会希望执行以下操作:
[System.Collections.ArrayList] $outputTable = @()
foreach ($dbServer in $dbServers) {
$obj = [PSCustomObject] @{
Name = $dbServer.Name
}
foreach ($ExchSvr in $ExchSvrs) {
$status = Get-MailboxDatabaseCopyStatus -Server $ExchSvr
$obj | Add-Member -NotePropertyName $ExchSvr.Name -NotePropertyValue $status
}
$outputTable += $obj
}
$outputTable
但是,要使表正确对齐,每个数据库的所有$EXCHSVR都必须相同。如果每个数据库行的$ExchSvr不同,您必须做一些额外的工作来重用$obj,并检查成员是否已经存在。因此
Get-MailboxDatabaseCopyStatus-Server$ExchSvr
为您提供最高的输出?exch 1和“exch2”在哪里来自?很抱歉,我不太清楚我正在遍历所有服务器foreach($ExchSvrs中的ExchSvr){Get-MailboxDatabaseCopyStatus-Server$ExchSvr}
这会给我每个服务器的最高输出,所以exchange服务器1、2、3等(Exch Svr1、Exch Svr2、Exch Svr3等)我将为每个服务器生成一次顶部输出,并希望将所有这些输出合并到一个表中,每个exchange服务器有一列,每个数据库有一行。