Skip to content

Use .count instead of .Length to get length of $taskArgs #2

@daren-thomas

Description

@daren-thomas

if ($taskArgs.Length -eq 0) {

Ever seen this message:

PropertyNotFoundException: C:\Users\dtm\Documents\PowerShell\Modules\PwshRun\1.3.2\pwshrun-bootstrap.ps1:183
Line |
 183 |      if ($taskArgs.Length -eq 0) {
     |          ~~~~~~~~~~~~~~~~~~~~~~
     | The property 'Length' cannot be found on this object. Verify that the property exists.

So, I did some digging, and... while $taskArgs really does seem to be an IList<object>, powershell seems to be totally weird about it and (probably) wraps collections internally or something.

According to https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.3#count, you can actually just use the .count property to get the length (tested on my machine). Note the lowercase. That's intentional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions