我是 PowerShell 新手。
我目前正在尝试合并两个不同的 csv:
- 一个有 7 列和 31 行
- 另一个有3列,大约10行
- 他们只有 PC 列是共同的,其余的我必须添加列
我尝试过几种操作,但没有成功。
这是 powershell 控制台上第一个 csv 的开头:
1st CSV https://i.stack.imgur.com/nPvq9.png
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 :
2nd Csv https://i.stack.imgur.com/Qmfvx.png
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 中的相关行
像那样 :示例数组 https://i.stack.imgur.com/oBe4x.png
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文件,对吗?这对我来说不再适用了:(
Regards.