Powershell script for Backfill/Recalculate

Hi All,

 

I'm trying to create a PS script (I don't have much knowledge of programming but I'm trying and testing out, so far nothing worked) for backfilling 3 different analysis during a time range that will run everyday using Windows Task Scheduler. The analysis are interdependent, so the first analysis have to be backfill first than the second and then the third. I'm thinking to filter the analysis by the analysis category

 

Have ever anyone done that before? Does anyone has a script that they can share?

 

Thanks!!!!

  • Is there a reason you need to backfill the data everyday instead of having the analyses set up to automatically calculate on the correct period?

  • I would take a look at this old post if the automatic recalculation is not what you are looking for: https://pisquare.osisoft.com/s/question/0D51I00004UHiaWSAT/powershell-script-for-backfillrecalculate

    If your database is well designed and depending on what you are trying to do, you might be able to run it just once using option: DeleteExistingDataCalculateDependents.

     

    I seem to spent a large chunk of my time thinking thinking about recalculations and dependencies issues.

     

  • The version used does not support automatic recalculation and they don't want to upgrade.

    Also, the attribute that can be added more data if someone forget on the time it is a table lookup attribute, which does not recognizes as out of order data, so automatic recalculation would not work as well

  • I did take a look on that,

     

    I come up with something that run but when I put the foreach search elements to filter that part does not work.

     

    Like param(

    [Parameter(Mandatory=$true)]

    [string]$PISystem,

    [Parameter(Mandatory=$true)]

    [string]$Database,

    [string]$Query = ""

    [string]$Start = "*-1d",

    [string]$End = "*",

    [ValidateSet('DeleteExistingData','FillDataGaps')]

    [string]$Option = 'DeleteExistingData'

    )

    # load AFSDK

    [Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK") | out-null

    $systems = new-object OSIsoft.AF.PISystems

    $system = $systems[$PISystem]

    $db = $system.Databases[$Database]

     

    Element

    $SiteOperation_Element = $DB.Elements["SiteName"].Elements["SiteCode"].Elements["SiteOperation"]

    foreach($element in $SiteOperation_Element.Elements)

    {

      Analysis

    function GetAnalysis($Query)

    {

    $analysisSearch = new-object OSIsoft.AF.Search.AFAnalysisSearch $db, "", $Query

    $count = $analysisSearch.GetTotalCount()

    if ($count -gt 0) {

      $timeRange = new-object OSIsoft.AF.Time.AFTimeRange $Start, $End

     $analyses = $analysisSearch.FindAnalyses()

     [void]$system.AnalysisService.QueueCalculation($analyses, $timeRange, $Option)

     Write-Host "Queued $count analyses for recalculation from $($timeRange.StartTime) to $($timeRange.EndTime) using option $option"

    } else {

    Write-Host "No analyses found matching '$Query' in $Database" }

    }

     

    $Query = "Analysis1"

    GetAnalysis $Query

    $Query = "Analysis2"

    GetAnalysis $Query

    $Query = "Analysis3"

    GetAnalysis $Query

     

    If I added the part in bold it does not work as I wanted, If I removed, the script worked fine, but I need to filter on the element path.

     

    Do you know what I am missing?

     

  • I did take a look on that,

     

    I come up with something that run but when I put the foreach search elements to filter that part does not work.

     

    Like param(

    [Parameter(Mandatory=$true)]

    [string]$PISystem,

    [Parameter(Mandatory=$true)]

    [string]$Database,

    [string]$Query = ""

    [string]$Start = "*-1d",

    [string]$End = "*",

    [ValidateSet('DeleteExistingData','FillDataGaps')]

    [string]$Option = 'DeleteExistingData'

    )

    # load AFSDK

    [Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK") | out-null

    $systems = new-object OSIsoft.AF.PISystems

    $system = $systems[$PISystem]

    $db = $system.Databases[$Database]

     

    Element

    $SiteOperation_Element = $DB.Elements["SiteName"].Elements["SiteCode"].Elements["SiteOperation"]

    foreach($element in $SiteOperation_Element.Elements)

    {

      Analysis

    function GetAnalysis($Query)

    {

    $analysisSearch = new-object OSIsoft.AF.Search.AFAnalysisSearch $db, "", $Query

    $count = $analysisSearch.GetTotalCount()

    if ($count -gt 0) {

      $timeRange = new-object OSIsoft.AF.Time.AFTimeRange $Start, $End

     $analyses = $analysisSearch.FindAnalyses()

     [void]$system.AnalysisService.QueueCalculation($analyses, $timeRange, $Option)

     Write-Host "Queued $count analyses for recalculation from $($timeRange.StartTime) to $($timeRange.EndTime) using option $option"

    } else {

    Write-Host "No analyses found matching '$Query' in $Database" }

    }

     

    $Query = "Analysis1"

    GetAnalysis $Query

    $Query = "Analysis2"

    GetAnalysis $Query

    $Query = "Analysis3"

    GetAnalysis $Query

     

    If I added the part in bold it does not work as I wanted, If I removed, the script worked fine, but I need to filter on the element path.

     

    Do you know what I am missing? What should I change for the filter on the elements to work?

     

  • It looks like you want to loop through the $Packing_Element.Elements, but it is not being used. Also, from each element, you probably want to call the Analyses Property described here and remove your AFAnalysis search query.

     

     

  • It was supposed to be $SiteOperation_Element, I corrected that, but still not running

  • Your analysis query does not use the element. You could remove all of that and replace:

     $analyses = $analysisSearch.FindAnalyses()

    with: $analyses = $element.Analyses

     

    Also, you probably don't need to call function GetAnalysis($query)...

  • but then doing like this I may find the analysis in some elements that I don't want to run the backfill no?

  • That's true. You might have to modify your query to include the element then.