PowerShell: How can I to force to get a result as an Array instead of Object

前端 未结 4 556
旧巷少年郎
旧巷少年郎 2020-12-03 16:35
$result = Get-ADUser -Filter $filter

If I have 2 or more results, I get $x as array, but if I have only one result, a get $x as object. How to make

相关标签:
4条回答
  • 2020-12-03 17:13

    I had the same issue using an indexed value in a loop. I fixed it by changing

    $PatchGroupData.SCCM_Collection[$i]
    

    to

    @($PatchGroupData.SCCM_Collection)[$i]
    
    0 讨论(0)
  • 2020-12-03 17:21

    By the way, the other solutions in this question are not really the best way to do this, for the reasons stated in their comments. A better way is simply to put a comma before the function, like

    $result = ,(Get-ADUser -Filter $filter)
    

    That will put an empty result into an empty array, a 1 element result into a 1 element array and a 2+ element result into an array of equal elements.

    0 讨论(0)
  • 2020-12-03 17:27

    Also, you can use $x=[array]get-aduser

    0 讨论(0)
  • 2020-12-03 17:28

    Try $x = @(get-aduser)

    The @() syntax forces the result to be an array

    0 讨论(0)
提交回复
热议问题