将 PSCustomObject 与对象进行比较
Compare PSCustomObject to Object
我创建了一个PsCustomObject
,当变量被ISE调用时,它读取了一个table的相关数据。但是,如果我尝试将 PsCustomObject
与另一个对象进行比较,则 PsCustomObject
无法正确读取。如果现有 CSV 中的任何行与 PSCustomObject
匹配,我想告诉脚本不要将数据导出到 CSV,换句话说,跳过 CSV 文件中的重复行。 CSV 可能有也可能没有多行。
$fileInfo = @(
[pscustomobject]@{
user_id = $user
studio = $studio
function = $Task
end_time_local = $creationTime
asin = $ASIN
variant = $variant
process_class_id = $processClass
}
)
$currentData = Import-Csv "$scansFolder$fileName.csv"
if($fileInfo -ne $currentData){
$fileInfo | Export-Csv "$scansFolder$fileName.csv" -Append -NoTypeInformation -Force
}
[pscustomobject]
是 .NET reference 类型,因此比较两个实例 [1] 与 -eq
将测试 reference equality (identity),即如果两个实例是 one 并且是同一个对象[2] - 在您的场景中显然不是这种情况。
假设您的自定义对象的属性是值类型或字符串的实例(似乎是这种情况),您可以使用 Compare-Object
通过对象的 属性 值 来比较对象,并能够比较 两个对象集合:
$fileInfo = @(
[pscustomobject]@{
user_id = $user
studio = $studio
function = $Task
end_time_local = $creationTime
asin = $ASIN
variant = $variant
process_class_id = $processClass
}
)
# Get the property names.
# This assumes that the CSV data has (at least) the same
# set of properties (columns).
$propNames = $fileInfo[0].psobject.properties.Name
$currentData = Import-Csv "$scansFolder$fileName.csv"
# Compare the $fileInfo custom object(s) to the custom objects read
# from the CSV file and only export those that are unique to the RHS ('=>')
# back to the file, i.e., those that don't match $fileInfo.
Compare-Object -Property $propNames $fileInfo $currentData |
Where-Object SideIndicator -eq '=>' | Select-Object InputObject |
Export-Csv "$scansFolder$fileName.csv" -Append -NoTypeInformation -Force
[1] Import-Csv
也输出 [pscustomobject]
个实例。
[2] 请参阅 Equality Comparison 帮助主题(为 C# 编写,但类似于 PowerShell 的 -eq
运算符)。
我创建了一个PsCustomObject
,当变量被ISE调用时,它读取了一个table的相关数据。但是,如果我尝试将 PsCustomObject
与另一个对象进行比较,则 PsCustomObject
无法正确读取。如果现有 CSV 中的任何行与 PSCustomObject
匹配,我想告诉脚本不要将数据导出到 CSV,换句话说,跳过 CSV 文件中的重复行。 CSV 可能有也可能没有多行。
$fileInfo = @(
[pscustomobject]@{
user_id = $user
studio = $studio
function = $Task
end_time_local = $creationTime
asin = $ASIN
variant = $variant
process_class_id = $processClass
}
)
$currentData = Import-Csv "$scansFolder$fileName.csv"
if($fileInfo -ne $currentData){
$fileInfo | Export-Csv "$scansFolder$fileName.csv" -Append -NoTypeInformation -Force
}
[pscustomobject]
是 .NET reference 类型,因此比较两个实例 [1] 与 -eq
将测试 reference equality (identity),即如果两个实例是 one 并且是同一个对象[2] - 在您的场景中显然不是这种情况。
假设您的自定义对象的属性是值类型或字符串的实例(似乎是这种情况),您可以使用 Compare-Object
通过对象的 属性 值 来比较对象,并能够比较 两个对象集合:
$fileInfo = @(
[pscustomobject]@{
user_id = $user
studio = $studio
function = $Task
end_time_local = $creationTime
asin = $ASIN
variant = $variant
process_class_id = $processClass
}
)
# Get the property names.
# This assumes that the CSV data has (at least) the same
# set of properties (columns).
$propNames = $fileInfo[0].psobject.properties.Name
$currentData = Import-Csv "$scansFolder$fileName.csv"
# Compare the $fileInfo custom object(s) to the custom objects read
# from the CSV file and only export those that are unique to the RHS ('=>')
# back to the file, i.e., those that don't match $fileInfo.
Compare-Object -Property $propNames $fileInfo $currentData |
Where-Object SideIndicator -eq '=>' | Select-Object InputObject |
Export-Csv "$scansFolder$fileName.csv" -Append -NoTypeInformation -Force
[1] Import-Csv
也输出 [pscustomobject]
个实例。
[2] 请参阅 Equality Comparison 帮助主题(为 C# 编写,但类似于 PowerShell 的 -eq
运算符)。