【发布时间】:2021-07-17 22:30:45
【问题描述】:
我是 PowerShell 新手。
我目前正在尝试合并两个不同的 csv:
- 一个有 7 列 31 行
- 另一个有3列10行左右
- 他们只有共同的 PC 列,其余的我必须添加列
我尝试了几种操作,但都没有成功。 这是 powershell 控制台上第一个 csv 的开头:
PS Z:\Partage> $CSVInitiale | Format-Table
PC Processeur Taille_de_la_RAM Capacite_carte_reseau Taille_du_dique_dur Systeme_d_exploitation
-- ---------- ---------------- --------------------- ------------------- ----------------------
ADSERVER Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB 1 GB 60,00 GB Microsoft Windows Server 2016 Datacenter
PCW10 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB 1 GB 60,00 GB Microsoft Windows 10 ?ducation
PCW7 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 1 GB 1 GB 80,00 GB Microsoft Windows 7 Professionnel
User1_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User2_PCW7 Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB 200 MB 250 GB Microsoft Windows 7 Professionnel
User3_PCW10 Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB 2 GB 1500 GB Windows 10 ?ducation
User4_PCW95 Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB 100 MB 120 GB Windows 95
User5_PCW7 Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB 1 GB 900 GB Windows 7 Professionnel
User6_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User7_PCWVista Intel(R) Core(TM) i5-1900K CPU @ 1.60GHz 1 GB 50 MB 120 GB Windows Vista Professionel
User8_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User9_PCW7 Intel(R) Core(TM) i5-8900K CPU @ 3.60GHz 16 GB 1 GB 900 GB Windows 7 Professionnel
User10_PCW7 Intel(R) Core(TM) i3-5900K CPU @ 2.60GHz 6 GB 500 MB 200 GB Windows 7 Professionnel
User11_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User12_PCW7 Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB 200 MB 250 GB Microsoft Windows 7 Professionnel
User13_PCW10 Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB 2 GB 1500 GB Windows 10 ?ducation
User14_PCW95 Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB 100 MB 120 GB Windows 95
User15_PCW7 Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB 1 GB 900 GB Windows 7 Professionnel
User16_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User17_PCWVista Intel(R) Core(TM) i5-1900K CPU @ 1.60GHz 1 GB 50 MB 120 GB Windows Vista Professionel
User18_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User19_PCW7 Intel(R) Core(TM) i5-8900K CPU @ 3.60GHz 16 GB 1 GB 900 GB Windows 7 Professionnel
User20_PCW7 Intel(R) Core(TM) i3-5900K CPU @ 2.60GHz 6 GB 500 MB 200 GB Windows 7 Professionnel
User21_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User22_PCW7 Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB 200 MB 250 GB Microsoft Windows 7 Professionnel
User23_PCW10 Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB 2 GB 1500 GB Windows 10 ?ducation
User24_PCW95 Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB 100 MB 120 GB Windows 95
User25_PCW7 Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB 1 GB 900 GB Windows 7 Professionnel
User26_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User27_PCWVista Intel(R) Core(TM) i5-1900K CPU @ 1.60GHz 1 GB 50 MB 120 GB Windows Vista Professionel
User28_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation
User29_PCW7 Intel(R) Core(TM) i5-8900K CPU @ 3.60GHz 16 GB 1 GB 900 GB Windows 7 Professionnel
User30_PCW7 Intel(R) Core(TM) i3-5900K CPU @ 2.60GHz 6 GB 500 MB 200 GB Windows 7 Professionnel
以及 powershell 控制台上的第二个 csv:
PS Z:\Partage> $PrestaCSV | Format-Table
PC Nom_du_prestataire Date_de_Migration
-- ------------------ -----------------
PCW esgi-src12\presta1 17_07_21_01:35
PWX esgi-src12\presta1 17_07_21_01:36
User10_PCW7 esgi-src12\presta1 17_07_21_01:50
User6_PCW10 esgi-src12\presta1 17_07_21_01:55
User6_PCW10 esgi-src12\presta1 17_07_21_01:56
User6_PCW10 esgi-src12\presta1 17_07_21_02:01
User11_PCW10 esgi-src12\presta1 17_07_21_02:01
User25_PCW7 esgi-src12\presta2 17_07_21_02:07
我想做的是: 1 - 在第一个 csv 中添加最后两列 2 - 仅更新第一个 csv 中的相关行
像这样:Example array
1st CSV : 2nd csv: Result:
| A | B | C | | A | Y | X | | A | B | C | Y | X |
|---|---|---| |---|---|---| |---|---|---|---|---|
| A1| B1| C1| | A2| Y2| X2|----------\ | A1| B1| C1| | |
| A2| B2| C2| + | A3| Y3| X3|----------/ | A2| B2| C2| Y2| X2|
| A3| B3| C3| | | | | | A3| B3| C3| Y3| X3|
这是我尝试做的脚本:
# TEST 4 => Crée les deux colonnes supplémentaires mais ne met pas les bonnes données
$NomCsvInitiale = "\\ADSERVER\Inventaire\InventairePCAD.csv"
$CSVInitiale = Import-Csv -Path '\\ADSERVER\Inventaire\InventairePCAD.csv' -Delimiter ','
$Folder = "\\ADSERVER\Inventaire\"
$GetAllCsv = (Get-ChildItem -Path $Folder -Filter 'Presta*.csv').FullName
$PrestaCSV = Import-Csv -Path $GetAllCsv -Delimiter ','
foreach ( $PrestaCSV1 in $PrestaCSV) {
if ($CSVInitiale.PC -eq $PrestaCSV1.PC) {
$CSVInitiale | Select-Object *, @{n="Nom_du_prestataire";e={$PrestaCSV1.Nom_du_prestataire}}, @{n="Date_de_Migration";e={$PrestaCSV1.Date_de_Migration}} | Export-Csv -Path $NomCsvInitiale -NoTypeInformation;
} elseif ($CSVInitiale.PC -ne $PrestaCSV1.PC) {
$CSVInitiale | Select-Object *, @{n="Nom_du_prestataire";e={" "}}, @{n="Date_de_Migration";e={" "}} | Export-Csv -Path $NomCsvInitiale -NoTypeInformation;
}
}
它确实添加了两列,但它只将最后一行放在所有 PC 上。
实际结果示例:
PS Z:\Partage> $PrestaCSV
PC Nom_du_prestataire Date_de_Migration
-- ------------------ -----------------
PCW esgi-src12\presta1 17_07_21_01:35
PWX esgi-src12\presta1 17_07_21_01:36
User10_PCW7 esgi-src12\presta1 17_07_21_01:50
User6_PCW10 esgi-src12\presta1 17_07_21_01:55
User6_PCW10 esgi-src12\presta1 17_07_21_01:56
User6_PCW10 esgi-src12\presta1 17_07_21_02:01
User11_PCW10 esgi-src12\presta1 17_07_21_02:01
User25_PCW7 esgi-src12\presta2 17_07_21_02:07
PS Z:\Partage> $CSVInitiale | Format-Table
PC Processeur Taille_de_la_RAM Capacite_carte_reseau Taille_du_dique_dur Systeme_d_exploitation Nom_du_prestataire Date_de_Migration
-- ---------- ---------------- --------------------- ------------------- ---------------------- ------------------ -----------------
ADSERVER Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB 1 GB 60,00 GB Microsoft Windows Server 2016 Datacenter esgi-src12\presta2 17_07_21_02:07
PCW10 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 2 GB 1 GB 60,00 GB Microsoft Windows 10 ?ducation esgi-src12\presta2 17_07_21_02:07
PCW7 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz 1 GB 1 GB 80,00 GB Microsoft Windows 7 Professionnel esgi-src12\presta2 17_07_21_02:07
User1_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation esgi-src12\presta2 17_07_21_02:07
User2_PCW7 Intel(R) Core(TM) i3-4600K CPU @ 1.60GHz 8 GB 200 MB 250 GB Microsoft Windows 7 Professionnel esgi-src12\presta2 17_07_21_02:07
User3_PCW10 Intel(R) Core(TM) i7-8900K CPU @ 3.60GHz 16 GB 2 GB 1500 GB Windows 10 ?ducation esgi-src12\presta2 17_07_21_02:07
User4_PCW95 Intel(R) Core(TM) i3-2300K CPU @ 1.60GHz 2 GB 100 MB 120 GB Windows 95 esgi-src12\presta2 17_07_21_02:07
User5_PCW7 Intel(R) Core(TM) i5-9600K CPU @ 3.60GHz 24 GB 1 GB 900 GB Windows 7 Professionnel esgi-src12\presta2 17_07_21_02:07
User6_PCW10 Intel(R) Core(TM) i5-5900K CPU @ 1.60GHz 12 GB 420 MB 900 GB Windows 10 ?ducation esgi-src12\presta2 17_07_21_02:07
看了论坛,我以为我明白了,不能直接收集,需要重新创建一个csv文件,对吗?这对我不起作用了:(
问候。
【问题讨论】:
-
您尝试做的事情通常被称为Join-object。 Join 一词来自 SQL 和关系数据库的世界。大约十年来,人们一直对向 powershell 添加 Join-Object 感兴趣。您可以在this web page 阅读此内容。
-
请不要发布数据/错误/代码的图像。为什么? lookee ... 为什么在提问时不上传代码/错误的图像? - 元堆栈溢出 — meta.stackoverflow.com/questions/285551/…
-
这里是一个 Join-Object 项目的示例:Github Join-Object。
-
@WalterMitty 感谢您的快速回复,我会试试这个,我会回答结果如何!
-
@Lee_Dailey 我已经重新格式化我的帖子
标签: powershell