简体   繁体   English

PowerShell,如何将两组不相关的数据组合成一个数组或csv

[英]PowerShell, How to combine two sets of unrelated data into one array or csv

I'm trying to write a simple powershell script to pull software and service data off a machine.我正在尝试编写一个简单的 powershell 脚本来从机器上提取软件和服务数据。 So I'm trying to figure out the best way to consolidate the data onto one cvs file instead of two separate csv files.所以我试图找出将数据合并到一个 cvs 文件而不是两个单独的 csv 文件的最佳方法。 I wanted to use the below, but it would save time in the end for both sets of data to be combined.我想使用下面的,但最终可以节省时间来合并两组数据。

Get-Services | Select StartType, Status, Name, DisplayName |
Export-CSV -path $servicescsv

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | 
Export-CSV -path $softwarecsv

So the above is probably quite simple, but I could just create two arrays for software and services.所以上面可能很简单,但我可以为软件和服务创建两个 arrays。 But then what is the best method to combine the two arrays without writing over each other?但是,将两个 arrays 组合在一起而不相互覆盖的最佳方法是什么? I know one of the issues is that DisplayName for software conflicts with DisplayName for services.我知道其中一个问题是软件的 DisplayName 与服务的 DisplayName 冲突。 So right now $array3 = $services + $software only adds the software display name to the array, but the other columns are lost.所以现在$array3 = $services + $software只将软件显示名称添加到数组中,但其他列都丢失了。

What is the best approach to this problem?解决这个问题的最佳方法是什么?

Without an example, of the current CSV files and your desired output, it's not clear what your intended goal is.如果没有示例,当前的 CSV 文件和您想要的 output 中,尚不清楚您的预期目标是什么。

I think you could accomplish what you're trying to do like this:我认为你可以像这样完成你想做的事情:

Get-Service |
    Select-Object StartType, Status, Name, DisplayName, DisplayVersion, Publisher, InstallDate |
    Export-CSV -Path $CombinedCsv

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
    Select-Object StartType, Status, Name, DisplayName, DisplayVersion, Publisher, InstallDate |
    Export-CSV -Path $CombinedCSV -Append

The issue is that Export-Csv uses the first objects in the pipeline to identify the available properties.问题是 Export-Csv 使用管道中的第一个对象来识别可用属性。 If you need to include properties that don't exist at the start, you should specify all of them.如果您需要包含一开始不存在的属性,则应指定所有这些属性。 That will cause Select-Object to create new properties that are blank, which will then export appropriately to CSV.这将导致Select-Object创建空白的新属性,然后将其正确导出到 CSV。

If, on the other hand, your goal is to try to join $servicescsv and $softwarecsv like two SQL tables with a common field... well, you can do that but it's significantly more complicated and requires key fields to join on.另一方面,如果您的目标是尝试加入$servicescsv$softwarecsv ,就像两个具有公共字段的 SQL 表一样......好吧,您可以这样做,但它要复杂得多,并且需要关键字段才能加入。 I don't think the DisplayName property of the registry key you list and the services module are very likely to agree on a name.我认为您列出的注册表项的 DisplayName 属性和服务模块很可能不会就名称达成一致。 Especially because lots of entries in the registry don't have a DisplayName.特别是因为注册表中的许多条目没有 DisplayName。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM