在 powershell 中跨两个数据集使用 -eq
Using -eq across two datasets in powershell
我正处于紧要关头。我在 powershell 中有两个数据集。数据集 1($table) 通过 sql 查询接收 (从 12 到 17 行不等,有 8 列) 和数据集 2($team) 在脚本中硬编码 (有 18 行和 2 列)。这两个都有一个共同的专栏,Contest。现在我必须开始工作的脚本是 - 对于 $table.contest 中的每个 Contest,从 [=34= 获取其他相应参数]$table并匹配$team.contest中的Contest得到对应的$team.column2 发挥价值。
我能够从每个 table 中单独获取数据,但是当我在 $table.contest & [ 中使用“-eq”条件时=34=]$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 为空白
以下是关于 hash tables 的更改代码。我试图将对象数组转换为字符串,但没有成功。
$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
}
我试过直接喂$Contest、$ContestName和$stast.Contest 在 ContainsKey 中,但始终输出相同 -
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
我做错了什么?
如果不确切知道 $Dataset 中的内容,我无法确定,但您的症状都指向导致测试失败的 Contest 值中的尾随空格。
试试这个,看看是否会得到不同的结果:
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
}
我正处于紧要关头。我在 powershell 中有两个数据集。数据集 1($table) 通过 sql 查询接收 (从 12 到 17 行不等,有 8 列) 和数据集 2($team) 在脚本中硬编码 (有 18 行和 2 列)。这两个都有一个共同的专栏,Contest。现在我必须开始工作的脚本是 - 对于 $table.contest 中的每个 Contest,从 [=34= 获取其他相应参数]$table并匹配$team.contest中的Contest得到对应的$team.column2 发挥价值。
我能够从每个 table 中单独获取数据,但是当我在 $table.contest & [ 中使用“-eq”条件时=34=]$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 为空白
以下是关于 hash tables 的更改代码。我试图将对象数组转换为字符串,但没有成功。
$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
}
我试过直接喂$Contest、$ContestName和$stast.Contest 在 ContainsKey 中,但始终输出相同 -
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
我做错了什么?
如果不确切知道 $Dataset 中的内容,我无法确定,但您的症状都指向导致测试失败的 Contest 值中的尾随空格。
试试这个,看看是否会得到不同的结果:
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
}