Adding PSCustomObject to Array gives Error, but works fine when debugging the code in Visual Studio Code

自作多情 提交于 2021-01-29 12:06:57

问题


When reading data from a CSV file and then trying to add a new custom object to the list I get an error when running the script, but when I try to debug the code I works as intended, and I can't figure out why it does work in debug mode, but not when run normally.

    $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

Expected result: Array with measurements with the latest measurement at the end.

Error when not in debug mode:

Error: Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.


Update

Got it working now with the following update to the code, but I still would like to know why there is a difference in code execution between debug mode and normal mode.

    $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)

回答1:


I'm going to guess that this depends on how many entries are already in your logfile.

If there's just one entry, Import-Csv will return a single PSCustomObject (note, I'm using ConvertFrom-Csv below, but Import-Csv works the same way):

PS> $x = ConvertFrom-Csv "aaa, bbb, ccc`r`nxxx, yyy, zzz"
PS> $x.GetType().FullName
System.Management.Automation.PSCustomObject

but if there's multiple lines in the csv file you get an array of Objects instead:

PS> $x = ConvertFrom-Csv "aaa, bbb, ccc`r`nppp, qqq, rrr`r`nxxx, yyy, zzz"
PS> $x.GetType().FullName
System.Object[]

In the first case, PowerShell is telling you it doesn't know how to append a new entry onto a PSCustomObject because there's no op_Addition method defined on that type.

You can coerce the result of ConvertFrom-Csv / Import-Csv into an array in a few ways:

PS> $x = @( ConvertFrom-Csv "aaa, bbb, ccc`r`nxxx, yyy, zzz" )
PS> $x.GetType().FullName
System.Object[]

PS> [object[]] $x = ConvertFrom-Csv "aaa, bbb, ccc`r`nxxx, yyy, zzz"
PS> $x.GetType().FullName
System.Object[]

or, as you've found:

PS> [System.Collections.ArrayList] $x = ConvertFrom-Csv "aaa, bbb, ccc`r`nxxx, yyy, zzz"
PS> $x.GetType().FullName
System.Collections.ArrayList

And PowerShell knows how to do a += on all of these types...



来源:https://stackoverflow.com/questions/58640581/adding-pscustomobject-to-array-gives-error-but-works-fine-when-debugging-the-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!