I am still pretty new to PowerShell in general, and have been scrambling to assemble useful code using the OSIsoft.PowerShell library to perform various PI data management tasks. I was really glad when I got this one worked out and decided to share it with you all. This script uses a variation of the OSIsoft.PowerShell library cmdlet "Get-PIExpressionSummary". Developers may want to note you can substitute "Average" for other summary types such as Total, Maximum, Minimum, StdDev, etc.. This tool is a pretty quick and convenient way to generate a bunch of .csv files for a list of tags.

 

If you are not too familiar with OSIsoft.Powershell I just want to remind you to execute:

  Get-Command -Module OSIsoft.Powershell

to load the OSIsoft.PowerShell library, if you have not already done so, before you try this PowerShell script.

 

The "source_tags_TEST.txt" file is just a single space list of pi tags like this:

SINUSOID

SINUSOIDU

CDT158

 

Here it is:

<# =====  OSISOFT  POWERSHELL  SCRIPT  DESCRIPTION:

     This PowerShell Script Retrieves interpolated values for a list of PI tags at specific intervals.

     The data is written to separate csv files for each tag. As noted the intervals should be a count

     of intervals between the start and end time. For example every minute for one day would be

     60 minutes x 24 hours = 1440.

#>

 

# ==========  C O N F I G U R A T I O N S   P E R   R U N  ==========

$piServerName = "YOUR_SOURCE_PI_DATA_ARCHIVE"

$sourceFile = "C:\Temp\Source\source_tags_TEST.txt"              #~ Directory and file name for the list of tags

$destinationFolder = "C:\Temp\Destination\"                      #~ Directory where the csv files will be written

$dataStartTime = Get-Date("17-Mar-17 13:00:00")                  #~ Specify data extration start time

$dataEndTime = Get-Date("17-Mar-17 15:00:00")                    #~ Specify data extration end time

$intervals = 120                                                 #~ Needs to be a chosen count of intervals for the time range (i.e. 60 minutes x 2 hours = 120)

 

#~ Other configurations for generating values and the files:

$times = @($dataStartTime,$dataEndTime)

$suffix = ".csv"

 

#~ Connect to the target PI Server:

$piConn = Connect-PIDataArchive -PIDataArchiveMachineName $piServerName

 

#~ Get your pi points one by one and write the csv files:

$piPoints = Get-Content $sourceFile

ForEach($piPoint in $piPoints){

    $expression = "'$($piPoint)'"

  

   #~ The next line does the heavy lifting:

    $results = Get-PIExpressionSummary -Times $times -Intervals $intervals -SummaryType Average -Basis TimeWeighted -Expression $expression -Connection $piConn

  

    $myDestinationForData = "$($destinationFolder)$($piPoint)$($suffix)"

    $myStreamWriter = [System.IO.StreamWriter] ($myDestinationForData)

    ForEach ($result in $results){

        $piTime = $result.StartTime.ToString("MM/dd/yyyy HH:mm:ss")

        $comma = ","

        $resultValue = $result.Values["Average"].Value

        $myStreamWriter.WriteLine("$($piTime)$($comma)$($resultValue)")

    }

    $myStreamWriter.Close()

}