我有2个数组,想在Powershell上进行比较。 我为此使用Compare-Object,但是有时比较过程不正确。
如果其中一个数组具有另一个记录不存在的记录,则可以识别差异
■ActualResult
date data
--------- ------
2019/7/11 1
■ExpectedResult
date data
--------- ------
■diff
SideIndicator date data
------------- --------- ------
<= 2019/7/11 1
但是如果与列之一进行比较,则不会触发任何内容
■ActualResult
date data
--------- ------
2019/7/11 1
■ExpectedResult
date data
--------- ------
2019/7/11 2
■diff
SideIndicator date data
------------- --------- ------
这是我用于此结果的代码
foreach($package in $targetPackage)
{
$sheetName = $package.No
$tempExpectedVal = Import-XLSX -Path $testData -Sheet $sheetName
$targetPackage = $project.Packages[$package.PackageName]
foreach($parameter in $targetParameter){
if($parameter.PackageName -eq $targetPackage.Name){
if($parameter.ParamaterType -eq "Package"){
$targetPackage.Parameters[$parameter.ParameterName].Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Literal,$parameter.ParameterVal)
$Project.Alter()
}elseif ($parameter.ParamaterType -eq "Project") {
$project.Parameters[$parameter.ParameterName].Set([Microsoft.SqlServer.Management.IntegrationServices.ParameterInfo+ParameterValueType]::Literal,$parameter.ParameterVal)
$Project.Alter()
}else {
}
}
}
#$startTime = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
$startTime = "2019/07/10 11:00:00"
$targetPackage.Execute("false", $null)
$endTime = "2019/07/10 19:00:00"
#create tag list object
$tempActualVal = New-Object System.Data.DataTable
#create new SQL Server Connection
$sqlConnString = "Data Source=10.118.2.5;Initial Catalog=mieruka_dev_db;uid=sqlroot;pwd=@S9L_adminS1"
$sqlConn = New-Object System.Data.SqlClient.SqlConnection
$sqlConn.ConnectionString = $sqlConnString
$sqlConn.Open()
#prepare SQL command object and SQL Adapter to execute the query
$sqlQuery = New-Object System.Data.SqlClient.SqlCommand
$sqlQuery.Connection = $sqlConn
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
#Create select Query and prepare it on SQL Adapter
$table = $package.Schema+"."+$package.Table
$sqlQuery.CommandText = "SELECT * FROM "+$table+" WHERE created_at BETWEEN @param2 AND @param3 OR updated_at BETWEEN @param2 AND @param3"
$sqlQuery.Parameters.Add("param2",$startTime)
$sqlQuery.Parameters.Add("param3",$endTime)
#prepare insert command to sql adapter
$sqlAdapter.SelectCommand = $sqlQuery
#execute the query
$res = $sqlAdapter.SelectCommand.ExecuteReader()
$tempActualVal.Load($res)
$sqlConn.Close()
$tempActualVal.Columns.Remove("created_by")
$tempActualVal.Columns.Remove("updated_by")
$tempActualVal.Columns.Remove("created_at")
$tempActualVal.Columns.Remove("updated_at")
$actualVal = [System.Collections.ArrayList]@{}
$expectedVal = [System.Collections.ArrayList]@{}
$report = [System.Collections.ArrayList]@{}
$columns = $tempExpectedVal | Get-Member -MemberType NoteProperty | ForEach-Object {"$($_.Name)"}
foreach($tarVal in $tempExpectedVal){
$tempData1 = @{}
foreach($col in $columns){
$tempData1.Add($col, $tarVal.$col)
}
$expectedVal.Add($tempData1)
}
foreach($val in $tempActualVal){
$tempData2 = @{}
foreach($col in $columns){
$tempData2.Add($col, $val[$col])
}
$actualVal.Add($tempData2)
}
$fileName = "diff_"+ $package.packageName + "_" + (Get-Date).ToString("yyyyMMddHHmmss")+".xlsx"
$resDir = $parentfDir + "\testcase\"+$fileName
$diffs = Compare-Object $actualVal $expectedVal -Property $columns -PassThru
if($diffs){
foreach($diff in $diffs){
$rep = New-Object -Type PSCustomObject
if($diff.SideIndicator -eq "=>"){
$rep | Add-Member -type NoteProperty -Name "Type" -Value "Expected_value"
}else{
$rep | Add-Member -type NoteProperty -Name "Type" -Value "Actual_value"
}
foreach($col in $columns){
$rep | Add-Member -type NoteProperty -Name $col -Value $diff.$col
}
$report.Add($rep) | Out-Null
}
$report | Export-XLSX -Path $resDir
}
}
我希望结果是这样的:
■diff
SideIndicator date data
------------- --------- ------
<= 2019/7/11 1
=> 2019/7/11 2
Powershell中是否还有其他功能可以让我得到我期望的结果?
答案 0 :(得分:0)
最后,我不使用任何Compare-Object。 我只是将所有记录循环到我的2个arraylist上,并将每个记录与-eq进行比较。
这不是一种聪明的解决方法,但是比我想的要好。