1

すべてのSQLServerを処理するスクリプトがあります。スクリプトにはいくつかの関数があり、サーバー名、関数名、エラーメッセージを500行x3列の配列に記録するエラールーチンがあります。スクリプトの最後で、この配列をサーバー名の順序に並べ替えます。いくつかの投稿では、配列をsort-objectコマンドレットにパイプするだけでよいと示唆されていますが、これを行うと、配列のすべての要素がに置き換えられsystem.Object[]ます。NB。関数の前の配列の塗りつぶしは、私の配列がどのように見えるかの単なる例です

$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[]  

ここで私が間違っていることを誰かに教えてもらえますか?

どうもありがとう :-)

4

3 に答える 3

1

この方法で配列を作成する (ギザギザの配列: array[ ][ ] )sort-objectことができます:

$global:ErrArray += ,@("SV000004","ProcessServers","The server was not found or was not accessible.")
$global:ErrArray += ,@("BOSWEB02","GetDatabases","Database Status = Shutdown")
$global:ErrArray += ,@("SATURN","GetDatabases","Database Status = Shutdown")
$global:ErrArray += ,@("BOSWEB02","GetSystemInfo","Access is denied" )

Function DisplayErrors
{
    Write-Host "`nBefore:-`n"

    foreach ( $server in $global:Errarray)
    {
    write-host $server 
    }

    $sorted = $global:ErrArray | Sort-Object @{Expression={$_[0]}}

    Write-Host "`nAfter:-`n"    

   foreach ( $server in $sorted)
    {
        write-host $server    
    }
}  

DisplayErrors

またはあなたのコードのように:

$global:ErrArray += ,@("SV000004","ProcessServers","The server was not found or was not accessible.")
$global:ErrArray += ,@("BOSWEB02","GetDatabases","Database Status = Shutdown")
$global:ErrArray += ,@("SATURN","GetDatabases","Database Status = Shutdown")
$global:ErrArray += ,@("BOSWEB02","GetSystemInfo","Access is denied" )



Function DisplayErrors
{
    Write-Host "`nBefore:-`n"

for ( $iLoop=0; $iLoop -lt $global:errarray.count; $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=0; $iLoop -lt $sorted.count; $iLoop++)
    {
        "{0,-14}  {1,-18}  {2,-80}" -f   $Sorted[$iLoop][0], $Sorted[$iLoop][1], $Sorted[$iLoop][2]
    }
}  

DisplayErrors
于 2012-06-26T12:18:02.140 に答える
0

Powershell が配列を台無しにする理由はわかりませんが、Powershell らしい回避策がもっとあるかもしれません。データとそれらの 1 次元配列を含むカスタム オブジェクトを作成しましょう。並べ替えは簡単です。

# Custom error object
function CreateErrorObject {
    param ([string]$server, [string]$proc, [string]$message)
    $objError = New-Object System.Object
    $objError | Add-Member -type NoteProperty -name Server -value $server
    $objError | Add-Member -type NoteProperty -name Proc -value $proc
    $objError | Add-Member -type NoteProperty -name Message -value $message
    $objError
}

$errors = @() # Empty array

# Populate error objects some way
$errors += CreateErrorObject "SV000004" "ProcessServers" "The server was not found or was not accessible"
$errors += CreateErrorObject "BOSWEB02" "GetDatabases" "Database Status = Shutdown"
$errors += CreateErrorObject "SATURN" "GetDatabases" "Database Status = Shutdown"
$errors += CreateErrorObject "BOSWEB02" "GetSystemInfo" "Access is denied"


$errors # Unsorted list
$errors | sort -Property server # sort by server property
$errors | sort -Property proc # sort by proc property
$errors | sort -Property message # sort by message property
于 2012-06-26T11:56:02.213 に答える