You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OSIsoft PISDK 4.0传递正确时间格式的PowerShell问题

解决PI AF SDK中RecordedValues方法的参数类型错误

问题概述

编写PowerShell脚本统计CSV文件中所有PI标签在指定时间段(2024-01-01至2024-01-15)的总事件数时,遇到以下类型转换错误:

Cannot convert argument "timeRange", with value: "2024-08-13", for "RecordedValues" to type "OSIsoft.AF.Time.AFTimeRange": "Cannot convert the 
"2024-08-13" value of type "System.String" to type "OSIsoft.AF.Time.AFTimeRange"."
At C:\pipointeventextract\script\pipoint-event-extract.ps1:25 char:5
+     $eventCount = $piPoint.RecordedValues($startTime, $endTime, [OSIs ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

错误原因

PIPoint.RecordedValues方法要求传入**OSIsoft.AF.Time.AFTimeRange类型**的时间范围参数,而非直接传入字符串格式的日期。直接传递字符串会触发类型转换失败异常。

修正后的脚本

cls

# Load the necessary PI AF SDK assemblies
$AFSDKAssembly = [Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK")

# Define the PI Server and time range (字符串格式日期)
$piServerName = "HYDRAN-PISRV001"
$startTimeStr = "2024-01-01"
$endTimeStr = "2024-01-15"

# 将字符串日期转换为AFTime对象,再创建AFTimeRange
$afStartTime = New-Object OSIsoft.AF.Time.AFTime($startTimeStr)
$afEndTime = New-Object OSIsoft.AF.Time.AFTime($endTimeStr)
$timeRange = New-Object OSIsoft.AF.Time.AFTimeRange($afStartTime, $afEndTime)

# Read the PI Tags from the CSV file
$piTags = Import-Csv -Path "C:\pipointeventextract\data\pipoints-events-extract.csv" | Select-Object -ExpandProperty PointName

# Create a list to store the results
$results = @()

# Connect to the PI Server
$piServers = New-Object OSIsoft.AF.PI.PIServers
$piServer = $piServers[$piServerName]
$piServer.Connect()

# Loop through each PI Tag and get the total number of events
foreach ($tag in $piTags) {
    $piPoint = [OSIsoft.AF.PI.PIPoint]::FindPIPoint($piServer, $tag)
    # 使用AFTimeRange对象作为参数调用RecordedValues
    $eventCount = $piPoint.RecordedValues($timeRange, [OSIsoft.AF.Data.AFBoundaryType]::Inside, $null, $false).Count

    # Add the result to the list
    $results += [PSCustomObject]@{
        "PI Server Name" = $piServerName
        "Tag" = $tag
        "Start Time" = $startTimeStr
        "End Time" = $endTimeStr
        "Total Events" = $eventCount
    }
}

# Define the output file path with the current date and time
$outputFilePath = "C:\pipointeventextract\report\pipointeventreport-hvn" + (Get-Date).ToString("yyyyMMdd_HHmmss") + ".csv"

# Export the results to a CSV file
$results | Export-Csv -Path $outputFilePath -NoTypeInformation

Write-Output "Report generated: $outputFilePath"

关键修改说明

  1. 将原字符串格式的日期变量重命名为$startTimeStr和$endTimeStr,明确其类型
  2. 通过New-Object创建AFTime对象解析字符串日期,再基于这两个对象构建AFTimeRange时间范围对象
  3. 在调用RecordedValues时,传入AFTimeRange类型的$timeRange参数,匹配方法的参数要求

内容的提问来源于stack exchange,提问作者NanoNet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:22:39