使用 PowerShell 将 JSON 文件转换为 .CSV 文件?

问题描述 投票:0回答:2

我需要一些帮助将包含 JSON 的 Input.CSV 格式化为 Output.CSV,它将显示如下列:

Date (Local Timezone, not UTC), IP Address, User, Record Type, Activity, Item

请参阅下面的示例 Input.csv:

RecordId,CreationDate,RecordType,Operation,UserId,AuditData,AssociatedAdminUnits,AssociatedAdminUnitsNames
GUID,1/24/2023 12:39:18 AM,20,ViewReport,[email protected],"{
    ""Id"": ""GUID"",
    ""RecordType"": 20,
    ""CreationTime"": ""2023-01-24T00:39:18"",
    ""Operation"": ""ViewReport"",
    ""OrganizationId"": ""GUID"",
    ""UserType"": 0,
    ""UserKey"": ""123"",
    ""Workload"": ""PowerBI"",
    ""UserId"": ""[email protected]"",
    ""ClientIP"": ""123.11.22.33"",
    ""Activity"": ""ViewReport"",
    ""ItemName"": ""Important Report"",
    ""WorkSpaceName"": ""banking Confidential Data"",
    ""DatasetName"": ""Corporate Finance"",
    ""ReportName"": ""Corporate Finance"",
    ""CapacityId"": ""GUID"",
    ""CapacityName"": ""Shared On Premium - Reserved"",
    ""WorkspaceId"": ""GUID"",
    ""ObjectId"": ""Corporate Finance"",
    ""DatasetId"": ""GUID"",
    ""ReportId"": ""GUID"",
    ""ArtifactId"": ""GUID"",
    ""ArtifactName"": ""Corporate Finance"",
    ""IsSuccess"": true,
    ""ReportType"": ""PowerBIReport"",
    ""RequestId"": ""GUID"",
    ""ActivityId"": ""GUID"",
    ""DistributionMethod"": ""Shared"",
    ""ConsumptionMethod"": ""Power BI Web"",
    ""ArtifactKind"": ""Report""
}",,

这里最大的挑战是将 Date 列从 UTC 转换为本地时区。

#some json data...
$jsonData = Import-CSV -path C:\Input.CSV


#convert JSON to PowerShell
$psData = $jsonData | ConvertFrom-Json

# then exporting...
$psData | Export-Csv -Path c:\temp\psData.csv

非常感谢任何帮助。

json powershell azure-powershell
2个回答
1
投票

要将 UTC 日期转换为本地时间,可以使用

ToTimeZone
类的
DateTimeOffset
方法。您应该能够使用它来创建您需要的东西。我没有测试这个,但这应该有效:

# Import the CSV file
$jsonData = Import-CSV -path C:\Input.CSV

# Convert the JSON data to PowerShell objects
$psData = $jsonData | ConvertFrom-Json

# Define the local time zone
$localTimeZone = [System.TimeZoneInfo]::Local

# Loop through the PowerShell objects and convert the UTC date to local time
foreach ($psObject in $psData) {
    $utcDate = [DateTimeOffset]::Parse($psObject.AuditData.CreationTime)
    $localDate = $utcDate.ToTimeZone($localTimeZone)
    $psObject.AuditData.CreationTime = $localDate.ToString("yyyy-MM-ddTHH:mm:ss")
}

# Export the updated data to a new CSV file
$psData | Export-Csv -Path C:\Output.CSV -NoTypeInformation

0
投票

为此,您可以使用.ToLocalTime()

中的
datetime
方法。在 PowerShell 7+ 中,
ConvertFrom-Json
已经将 Json 中的
CreationTime
键转换为
datetime
实例,因此不需要转换
[datetime]
但为了与两个版本兼容,您应该保持原样。

Import-Csv C:\Input.CSV | ForEach-Object AuditData |
    ConvertFrom-Json | ForEach-Object {
        [pscustomobject]@{
            Date       = ([datetime] $_.CreationTime).ToLocalTime().ToString('yyyy-MM-ddTHH:mm:ss')
            IpAddress  = $_.ClientIp
            User       = $_.UserId
            RecordType = $_.RecordType
            Activity   = $_.Activity
            Item       = $_.ItemName
        }
    } | Export-Csv c:\temp\psData.csv -NoTypeInformation
© www.soinside.com 2019 - 2024. All rights reserved.