将结果从Compare-Object添加到数组 [英] Adding Results from Compare-Object to Array

查看:187
本文介绍了将结果从Compare-Object添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在比较两个文件,一个是CSV文件,一个是txt文件. CSV文件包含大约5,000台服务器的信息,而txt文件仅包含大约3,000台服务器的服务器名称. CSV内的列是名称,操作系统和类型.这是我比较对象的步骤:

I have two files being compared, one a CSV and one a txt file. The CSV file has the information for around 5,000 servers and the txt file has only server names for around 3,000. The columns within the CSV are Name, OS, and Type. This is what I did to compare the objects:

$compareObject = Compare-Object -ReferenceObject $txtFile -DifferenceObject $csvFile.Name -IncludeEqual

在此之后,我得到了三个选择.在两个列表==上的一个,仅在txt文件=>上的一个,以及仅在csv文件=<上的一个.

After this, I was then given three options. Ones that are on both lists ==, ones that are only on the txt file =>, and ones that are only on the csv file =<.

我想做的是将.SideIndicator取为等于==的值,并将其作为$csvFile内的一列,这样我最终可以执行If ($csvFile.SideIndicator -eq "==")...

What I'm trying to do is take the .SideIndicator for values that equal ==, and put that as a column within the $csvFile so I can eventually do If ($csvFile.SideIndicator -eq "==")...

所以基本上我想弄清楚怎么写:

So basically I'm trying to figure out how to write:

If (($csvFile.Name -like $compareObject.InputObject) -and ($compareObject.InputObject -eq "==") {
    (add .SideIndicator to CSV file)
}

我尝试将变量$count++放置在脚本中当前有add .SideIndicator...的位置,以查看返回多少结果,并且始终返回0.

I've tried placing a variable $count++ where I currently have add .SideIndicator... within my script to see how many results return, and it always returns 0.

有人可以帮我解决这个问题或给我一些想法吗?

Can someone help me out with this or give me some ideas?

推荐答案

您希望通过管道传送Compare-Object的结果来终止对SideIndicator的过滤,如下所示:

You want to pipe the results of Compare-Object to endable the filtering of SideIndicator like so:

$fields = "name", "street", "zip", "city"
Compare-Object -ReferenceObject $txtFile -DifferenceObject $csvFile.Name -IncludeEqual -Property $fields -PassThru | Where-Object {
    $_.SideIndicator -eq "=="
} | Select-Object $fields | Export-Csv ".\output.csv" -NoTypeInformation

$fields包含您的CSV标头的地方

Where $fields contain your CSV-Headers

这篇关于将结果从Compare-Object添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆