0

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'
0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, creating the empty array helped me out. I put in parts of my code that directly related to my issue. I essentially the XML as a CVS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.