挑战:编写了 powershell 代码,该代码将计算 2024 年 1 月 1 日至 2024 年 1 月 15 日之间在某些 .csv 文件中找到的每个标签的事件总数。代码错误如下:
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
无法明确要传递什么。 期望的结果:能够传递开始日期、结束日期以及脚本来汇总另一个 .csv 文件中列出的 Pitag 集的所有事件。
代码
cls
# Load the necessary PI AF SDK assemblies
$AFSDKAssembly = [Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK")
# Define the PI Server and time range
$piServerName = "HYDRAN-PISRV001"
$startTime = "2024-08-13"
$endTime = "2024-08-14"
# 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)
$eventCount = $piPoint.RecordedValues($startTime, $endTime, [OSIsoft.AF.Data.AFBoundaryType]::Inside, $null, $false).Count
# Add the result to the list
$results += [PSCustomObject]@{
"PI Server Name" = $piServerName
"Tag" = $tag
"Start Time" = $startTime
"End Time" = $endTime
"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"
您应该首先使用 startTime 和 endTime 创建时间范围:
$timeRange = New-Object OSIsoft.AF.Time.AFTimeRange($startTime, $endTime)
然后将其传递给 RecordedValues:
$piPoint.RecordedValues($timeRange, [OSIsoft.AF.Data.AFBoundaryType]::Inside, $null, $false)