pt030

PIPoint get RecordedValues

Discussion created by pt030 on Dec 6, 2013
Latest reply on Dec 17, 2013 by pt030

Hi Guys

 

I just started looking at Powershell and I noticed something weird and wonder what I'm doing wrong or whether someone knows about this.   I have this very simple script to get recorded values from a start and end time and I want a count of how many events in that period. 

 

 

 
function PI-RecordedValues 
{param ($PIServerName, $MyPointName, $StartTime, $EndTime) 
  
# Add a reference to the PI SDK assembly. 
[reflection.assembly]::loadwithpartialname("OSIsoft.PISDK") | Out-Null 
[reflection.assembly]::loadwithpartialname("OSIsoft.PISDKCommon") | Out-Null 
  
# Create a new PI SDK object. 
$_PISDK = New-Object PISDK.PISDKClass 
$_Asynch = New-Object PISDKCommon.PIAsynchStatusClass 
  
# Open a connection with the specified PI Server. 
$_PIServer = $_PISDK.Servers.Item($PIServerName) 
  
# Reference the specified Point/Tag. 
$_MyPoint = $_PIServer.PIPoints.Item($MyPointName) 

# Invoke the RecordedValues method with the specified arguments. 
$_PIValues = $_MyPoint.Data.RecordedValues($StartTime, $EndTime, 0, $null, $null, $_Asynch)

[System.Threading.Thread]::Sleep(1000)

# Check how many archive Values in that past 5m for a 1s periodic event. 
$currentDateTime = Get-Date

if ($_PIValues.Count.ToString() -eq 300) 
{
    Write-Host "OK: No errors found - received 300 events in past 5m."
}
elseif ($_PIValues.Count.ToString() -eq 0) 
{
    Write-Host "CRITICAL: No events found in past 5m."
}
elseif ( ($_PIValues.Count.ToString() -gt 0) -and ($_PIValues.Count.ToString() -lt 300) )
{
    Write-Host "WARNING: Missing events - only", $_PIValues.Count.ToString(), "/300 events in past 5m."
}
else
{
    Write-Host "UNKNOWN: Received", $_PIValues.Count.ToString(), " values. Should be between 0 and 300."
}
Write-Host " Checked at", $currentDateTime
}

 The line that I don't like in that code is

 

 

 
[System.Threading.Thread]::Sleep(1000)

 If I don't have that line for a wait, I'm not getting PI Values returned, the count is zero.  Or if I don't have that line and do a Write-Host of something before evaluation of the count, it seems to work also.   Is it because RecordedValues is running in another thread (async) and thus the script just continues without waiting for returned values?   I also can't put $null in the async parameter, it fails.

 

Cheers

 

Peter

Outcomes