【发布时间】:2019-05-31 04:18:38
【问题描述】:
我没有使用 PowerShell 的经验,我被要求创建这个脚本以帮助我的一个朋友。该脚本应该读取一个csv文件(这些文件除了时间和主机之外有不同的列,这在所有文件中都是通用的),并将其内容输出到以下格式的JSON文件中:
CSV 文件包含列:
主机|留言 |时间 |严重性 |来源 |
{
"time": 1437522387,
"host": "dataserver992.example.com",
"event": {
"message": "Something happened",
"severity": "INFO",
"source": "testapp"
#...All columns except for time and host should be under "event"
}
}
*唯一保证的列是时间和主机。所有其他列标题因文件而异。
这是我目前所拥有的一部分:
$csvFile = Import-Csv $filePath
function jsonConverter($file)
{
#Currently not in use
$eventString = $file| select * -ExcludeProperty time, host
$file | Foreach-Object {
Write-Host '{'
Write-Host '"host":"'$_.host'",'
Write-Host '"time":"'$_.time'",'
Write-Host '"event":{'
#TODO: Put all other columns (key, values) under event - Except for
time and host
Write-Host '}'
}
}
jsonConverter($csvFile)
关于如何仅提取剩余列、逐行、将其内容输出为键、值 JSON 格式(如上面的示例)的任何想法? 谢谢!
【问题讨论】:
标签: json powershell csv