I need some help formatting the Input.CSV which contains JSON into Output .CSV which will show the column like:
Date (Local Timezone, not UTC), IP Address, User, Record Type, Activity, Item
See the sample Input.csv in the below:
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""
}",,
The big challenge here is to convert the Date column from UTC into the local timezone.
#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:temppsData.csv
Any help is much appreciated.
2
Answers
You can use the
.ToLocalTime()
method fromdatetime
for this. In PowerShell 7+ConvertFrom-Json
would already convert theCreationTime
key in your Json into adatetime
instance thus the casting of[datetime]
wouldn’t be needed but for compatibility with both versions you should leave it as is.I have reproduced in my environment and got expected results as below :
Output :
Now(Just to check):