I am not able to export the out put to csv/xls file. Please help me

Welcome Forums General PowerShell Q&A I am not able to export the out put to csv/xls file. Please help me

This topic contains 2 replies, has 3 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #93274

    Points: 0
    Rank: Member


    Please find below my script. I am not able to export the output to csv/xls file. Please help me. And also i have 10 vcenters, I need this report in xls format with each VC worksheets in one excel file.

    $VM = Get-Cluster test-cluster | Get-VM | `
    ForEach-Object {
    $VM = $_
    $VMview = $VM | Get-View
    $Snapshots = $VM | Get-Snapshot
    $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,VMXname,VmdkSizeGB,DatastoreName,SnapshotCount,Owner
    $Report.VMName = $VM.name
    $Report.vmCreatedByUser = Get-VIEvent $VM | sort createdTime | select -first 1 | select UserName | % {$_.UserName.split("\")[1]}
    $Report.vmCreatedDate = Get-VIEvent $VM | sort createdTime | select -first 1 | select CreatedTime
    $Report.ESXname = $VM.VMHost
    $Report.ClusterName = ($VM | Get-Cluster).Name
    $Report.MemoryGB = $VM.MemoryMB/1024
    $Report.vCPUcount = $VM.NumCpu
    $Report.VMXname = $VM.ExtensionData.Config.Files.VmPathName.Split("/")[1]
    $Report.VmdkSizeGB = $VM.UsedSpaceGB
    $Report.DatastoreName = $VMview.Config.DatastoreUrl
    $Report.SnapshotCount = ($VM | Get-Snapshot).Count
    $Report.Owner = $VM | Get-VIPermission | Where-Object {$_.Role -like "xxxxxx*"} | select Principal | % {$_.Principal.split("\")[1]}
    Write-Output $Report


  • #93310

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Please consider enclosing your code in blocks, as indicated in the instructions, to format it. That makes it a lot easier to follow and to help ;).

    You're kind of going about the custom-object creation in an odd way, but is it safe to assume that you're seeing the desired output on the screen when you run this?

  • #93333

    Points: 1,141
    Helping Hand
    Rank: Community Hero

    OK, the custom object issues effort notwithstanding.
    I am almost, completely baffled by what you are trying to do here.

    Firstly, You are assigning the results to a variable, here...

        $VM = Get-Cluster test-cluster | Get-VM | `

    ... and using another assignment to the same variable here:

        $VM = $_

    Why are you doing this?

    Then have this select..

        $Report = "" | Select-Object VMname,vmCreatedByUser,vmCreatedDate,ESXname,ClusterName,MemoryGB,vCPUcount,VMXname,VmdkSizeGB,DatastoreName,SnapshotCount,Owner

    ... you are trying to select from an empty string. This should just error.

    Are you not just overwriting here vs appending?

        Write-Output $Report

    I mean since you are inside a loop and not using -append.

    Full disclosure:
    I am not a VMWare person, I mean I have used it, but I spend my time in Hyper-V. So many of these VMW cmdlets, I've never seen, and thus, I cannot comment on them.

    Yet, trying to do even the minimum of what you show here, just completely fails when I try it in my simple test VMWare environment.

The topic ‘I am not able to export the out put to csv/xls file. Please help me’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort