2017-06-21 22 views
0

から返されたプライマリ、セカンダリ、およびテーブルから第三紀:は、私がDeviceNameのを引っ張るSQLクエリを持っているDBクエリ

$SQLQuery= (Invoke-Sqlcmd -Serverinstance $ServerInstance -Database $Database -Query "select DeviceName, Primary, Secondary, Tertiary from dbo.Table") 

私はその後、プライマリとハッシュテーブルにそれらのエントリを投げます値の配列として三紀/ /セカンダリ:

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

私はに実行している問題は、SQLクエリがNULLされていない空白の結果を返すことができるということです。これは、このような$DeviceList外観は作る:

elseif ($item.Primary -and !$item.Secondary -and !$item.Tertiary) 
{ 
    if($item.Primary -ne '') 
    { 
    $DeviceList += @{$item.DeviceName = "$($item.Primary)"} 
    } 
} 

しかし、それは信じられないほどの繰り返しだし、時間がかかる:

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

私は声明のような場合には、繰り返しに投げることによって、これらをフィルタリングすることができます。私はそれらを無視するようにSQLクエリを変更することもできますが、SQLクエリを単純にしてPowerShellでフィルタリングするほうがはるかに好きです。それを行うより簡単な方法があります。 、私の問題は意味をなさない場合

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

してください:私が探しています

最終的な結果は、エントリが空白の場合、それはちょうどそう$DeviceListは次のようになり、スキップされることをです私に知らせてください、私は明確にしようとします。いつものように、ありがとう!

答えて

2

ビルドアップインクリメンタルデバイスごとのリスト:Invoke-SqlcmdNULLまたはDBNull返された場合

$DeviceList = @{}; 
foreach ($item in $SQLQuery) 
{ 
    $arr = @(); 
    if($item.Primary -ne $null -and $item.Primary -ne [System.DBNull]::Value -and $item.Primary -ne '') 
    { 
     $arr += "$($item.Primary)"; 
    } 
    if($item.Secondary -ne $null -and $item.Secondary -ne [System.DBNull]::Value -and $item.Secondary -ne '') 
    { 
     $arr += "$($item.Secondary)"; 
    } 
    if($item.Tertiary -ne $null -and $item.Tertiary -ne [System.DBNull]::Value -and $item.Tertiary -ne '') 
    { 
     $arr += "$($item.Tertiary)"; 
    } 
    if ($arr.count -gt 0) 
    { 
     $DeviceList += @{$item.DeviceName = $arr}; 
    } 
} 

は私はわからないので、私は両方のチェックを含めました。

+0

Andy、実際には、$ arr = @ {}を '$ arr = @()'に変更した後、最初の答えは完全に機能しました。あなたは私の人生をずっと簡単にしました、ありがとう! – Nick

0

私はSQLがインストールされていないため、WMIを持つ例:

(Get-WMIObject -Class Win32_Bios).PSObject.Properties|? Value -ne $NULL|ft Name, Value 

これが唯一の非nullのプロパティを返しますが、すべてのWin32_Biosクラスの機能を失うことになります。私はあなたのコードのコンテキストを考えれば、これはうまくいくはずだと思います。

関連する問題