Powershell ISE - Create file with 10 minute averages

I have a powershell ise file that i inherited from a former employees work - this creates a 10 minute averages file of multiple input tags.

The only issue is that the csv file is outputted in:

Start Time, End Time, TagName, Value

where it appends Tag1, Tag2, Tag3 etc

 

When it needs to be:

Start Time, Tag1 Value, Tag2 Value, Tag3 Value

 

The current code is below - any ideas on how to change the output bit to get the required format?

 

[CmdletBinding()]

param(

  [Parameter(Mandatory=$true,Position=0)]

  [string]$PIDataArchiveMachineName,

 

  [Parameter(Mandatory=$true,Position=1)]

  [string[]]$PITagMasks

)

 

# Connect to PI Data Archive

$con = Connect-PIDataArchive -PIDataArchiveMachineName $PIDataArchiveMachineName

 

# Get start time as beginning of previous hour & end time as beginning of current hour

[string]$currentHourAsString = Get-Date -Format "dd-MMM-yyyy_HH"

[string]$end_time = Get-Date -Format "yyyy-MMM-dd HH"

$end_time = $end_time+":00:00"

[DateTime]$end_time = $end_time

[DateTime]$start_time = $end_time.AddHours(-1)

 

# Define Output File

$csvRootPath = "E:\CSV Folder\"

$csvPath = $csvRootPath + $currentHourAsString + "_TenMinSummaries.txt"

 

# Get collection of all PI Points to have summary calculations

Foreach ($PITagMask in $PITagMasks)

{

  $pipoints = $pipoints + ( Get-PIPoint -Name $PITagMask -AllAttributes -Connection $con )

}

 

$stringPIPointNames = $pipoints.Point.Name

$uniqueStringPIPointNames = $stringPIPointNames | Select-Object -Unique

 

# set up start time, end time, and number of intervals

$times = @($start_time,$end_time)

$intervals = ($end_time-$start_time).TotalMinutes / 10

 

# iterate through each PI Point returned and calculate summaries

foreach ($uniqueStringPIPointName in $uniqueStringPIPointNames)

{

  $expression = "`'$uniqueStringPIPointName`'"

  $tenMinuteAverages = Get-PIExpressionSummary -Times $times -Intervals $intervals -SummaryType Average -Basis EventWeighted -Expression $expression -Connection $con

  $output = $tenMinuteAverages | Select-Object -Property @{Name='StartTime';Expression={$_.StartTime.ToLocalTime()}},@{Name='EndTime';Expression={$_.EndTime.ToLocalTime()}},@{Name='PI Point';Expression={$uniqueStringPIPointName}},@{Name='Average';Expression={$_.Values.Values.Value}} | Export-Csv -Path $csvPath -Append -NoTypeInformation

}