Search code examples
powershellforeachtimestampkml

Powershell KML Generation Manipulate Timestamp in Loop


I have a block of powershell code that looks like this:

$(
foreach ($csv in $csvfiles) {
 $fname = (Get-Item $csv).Basename
 '<Folder>
  <name>{0}</name>' -f $fname
    $(
    Import-Csv $csv | 
    foreach {
    '<Placemark>
     <name>{0} - {1}</name>
     <TimeStamp>
      <begin>{1}</begin>
      <end>{1}</end>
     </TimeStamp>
     <description>Information - {4}</description>
     <styleUrl>#{5}</styleUrl>
     <Point>
      <coordinates>{2},{3}</coordinates>
     </Point>
    </Placemark>

      ' -f $_.Variable, $_.Timestamp, $_.Variable2, $_.Variable3, $_.Variable4, $_.Color
     }
     )
'</Folder>'
   }
)

The above code works. I am trying to make my placemarkers last a longer period so they do not disappear if the slider moves off of the seconds field. Timestamp is of the format:

2017-07-26T19:03:40Z

Sometimes the timestamp will shift to the 41 or 39 second, and when moving the slider in Google Earth, you will lose the placemark because it is moving in 1 minute increments...which is what I want. I want to set a begin and end timestamp for the placemark by subtracting and adding 20 seconds to the actual timestamp. How can I do this using the code below? Is there an easy way for me to just subtract and add seconds to the timestamp and still use it as an input to the KML file in that loop?


Solution

  • Perhaps you can try the following to just strip the seconds:

    -f $_.Variable, $($_.Timestamp -replace '^(.*)(\d\d)(.*)$','${1}00$3'), $_.Variable2, $_.Variable3, $_.Variable4, $_.Color
    

    $() : allow to compute the new value first.

    -replace regex, newvalue : reple the seconds by 00.

    I use ${1} for the first capture to avoid the $100 wich means nothing, the second capture is replaced by 00 the third capture is kept as is.