2017-05-23 13 views
0

SQLから情報を引き出すと、デバイス名、AppownerPrimary、Secondary、およびTertiaryの電子メールアドレスが収集されます。 0〜3個のメールアドレスがあります。この問題では、基本的にデバイス名を無視できます。PowerShellグループハッシュテーブル値を取得する配列

enter image description here

私は、foreachループを使用してハッシュテーブルにこの情報を投げる:

$DeviceList = @{} 

$DeviceLists = (Invoke-Sqlcmd -Serverinstance $ServerInstance -Database $Database -Query "select devicename, appownerprimaryemail, Secondary, Tertiary from dbo.Table order by AppOwnerPrimaryEmail") 

foreach ($item in $devicelists) 
{ 
if($item.appownerprimaryemail -and $item.Secondary -and $item.Tertiary) 
{ 
    $DeviceList += @{$item.DeviceName = "$($item.AppOwnerPrimaryEmail)", "$($item.Secondary)", "$($item.Tertiary)"} 
} 
elseif ($item.appownerprimaryemail -and $item.Secondary -and !$item.Tertiary) 
{ 
    $DeviceList += @{$item.DeviceName = "$($item.AppOwnerPrimaryEmail)", "$($item.Secondary)"} 
} 
elseif ($item.appownerprimaryemail -and !$item.Secondary -and !$item.Tertiary) 
{ 
    $DeviceList += @{$item.DeviceName = "$($item.AppOwnerPrimaryEmail)"} 
} 
} 

ハッシュテーブルはこのように見える終わる:

enter image description here

私は単に必要一意のメールグループが表示されるたびにカウントを取得します。私は、スクリプトを書き始めたとき、そこに私はSQLクエリの電子メールでソートしています唯一のメールだったので、私は、グループオブジェクトを使用することができました:

[array]$UniqueEmails = $DeviceList.Values | Group-Object 

0-3、グループがあるかもしれませんオブジェクトは全く動作していません。それはすべてのデバイスのすべての電子メールをまとめているようです。私はグループオブジェクトがハッシュテーブルを好きではないことを知っていますが、なぜこのようなことが起こっているのか分かりません。しかし、この記事のポイントは、私はそれぞれの一意の値の文字列の数だけを必要とすることです。

$Devicelist = @{"Device1" = "[email protected]"; "Device7" = "[email protected]"; "Device8" = "[email protected]"; "Device2" = "[email protected]", "[email protected]"; "Device3" = "[email protected]", "[email protected]"; "Device4" = "[email protected]", "[email protected]", "[email protected]"; "Device5" = "[email protected]", "[email protected]", "[email protected]"; "Device6" = "[email protected]", "[email protected]", "[email protected]"} 
[array]$UniqueEmails = $DeviceList.values | Group-Object 

私はすべての項目を列挙し、ループする必要があると思う:

この例を使用して、私はあなたがこれを実行して、問題を再現することができます

enter image description here

この出力が期待されますハッシュテーブルの値の配列で、同時に私はグループとしてそれらを扱う必要があるので、私は損失に瀕しています。どんな助けでも大歓迎です。

私が明確でない場合は、私に知らせてください。私はより良く説明するために最善を尽くします。

答えて

1
$devicelist = @{ 
    "Device1" = "[email protected]"; 
    "Device7" = "[email protected]"; 
    "Device8" = "[email protected]"; 
    "Device2" = "[email protected]", "[email protected]"; 
    "Device3" = "[email protected]", "[email protected]"; 
    "Device4" = "[email protected]", "[email protected]", "[email protected]"; 
    "Device5" = "[email protected]", "[email protected]", "[email protected]"; 
    "Device6" = "[email protected]", "[email protected]", "[email protected]" 
} 

$uniqueEmails = @{} 

# Get all the values in the list, sort them 
# and for each, use the value as the key in the unique emails list 
# then for that key add 1 every time the key repeats 
$devicelist.Values | Sort | % { $uniqueEmails["$_"] += 1 } 

Write-Host ($uniqueEmails | Out-String) 

# Outputs 
Name       Value 
----       ---- - 
[email protected]  3 
[email protected] Sally.J... 3 
[email protected] Jane.D... 2 

更新

私はポッシュV1、ポッシュv2とv3の上流階級でこれをテストし、そしてそれは同様に2012サーバーR2上で正常に動作します。問題はあなたのサーバーにある必要があります。 $ PSVersionTable.PSVersionを実行し、使用しているバージョンを確認してください。

古いPoshバージョンは、powershell -version 1 | 2 | 3で起動できます。

+0

'+ = 1'は何をすべきでしょうか? 「[email protected]」という値を「System.Int32」と入力できないというエラーがスローされます。エラー:「入力文字列が正しい形式ではありませんでした」。 – Nick

+0

その値のカウントが増えます。このコードは貼り付けたままで動作します。問題は、デバイスリストのデータが何であれ、私が持っている$ deviceListと一致しないということです。 –

+0

返信から直接コピーして貼り付けると、投稿したデバイスリストを使用して同じエラーメッセージが表示されます。私はPSバージョン5.0.10586.117にあります – Nick