Hello,
I am Using below script to get archives values for several tags (12-15 Tags). If I am trying for smaller range (approx 2~5 days), I am not getting for all tags listed in my Input File.
$piServerName = "PISRV01" Server Name
$SourceFile = "C:\Working\Input\Input.txt" File Path (Note : Input Tags separated by new line)
$OutputFolder = "C:\Working\Output\ArchiveTimeSeriesData\" folder where result should be stored
$Timestamp = get-date -uFormat "%d_%m_%Y_%H %M"
$NewFileName = "ArchiveTimeSeriesData_" + $Timestamp + ".csv" file name
to PI Server
$piConnection = Connect-PIDataArchive -PIDataArchiveMachineName $piServerName -AuthenticationMethod Windows
points from InputFile i.e. $SourceFile
$Points = Get-Content $SourceFile
file contains more than one point then enumerate all points and get data.
ForEach($piPoint in $Points){
point attributes like Name, Pointtype etc..
$pt = Get-PIPoint -Name $piPoint -Connection $piConnection -AllAttributes
line only for debugging to check point Points = Get.
Write-Host Data Write started for Tag $pt.Point.Name
#$results = Get-PIValue -PIPoint $pt -Time (ConvertFrom-AFRelativeTime -RelativeTime "*") -ArchiveMode Previous | Select TimeStamp, Value
$Starttime = (get-date)
$Endtime= (get-date).AddDays(-1)
#$StartTime = Get-Date("30-Sep-2023 00:00:00")
#$EndTime = Get-Date("05-Oct-2023 00:00:00")
$results = Get-PIValue -PIPoint $pt -StartTime $Starttime -EndTime $Endtime | Select TimeStamp, Value
line only for debugging to check results count i.e number of events fetched within the time range..
Write-Host $piPoint has @($results).Count events in the time range
if result is empty i.e no values then skip the piPoint and next the next piPoint..
if ($results -ne $null){
to file
$myDestinationForData = "$($OutputFolder)$($NewFileName)"
$fs = New-Object System.IO.FileStream $myDestinationForData ,'Append','Write','Read'
$myStreamWriter = New-Object System.IO.StreamWriter($fs)
if the point type is digital. If Yes then get digital set else it will only give state id.
if ($pt.Attributes.pointtype -eq "Digital"){
$digStateSet= Get-PIDigitalStateSet -Name $pt.Attributes.digitalset -Connection $piConnection
the result to file.
ForEach ($result in $results){
$rtimeStamp = $result.TimeStamp
$rvalue = $digStateSet[$result.Value.State]
$myStreamWriter.WriteLine("$($pt.Point.Name),$rTimeStamp,$rvalue")
}
}
if the point type is not digital then write value to file.
else {
ForEach ($result in $results){
$rtimeStamp = $result.TimeStamp
$rvalue = $result.Value
$myStreamWriter.WriteLine("$($pt.Point.Name),$rTimeStamp,$rvalue")
}
}
}
line only for debugging to check point name and completion for single point.
Write-Host Data Write Completed for Tag $pt.Point.Name
$myStreamWriter.Close()
}
Thanks,
Ruchit