Reputation: 5
I'm working a Powershell script that creates and CSV from data pulled from and XML document. I created arrays for each data type, then create the CSV using those arrays.
The XML has a timestamp in UNIX time. I created an array with converted UNIX time to a more readable format but that array is not printing out to the CVS.
$StartTimeComp = [xml]$RMAudit.RMAudit.RMRecord.StartTime
foreach ($time in $StartTimeComp){
$StartTime += [System.TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($time))
}
$outArray = @()
foreach ( $newtime in $StartTime )
{
$outArray += New-Object PsObject -Property @{
'Start Time' = $NewTime}
}
$outArray | export-csv 'C:\location\Results.csv'
Upvotes: 0
Views: 137
Reputation: 36342
Strictly going off the code that you have above there is one obvious issue that could be solved two ways. $StartTime
is not an array. You can either create is as an empty array as you did with $outArray
, or you can move it so it reads like this:
$Starttime = foreach ($time in $StartTimeComp){
[System.TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($time))
}
That will cause the [System.TimeZone]
line to output a [DateTime]
object for each loop of the ForEach
loop, and $StartTime
will collect them all as an array. Then you have an array of things to loop through to create your array of custom objects... but you're really just wasting cycles. Instead try this:
[xml]$RMAudit.RMAudit.RMRecord.StartTime|Select @{label = 'Start Time';expression = {[System.TimeZone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($_))}}|Export-CSV 'C:\Location\Results.csv'
That should do all the work of your above code in one line.
Upvotes: 3