在 powershell 中以自定义格式将数据导出到 CSV 文件

export data to CSV file with custom format in powershell

我正在尝试以某种方式获取值和格式。我尝试了几种方法,但没有用。我是 Powershell 的新手,还在学习中。

Add-Content -Path data.csv -Value ‘"Type","Res","Size”'
$aList= @()

 $Accountinfo = Get-AzStorageAccount -ResourceGroupName $resName
        IF ($($Accountinfo).count -gt 0) {
            foreach ($accountiinform in $Accountinfo) {
                $Names = "Storage Account"
                $props = [ordered]@{
                    "Res" = $Names
                    "Name"     = $accountiinform.StorageAccountName
                }
                $aList += New-Object pscustomobject -Property $props
            }
        }
    # There are more if statement-checking more resources and adding the $props object to $alist

$aList | foreach { Add-Content -Path data.csv -Value $_ }

data.csv 有什么:

"Type","Res","Size"
@{Res=Storage Account; Name=dataStorage;}
@{Res=VM; Name=dataVM; Size=250 GB;}
@{Res=VM; Name=dataVM2; Size=500 GB;}

我想要的:

"Type","Res","Size"
Storage Account, Name=dataStorage,
VM,dataVM, 250 GB
VM, dataVM2,500 GB

请将最后一行改为:

Replace
$aList | foreach { Add-Content -Path data.csv -Value $_ }

With this:
$aList | Export-Csv -Path C:\Temp\data.csv -NoTypeInformation