Home:ALL Converter>powershell psobject getenumerator one

powershell psobject getenumerator one

Ask Time:2014-02-21T23:53:33         Author:EdLipson

Json Formatter

I borrowed some PowerShell code to compare hash table, and it returns a custom object with the name and difference indication of the hash entries. I want to output the difference returned.

The object:

 function result( [string]$side ) {
    if ($ReturnVals) {
        New-Object PSObject -Property @{
          'InputKey'= "$path$key";
          'SideIndicator' = $side;
          'ReferenceValue' = $refValue;
          'DifferenceValue' = $difValue;
        }
    }
    else {
        New-Object PSObject -Property @{
          'InputKey'= "$path$key";
          'SideIndicator' = $side;
        }
    }
  }

When working with the returned object, if it is Null or has more than one entry, everything is fine, GetEnumerator does what is needed and output is sorted into the files:

if ($comp -eq $Null) {
    write-host $d "No Differences"
    out-file -filepath $of -inputobject "`nNo Differences" -Encoding UTF8 -append
}
else {
    write-host $d "Differences"
    $comp.GetEnumerator() | Sort-Object -property InputKey |
                out-file -filepath $of -append -Encoding UTF8
}

If there is one difference, PowerShell throws an error as the object has no method GetEnumerator:

Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'GetEnumerator'.

I tried to use .count to see if there is one difference, but I don't get a count with just one. I get a count with 2 or more.

Custom Objects are a little advanced for my PowerShell skills. Any suggestions on how to prevent the error for one item in the object?

Author:EdLipson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21939026/powershell-psobject-getenumerator-one
mjolinor :

Get-Enumerator is an array method. If the function only returns one object, you don't get an array so the method doesn't work. This is normal and expected behaviour.\n\nYou can modify the function to always return an array, but that's kind of a non-standard approach. You want the function to behave like other cmdlets and functions as much as possible. Better to force it into an array outside of the function, in your main script:\n\n$comp = @(result)\n",
2014-02-21T16:17:35
Cole9350 :

Use the comma operator to always return the value as an array :\n\nfunction result( [string]$side ) {\n if ($ReturnVals) {\n $obj = New-Object PSObject -Property @{\n 'InputKey'= \"$path$key\";\n 'SideIndicator' = $side;\n 'ReferenceValue' = $refValue;\n 'DifferenceValue' = $difValue;\n }\n }\n else {\n $obj = New-Object PSObject -Property @{\n 'InputKey'= \"$path$key\";\n 'SideIndicator' = $side;\n }\n }\n return ,$obj\n}\n",
2014-02-21T16:05:35
rtf :

Seems like when $comp is one item, it is not an array. You need to \"initialize\" $comp as an empty array. I don't see where it's coming from in your code, but something like this ought to work:\n\n#This creates an empty array\n$comp = @()\n$comp += $foo\n",
2014-02-21T16:03:25
yy