我有一个处理我们所有 SQL Server 的脚本。该脚本有几个函数,我有一个错误例程,它将服务器名称、函数名称和错误消息记录到一个 500 行 x 3 列的数组中。在脚本结束时,我想将此数组排序为服务器名称序列。有几篇文章建议我只需将数组通过管道传输到sort-object
cmdlet,但是当我这样做时,数组的每个元素都将替换为system.Object[]
. 注意。函数之前的数组填充只是我的数组的一个例子
$global:ErrorCount = 0
$global:ErrArray = new-object 'object[,]' 500,3
$global:ErrArray[1,00]= "SV000004"
$global:ErrArray[1,01]= "ProcessServers"
$global:ErrArray[1,02]= "The server was not found or was not accessible."
$global:ErrArray[2,00]= "BOSWEB02"
$global:ErrArray[2,01]= "GetDatabases"
$global:ErrArray[2,02]= "Database Status = Shutdown"
$global:ErrArray[3,00]= "SATURN"
$global:ErrArray[3,01]= "GetDatabases"
$global:ErrArray[3,02]= "Database Status = Shutdown"
$global:ErrArray[4,00]= "BOSWEB02"
$global:ErrArray[4,01]= "GetSystemInfo"
$global:ErrArray[4,02]= "Access is denied"
$global:ErrorCount = 4
Function DisplayErrors
{
Write-Host "`nBefore:-`n"
for ( $iLoop=1; $iLoop -le $global:ErrorCount; $iLoop++)
{
"{0,-14} {1,-18} {2,-80}" -f
$global:ErrArray[$iLoop,0], $global:ErrArray[$iLoop,1],
$global:ErrArray[$iLoop,2]
}
$Sorted = $global:ErrArray | Sort-Object @{Expression={$_[0]}}
Write-Host "`nAfter:-`n"
for ( $iLoop=1; $iLoop -le $global:ErrorCount; $iLoop++)
{
"{0,-14} {1,-18} {2,-80}" -f
$Sorted[$iLoop,0], $Sorted[$iLoop,1], $Sorted[$iLoop,2]
}
}
DisplayErrors
输出如下所示:-
前:-
SV000004 ProcessServers The server was not found or was not accessible.
BOSWEB02 GetDatabases Database Status = Shutdown
SATURN GetDatabases Database Status = Shutdown
BOSWEB02 GetSystemInfo Access is denied
后:-
System.Object[] System.Object[] System.Object[]
System.Object[] System.Object[] System.Object[]
System.Object[] System.Object[] System.Object[]
System.Object[] System.Object[] System.Object[]
谁能告诉我我在这里做错了什么?
非常感谢 :-)