Azure/Azure-Network-Security

[System.Management.Automation.PSCustomObject] does not contain a method named 'toarray'.

Closed this issue · 3 comments

While running ".\Get-DanglingDnsRecordsPsDesktop.ps1 -FetchDnsRecordsFromAzureSubscription" this happens:

Get-DanglingDnsRecordsPsDesktop.ps1 : Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'toarray'.

jf781 commented

Any updates on this? I'm also experiencing this same issue.

jf781 commented

So I did some digging and found when running the following on PS Core on Mac OS or from the CloudShell I got the error below.

Get-DanglingDnsRecords.ps1: Method invocation failed because [System.Management.Automation.PSCustomObjec
t] does not contain a method named 'toarray'.

I found the following code on line 999 that was related to this.

If ($AzResourcesHash.count -gt 0) {
  $AzResourcesList = $AzResourcesHash.Values.toarray()

  $AzResourcesList | Export-Csv $outputResourcesFile -NoTypeInformation -Force
  Write-Host "Fetched $($AzResourcesHash.values.count) Azure resources; Saved the file as: $outputResourcesFile" -ForegroundColor Green
}

And I ran the necessary commands to get the $AzResourcesHash variable as defined in the script and saw it did not have the 'toarray' method as indicated in the error.

$AzResourcesHash.Values.
Count           SyncRoot        Equals          GetHashCode     ToString        ForEach
IsSynchronized  CopyTo          GetEnumerator   GetType         Where

To get around this I updated the if statement on line 999 with the following.

If ($AzResourcesHash.count -gt 0) {
  $AzResourcesList = @()
  foreach ($hash in $AzResourcesHash) {
    $AzResourcesList += $hash
  }

  $AzResourcesList | Export-Csv $outputResourcesFile -NoTypeInformation -Force
  Write-Host "Fetched $($AzResourcesHash.values.count) Azure resources; Saved the file as: $outputResourcesFile" -ForegroundColor Green
}

It runs as it supposed to now.

That said, I'm sure there is a more efficient way of doing this so open to any feedback on this.

@jerbro OK to close this ?