【发布时间】:2019-10-31 09:48:36
【问题描述】:
从 CSV 文件中读取数据,然后尝试将新的自定义对象添加到列表中时,我在运行脚本时遇到错误,但是当我尝试调试代码时,我按预期工作,但我不知道找出为什么它在调试模式下可以工作,但在正常运行时却不行。
$global:scanTime = Get-date
$script:logfile_Database = Import-Csv -Path "${logSpace}\${CSV_logfileData}" -Delimiter ";"
$currentLogfile = Get-ChildItem -Path "$($logfile.Path)\$($logfile.FileName)" -ErrorAction Stop
$logfile_Data = [PSCustomObject]@{
scanTime = $scanTime.ToString("yyyy-MM-ddTHH:mm:ss")
size = $currentLogfile.Length
}
$logfile_Database += $logfile_Data
预期结果:带有最后测量值的数组。
非调试模式时出错:
错误:方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为“op_Addition”的方法。
更新
现在可以使用以下代码更新,但我仍然想知道为什么调试模式和正常模式之间的代码执行存在差异。
$global:scanTime = Get-date
[System.Collections.ArrayList]$script:logfile_Database = Import-Csv -Path "${logSpace}\${CSV_logfileData}" -Delimiter ";"
$currentLogfile = Get-ChildItem -Path "$($logfile.Path)\$($logfile.FileName)" -ErrorAction Stop
$logfile_Data = [PSCustomObject]@{
scanTime = $scanTime.ToString("yyyy-MM-ddTHH:mm:ss")
size = $currentLogfile.Length
}
$logfile_Database.add($logfile_Data)
【问题讨论】:
标签: powershell visual-studio-code