Author Posts

February 8, 2018 at 3:39 pm


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 = $
$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


February 8, 2018 at 7:06 pm

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?

February 8, 2018 at 10:30 pm

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.