如何过滤从数据库查询

时间:2017-06-21 13:27:43

标签: powershell

我有一个SQL查询,它从表中提取DeviceName,Primary,Secondary和Tertiary:

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

然后我将这些条目放入哈希表中,其中Primary / Secondary / Tertiary作为值中的数组:

$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看起来像这样:

$devicelist = @{
    "Device1" = "Sally.Johnson@Domain.com";
    "Device7" = "Sally.Johnson@Domain.com";
    "Device8" = "Jane.Doe@Domain.com";
    "Device2" = "Bob.Davis@domain.com", "Jane.Doe@domain.com";
    "Device3" = "Bob.Davis@domain.com", "";
    "Device4" = "Jane.Doe@domain.com", "", "Bob.Davis@domain.com";
    "Device5" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com";
    "Device6" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", ""
}

我可以通过抛出重复的if语句来过滤它们,例如:

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

但这非常重复,而且非常耗时。我也可以修改SQL查询来忽略它们,但我更喜欢保持SQL查询简单并在PowerShell中进行过滤。必须有一种更简单的方法来做到这一点。

我正在寻找的最终结果是,如果条目为空,则只会跳过,因此$DeviceList将如下所示:

$devicelist = @{
    "Device1" = "Sally.Johnson@Domain.com";
    "Device7" = "Sally.Johnson@Domain.com";
    "Device8" = "Jane.Doe@Domain.com";
    "Device2" = "Bob.Davis@domain.com", "Jane.Doe@domain.com";
    "Device3" = "Bob.Davis@domain.com";
    "Device4" = "Jane.Doe@domain.com",  "Bob.Davis@domain.com";
    "Device5" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com", "Bob.Davis@domain.com";
    "Device6" = "Jane.Doe@domain.com", "Sally.Johnson@domain.com"
}

如果我的问题没有意义,请告诉我,我会尽力澄清。一如既往,谢谢!!

2 个答案:

答案 0 :(得分:2)

逐步建立每个设备的列表:

$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};
    }
}

我不确定Invoke-Sqlcmd是否会返回NULLDBNull,所以我已经检查了两者。

答案 1 :(得分:0)

WMI的一个示例,因为我没有安装SQL:

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

这将仅返回非null属性,但会丢失所有Win32_Bios类功能。我认为根据代码的上下文,这应该没问题。