sraposo

Log and disable all analyses in error

Blog Post created by sraposo on Jul 10, 2020

Analyses running that are in error add an unnecessary load on the PI Analysis Service. If you're not convinced, see my other blog post: Asset Analytics Best Practices - Part 4: Analyses in Warning or Error 

 

Often time when we suggest to customers to disable these analyses they have this concern: "How can I keep track of which analyses were disabled prior to disabling all of the ones in error?"  If they have a lot of AF Databases, it may also be inconvenient to use PI System Explorer's management tab to disable all analyses in error in each AF Database. This blog post is to help customers address this concern and possible pain point. The approach suggested in this post will only work with Asset Analytics and AF 2018 SP2 or later. The reason is that it leverages the programmatic access to analysis runtime information introduced in this version. 

 

The below PowerShell script will find all analyses in Error and log their ID and Path to a text file. It will then disable all of the analyses found. The script will work as is, but I offer no guarantee. It's up to the reader to understand it, test it and improve it. Some improvements could be: 

 

  • Find all analyses in Error and in Warning.
  • Confirm the analyses were properly disabled. 
  • Log the error message in the text file along with the ID and path. 
  • Etc...

 

This script is provided as an idea, not a solution. It is in no way supported by OSIsoft, and therefore please don't reach out to our Tech Support team with questions on this script. Questions in the comment space below this blog post are of course welcome  ! 

 

It's easier to read if you copy\paste the script in the PowerShell to ISE. 

 

<# The below example is offered as is, for more
information refer to: Apache License, Version 2.0
Questions in the comment section of the PI Square blog post
associated to this script are welcomed!#>

try
{
[Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK") | Out-Null #load AF SDK / this approach is outdated should probably use Add-Type cmdlet
}
catch
{
Write-Error "Unable to load AF SDK. Exception returned is:"
Exit
}

$afServerName = "AFServerName" #AFServer hostname, FQDN or IP here
$outputFilePath = "C:\PowerShellExamples\AnalysesInError.txt" #output log file. Make sure the folder path exists.

[OSIsoft.AF.PISystems] $afServers = new-object OSIsoft.AF.PISystems

try
{
[OSIsoft.AF.PISystem] $afServer = $AFServers[$AFServerName]
}
catch
{
Write-Error "Unable to find $afServerName. Exception returned is:"
Exit
}

$query = "status:'Error' " #Can modify query and fields here
$fields = "ID path"

try
{
$runTimeStatistics = $afServer.AnalysisService.QueryRuntimeInformation($query,$fields) #Get runtime information
}
catch
{
Write-Error "Can't retrieve analyses runtime statistics. Exception is:"
Exit
}

$guidList = New-Object System.Collections.Generic.List[guid] #Need GUID[] for static method AFAnalysis.FindAnalyses(). Will add to list then cast to array.

foreach ($runTimeStatistic in $runTimeStatistics)
{
$guidList.Add([System.Guid]::Parse($runTimeStatistic[0])) #Need to cast object to GUID and then add it to our list
try
{
$runtimeStatistic[0].ToString() + "," + $runTimeStatistic[1] | out-file -FilePath $outputFilePath -Append #Write ID and Path to file
}
catch
{
Write-Error "Failed writing to output file. Exception is:"
Exit
}

}

try
{
$Analyses = [OSIsoft.AF.Analysis.AFAnalysis]::FindAnalyses($afServer,$guidList.ToArray()) #Find the analyses using static method
}
catch
{
Write-Error "Unable to find analyses. Exception is:"
Exit
}

try
{
[OSIsoft.AF.Analysis.AFAnalysis]::SetStatus($Analyses, [OSIsoft.AF.Analysis.AFStatus]::Disabled) #use static bulk method for performance
}
catch
{
Write-Error "Unable to disable analyses in bulk"
}

write-host "Succesfully logged and stopped all analyses in error"

Outcomes