0

I wrote the below powershell script to get all Azure policy assignment within an azure subscription but when I try to export the result using Export-CSV, I get a System.String[] error for the NotScope row.

Get-AzResourceGroup | Select-Object  ResourceGroupName 
ForEach-Object { (Get-AzPolicyAssignment).Properties | ForEach-Object {
    $mgId += [PSCustomObject]@{
      SubscriptionId    = $SubscriptionId
      SubscriptionName  = $subscriptionName
      ResourceGroupName = $resourceGroupName
      PolicyName        = $_.Name
      DisplayName       = $_.DisplayName
      Scope             = $_.Scope
      NotScope          = $_.NotScopes
    }
  }
}
$mgId | Export-CSV

Anyone know what I'm missing/

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    `Export-Csv` and `ConvertTo-Csv` do not meaningfully support objects with properties that contain _collections_, such as arrays. If you want to represent all collection elements in a CSV column value, you'll have to create a string representation of the collection yourself. See the linked duplicate for details. – mklement0 Jun 23 '22 at 14:34
  • As an aside: `System.String[]` isn't an _error_, it is how the _array_ contained in `$_.NotScopes` gets (meaninglessly) stringified by `Export-Csv`. That is, `.ToString()` is simply called; verify with `([string[]] 'foo').ToString()`. Also, your `ForEach-Object` call is receiving no input. – mklement0 Jun 23 '22 at 14:36
  • @mklement0 From reading your response here and in the linked duplicate, you suggested the script using this format PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | Select-Object col1, @{ n='col2'; e={ $_.col2 -join ' ' } } | ConvertTo-Csv "col1","col2" "1","2 3" But when I do using Select-Object SubscriptionId, SubscriptionName, ResourceGroupName, PolicyName, DisplayName, Scope, @{ n='NotScope'; e={$_.NotScopes -join''}} | Export-CSV with the Export-CSV cmdlet rather than the ConvertTo-CSV, I get no information in the created CSV file, – Shynepapin101 Jun 23 '22 at 15:54
  • To add, all of the variables created hold values, only the NotScope hold multiple values in some instances not all instance. – Shynepapin101 Jun 23 '22 at 16:01
  • The `ConvertTo-Csv` command has pipeline input, which is necessary; your `Export-Csv` does not - perhaps you only omitted that for brevity (other than that, I see no issue). It's hard to troubleshoot in _comments_ and in any event this is a separate problem, so I encourage you to create a _new_ question post focused on the problem you now have, ideally in the form of a [mcve]. – mklement0 Jun 23 '22 at 16:03
  • All I'm trying to do is to get Azure policy assignment for all the management group and output the Policy Name, Policy Display Name, Scope, NotScope to a csv file. – Shynepapin101 Jun 23 '22 at 20:24
  • Understood. Now that you're past the original problem, but are having trouble implementing the suggested solution, I again suggest creating a new question post, detailing the specific problem you're _now_ having, including error messages, if any. – mklement0 Jun 23 '22 at 21:46

0 Answers0