0

我编写了这个脚本来生成一个 csv 文件:

$VMs = Get-AzureRmVM
$vmOutput = @()
$VMs | ForEach-Object { 
  $tmpObj = New-Object -TypeName PSObject
  $tmpObj | Add-Member -MemberType Noteproperty -Name "VM Name" -Value $_.Name
  $tmpObj | Add-Member -MemberType Noteproperty -Name "VM Type" -Value $_.StorageProfile.osDisk.osType
  $tmpObj | Add-Member -MemberType Noteproperty -Name "VM Profile" -Value $_.HardwareProfile.VmSize
  $tmpObj | Add-Member -MemberType Noteproperty -Name "VM OS Disk Size" -Value $_.StorageProfile.OsDisk.DiskSizeGB
  $tmpObj | Add-Member -MemberType Noteproperty -Name "VM Data Disk Size" -Value $_.StorageProfile.DataDisks.DiskSizeGB
  $vmOutput += $tmpObj
}
$vmOutput | export-csv C:\Users\***\data.csv -delimiter ";" -force -notypeinformation

但是,由于最后一列 VM 数据磁盘大小在文件中存储了一个多于多个数据(可以有多个磁盘),因此它们显示为System.Object[]. 如何强制 powershell 将此数据加入单个字符串?

我尝试在最后一行之后的第 9 行广告之后添加-join参数,但没有令人满意的结果。$tmpObj$vmoutput

4

2 回答 2

3

尝试将分层数据转换为结构化数据总是很棘手。像这样的东西实际上应该起作用:

$VMs = Get-AzureRmVM
$vmOutput = $VMs | ForEach-Object { 
    [PSCustomObject]@{
        "VM Name" = $_.Name
        "VM Type" = $_.StorageProfile.osDisk.osType
        "VM Profile" = $_.HardwareProfile.VmSize
        "VM OS Disk Size" = $_.StorageProfile.OsDisk.DiskSizeGB
        "VM Data Disk Size" = ($_.StorageProfile.DataDisks.DiskSizeGB) -join ','
    }
}
$vmOutput | export-csv C:\Users\***\data.csv -delimiter ";" -force -notypeinformation

我只是无法测试 - 抱歉。

于 2018-06-29T10:14:22.423 回答
0

我也无法对此进行测试,但这也可以吗?

$VMs = Get-AzureRmVM
$vmOutput = $VMs | ForEach-Object { 
    [PSCustomObject]@{
        "VM Name" = $_.Name
        "VM Type" = $_.StorageProfile.osDisk.osType
        "VM Profile" = $_.HardwareProfile.VmSize
        "VM OS Disk Size" = $_.StorageProfile.OsDisk.DiskSizeGB
        "VM Data Disk Size" = $_.StorageProfile.DataDisks.DiskSizeGB | Out-String
    }
}
$vmOutput | export-csv C:\Users\***\data.csv -delimiter ";" -force -notypeinformation
于 2018-06-29T10:25:53.343 回答