在powershell中使用-eq跨两个数据集

时间:2015-03-22 10:56:07

标签: powershell datatable dataset powershell-v2.0 powershell-v3.0

我在这里。我在powershell中有两个数据集。数据集1( $ table )通过sql查询(从12到17行变化,有8列)和数据集2( $ team )在脚本(有18行和2列)中进行了硬编码。这两个都有一个共同的列,比赛。现在我必须运行的脚本是 - 对于 $ table.contest 中的每个比赛,从 $ table 中获取其他相应的参数并匹配 $ team.contest 中的比赛,并获得相应的 $ team.column2 值。

我能够从每个表中单独获取数据,但是当我在 $ table.contest &中使用“-eq”条件时 $ team.contest ,没有任何反应。

这是我遇到问题的代码片段。

$Contests = ($DataSet.Contest)
$Team     = ($Team.cont)

foreach($Contest in $Contests)
{
    $ContestName = $Contest
    $stats       = $DataSet | where {$_.contest -eq $contest}
    $signups     = $stats.SignUps
    $newbies      = $stats.Newbies
    $uploads     = $stats.Uploads
    $views       = $stats.Views
    $eviews      = $stats.EViews
    $votes       = $stats.Votes
    $date        = $stats.EndDate
    $teamx       = $team | where {$_ -eq $stats.contest}
    $contest
    $teamx
    }

$ contest显示比赛名称,但$ teamx为空白

以下是关于哈希表的更改代码。我试图将对象数组转换为字符串但是徒劳无功。

$team = @{
"Short Film" = "Member4";
"Student Photography" = "Member0";
"Student Art" = "Member1";
"Macro Photography" = "Member2";
"Landscape Photography" = "Member3";
}

$Contests = ($DataSet.Contest)
$Contests = $Contests | where {$_ -ne "" -and $_ -ne $null -and $_ -ne [dbnull]::value}

foreach($Contest in $Contests)
{
    $ContestName = $Contest
    $stats       = $DataSet | where {$_.contest -eq $contest}
    $signups     = $stats.SignUps
    $datatemp    = $stats.Contest
    if ($team.ContainsKey($datatemp)) {write-output "Exists"}
    else {write-output "Doesn't Exist"}
    $datatemp
    $team.count
    }

我尝试在 ContainsKey 中直接提供 $ Contest $ ContestName $ stast.Contest ,但是所有时间输出都是相同的 -

Doesn't Exist
Short Film
5
Doesn't Exist
Student Photography
5
Doesn't Exist
Student Art
5
Doesn't Exist
Macro Photography
5
Doesn't Exist
Landscape Photography
5

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我无法确切地知道$ Dataset中究竟是什么,但是你的症状都指向竞赛价值中的空格,导致你的测试失败。

试试这个,看看你是否得到了不同的结果:

foreach($Contest in $Contests)
{
    $ContestName = $Contest
    $stats       = $DataSet | where {$_.contest -eq $contest}
    $signups     = $stats.SignUps
    $datatemp    = $stats.Contest.trim()
    if ($team.ContainsKey($datatemp)) {write-output "Exists"}
    else {write-output "Doesn't Exist"}
    $datatemp
    $team.count
    }