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

时间:2018-06-29 09:55:47

标签: powershell azure csv export export-csv

我写了这个脚本来生成一个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参数,但在最后一行的export class Parser { static instance: Parser jsonData: {} = {} nativeURL: string fileName = 'data.json' private constructor(private file: File) { } doFileInteractions() { let that = this let writeDataCompletionHandler = function (success, data) { if (success) { console.log('Final jsonData') console.log(that.jsonData) } else { } } let readDataCompletionHandler = function (success, data) { if (success) { that.jsonData = JSON.parse(data) console.log('JSON data...') console.log(that.jsonData) that.writeJsonData(writeDataCompletionHandler) } else { } } this.readJsonData(readDataCompletionHandler) } readJsonData(completionHandler) { let dirName = 'www/assets/data' console.log('this.file.applicationDirectory>>') console.log(this.file.applicationDirectory) this.file.listDir(this.file.applicationDirectory, dirName) .then((list) => { console.log('Success: listDir') console.log(list) list.forEach(element => { if (element.name == this.fileName) { // READ FILE this.nativeURL = element.nativeURL.replace('/' + this.fileName, '') this.file.readAsText(this.nativeURL, this.fileName) .then((result) => { console.log('Success: readAsText') console.log(result) completionHandler(true, result) }) .catch((error) => { console.log('Failure: readAsText') console.log(error) completionHandler(false, error) }) console.log('nativeURL: ') console.log(this.nativeURL) } }); }) .catch((error) => { console.log('Failure: listDir') console.log(error) }) } writeJsonData(completionHandler) { // WRITE FILE this.jsonData['data']['newData'] = 'X1234' let stringyfiedJson = JSON.stringify(this.jsonData) this.file.writeFile(this.nativeURL, this.fileName, stringyfiedJson, { append: false, replace: true }) .then((result) => { console.log('Success: Writefile') console.log(result) completionHandler(true, result) }) .catch((error) => { console.log('Failure: WriteFile') console.log(error) completionHandler(false, error) }) } static setInstance(file: File) { if (!this.instance) { this.instance = new Util_Parser(file) } } 之后,但是没有令人满意的结果。

2 个答案:

答案 0 :(得分:2)

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

$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

我只是无法测试-抱歉。

答案 1 :(得分: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