Powershell:export-csv文件中的System.Object [] [英] Powershell: System.Object[] in export-csv file

查看:443
本文介绍了Powershell:export-csv文件中的System.Object []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote this script to generate a csv file:

$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将这些数据连接到单个字符串中?

But as the last column VM Data Disk Size stores one than more data (there can be multiple disks) in file they're presented as System.Object[]. How can I force powershell to join this data into a single string?

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

I tried to add -join parameter after $tmpObj in line 9 ad after $vmoutput in last line, but there were no satisfactory results.

推荐答案

尝试将层次结构数据转换为结构化数据总是很棘手.这样的事情实际上应该可以工作:

It's always tricky to try to convert hierarchical data into structured data. Something like this should actually work:

$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

我只是无法测试-抱歉.

I just cannot test - sorry.

这篇关于Powershell:export-csv文件中的System.Object []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆