从CSV导入哈希表,然后导出回CSV
我有一个CSV,它使用运行日志来更新许多自动化任务的状态。我需要一个快速的方法从CSV中查找信息,以便我可以修改它并将其导出回CSV 对于本例,假设我正在跟踪正在运行的进程,并且希望使用“ID”属性作为哈希表的键从CSV导入哈希表,然后导出回CSV,csv,powershell,import,export,hashtable,Csv,Powershell,Import,Export,Hashtable,我有一个CSV,它使用运行日志来更新许多自动化任务的状态。我需要一个快速的方法从CSV中查找信息,以便我可以修改它并将其导出回CSV 对于本例,假设我正在跟踪正在运行的进程,并且希望使用“ID”属性作为哈希表的键 $pathCsv = 'C:\test\test.csv' Get-Process | Export-Csv $pathCsv 我可以很好地从CSV导入,但我不知道如何将其转换为哈希表或将其从哈希表转换为CSV。 要将CSV导入转换为哈希表,请使用组对象 要将哈希表导出回CSV,必须
$pathCsv = 'C:\test\test.csv'
Get-Process | Export-Csv $pathCsv
我可以很好地从CSV导入,但我不知道如何将其转换为哈希表或将其从哈希表转换为CSV。- 要将CSV导入转换为哈希表,请使用组对象
- 要将哈希表导出回CSV,必须使用.GetEnumerator()方法将其分解回对象,以便与导出CSV一起使用
$pathCsv = ($env:TEMP + '\test.csv')
Get-Process | Export-Csv $pathCsv
#import CSV to array as hash table using the ID property as the key
$toHashTable = Import-Csv $pathCsv | Group-Object -AsHashTable -Property ID
#make changes to hashtable here
#break the hashtable into the pipeline, then grab each $_.Value property
#and break that out of the hashtable into the pipeline
$toHashTable.GetEnumerator() | ForEach{($_.Value).GetEnumerator()} | Export-Csv $pathCsv
我在这里问了这个问题,这样我就可以为任何正在寻求解决同一问题的方法的人回答这个问题。- 要将CSV导入转换为哈希表,请使用组对象
- 要将哈希表导出回CSV,必须使用.GetEnumerator()方法将其分解回对象,以便与导出CSV一起使用
$pathCsv = ($env:TEMP + '\test.csv')
Get-Process | Export-Csv $pathCsv
#import CSV to array as hash table using the ID property as the key
$toHashTable = Import-Csv $pathCsv | Group-Object -AsHashTable -Property ID
#make changes to hashtable here
#break the hashtable into the pipeline, then grab each $_.Value property
#and break that out of the hashtable into the pipeline
$toHashTable.GetEnumerator() | ForEach{($_.Value).GetEnumerator()} | Export-Csv $pathCsv
我在这里问了这个问题,这样我就可以为任何正在寻求解决同一问题的方法的人回答这个问题