VMware Cloud Community
iliketurbos
Enthusiast
Enthusiast
Jump to solution

sending email with location of all vms in all vCenters

I have about 8 vCenters that i want to gather information on all vms and send an email telling me what host they are on and what vCenter they live on, here is what i have but man it takes FOREVERRRRR

 

any performance tweaks tips?


$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"
$name = "$vc"+"_$date"
$latest = "$vc"+"_Latest"
$vmsonvc = get-vm | select name, @{N="IPAddress"; E={$_.Guest.IPAddress[0]}}, @{N="VMDnsName"; E={$_.ExtensionData.Guest.Hostname}}, NumCPUs,MemoryGBs, Powerstate,VMHost, @{N="vCenter";E={$_.ExtensionData.CLient.ServiceUrl.Split('/')[2]}} | sort name
$vmsonvc | export-csv "C:\WhereAreTheVMs\$name.csv" -NoTypeInformation
sleep 30
$vmsonvc | Export-Csv -Force C:\WhereAreTheVMs\$latest.csv -NoTypeInformation

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Can you try the following two variations with Get-View?

$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"

$name = "$vc"+"_$date"

$latest = "$vc"+"_Latest"

&{foreach($vcInst in $global:DefaultVIServers){

    Get-View -ViewType VirtualMachine -Property Name,Guest,Config.Hardware,Runtime -Server $vcInst |

    select name,

        @{N="IPAddress"; E={$_.Guest.IPAddress}},

        @{N="VMDnsName"; E={$_.Guest.Hostname}},

        @{N='NumCPUs';E={$_.Config.Hardware.NumCPU}},

        @{N='MemoryGB';E={$_.Config.Hardware.MemoryMB/1KB}},

        @{N='Powerstate';E={$_.RunTime.PowerState}},

        @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -property Name).Name}},

        @{N="vCenter";E={$vcInst.Name}}

}} |

Sort-Object -Property name | ConvertTo-Csv -NoTypeInformation -UseCulture |

Tee-Object -FilePath "C:\Temp\$($name).csv" |

Set-Content -Path "C:\Temp\$($latest).csv"

and

$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"

$name = "$vc"+"_$date"

$latest = "$vc"+"_Latest"

Get-View -ViewType VirtualMachine -Property Name,Guest,Config.Hardware,Runtime |

select name,

    @{N="IPAddress"; E={$_.Guest.IPAddress}},

    @{N="VMDnsName"; E={$_.Guest.Hostname}},

    @{N='NumCPUs';E={$_.Config.Hardware.NumCPU}},

    @{N='MemoryGB';E={$_.Config.Hardware.MemoryMB/1KB}},

    @{N='Powerstate';E={$_.RunTime.PowerState}},

    @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -property Name).Name}},

    @{N="vCenter";E={([uri]$_.Client.ServiceUrl).Host}} |

Sort-Object -Property name | ConvertTo-Csv -NoTypeInformation -UseCulture |

Tee-Object -FilePath "C:\Temp\$($name).csv" |

Set-Content -Path "C:\Temp\$($latest).csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

You could avoid storing all the data in memory, and use the pipeline.

Something like this

$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"

$name = "$vc"+"_$date"

$latest = "$vc"+"_Latest"

Get-VM | select name,

        @{N="IPAddress"; E={$_.Guest.IPAddress[0]}},

        @{N="VMDnsName"; E={$_.ExtensionData.Guest.Hostname}},

        NumCPUs,MemoryGBs,Powerstate,VMHost,

        @{N="vCenter";E={$_.ExtensionData.CLient.ServiceUrl.Split('/')[2]}} |

Sort-Object -Property name | ConvertTo-Csv -NoTypeInformation -UseCulture |

Tee-Object -FilePath "C:\Temp\$($name).csv" |

Set-Content -Path "C:\Temp\$($latest).csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What exactly do you want to send via email?
The CSV?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
iliketurbos
Enthusiast
Enthusiast
Jump to solution

yeah, i'm sending the csv via email to our group and a few teams. it might also be the remote datacenters and a few other things slowing it down, since i'm making it a scheduled task it doesn't fully matter, was just trying to make it optimal Smiley Happy

am trying your version now, i left the last send-mailmessage line out since it had a bunch of internal info

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

We can make that version still a bit faster by going for Get-View instead of Get-VM.

But let me know if there is already some improvement in this Get-VM version.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
iliketurbos
Enthusiast
Enthusiast
Jump to solution

still takes roughly an hour, the get-vm being the majority of the time. i was wondering if going into each extension data was slowing it down, and couldn't find the appropriate get-view commands and subcategories. thanks and happy new years eve to you!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try the following two variations with Get-View?

$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"

$name = "$vc"+"_$date"

$latest = "$vc"+"_Latest"

&{foreach($vcInst in $global:DefaultVIServers){

    Get-View -ViewType VirtualMachine -Property Name,Guest,Config.Hardware,Runtime -Server $vcInst |

    select name,

        @{N="IPAddress"; E={$_.Guest.IPAddress}},

        @{N="VMDnsName"; E={$_.Guest.Hostname}},

        @{N='NumCPUs';E={$_.Config.Hardware.NumCPU}},

        @{N='MemoryGB';E={$_.Config.Hardware.MemoryMB/1KB}},

        @{N='Powerstate';E={$_.RunTime.PowerState}},

        @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -property Name).Name}},

        @{N="vCenter";E={$vcInst.Name}}

}} |

Sort-Object -Property name | ConvertTo-Csv -NoTypeInformation -UseCulture |

Tee-Object -FilePath "C:\Temp\$($name).csv" |

Set-Content -Path "C:\Temp\$($latest).csv"

and

$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"

$name = "$vc"+"_$date"

$latest = "$vc"+"_Latest"

Get-View -ViewType VirtualMachine -Property Name,Guest,Config.Hardware,Runtime |

select name,

    @{N="IPAddress"; E={$_.Guest.IPAddress}},

    @{N="VMDnsName"; E={$_.Guest.Hostname}},

    @{N='NumCPUs';E={$_.Config.Hardware.NumCPU}},

    @{N='MemoryGB';E={$_.Config.Hardware.MemoryMB/1KB}},

    @{N='Powerstate';E={$_.RunTime.PowerState}},

    @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -property Name).Name}},

    @{N="vCenter";E={([uri]$_.Client.ServiceUrl).Host}} |

Sort-Object -Property name | ConvertTo-Csv -NoTypeInformation -UseCulture |

Tee-Object -FilePath "C:\Temp\$($name).csv" |

Set-Content -Path "C:\Temp\$($latest).csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
iliketurbos
Enthusiast
Enthusiast
Jump to solution

&{foreach($vcInst in $global:DefaultVIServers)

slick! thank you sir, great work as always!

thank you for the protip.

Reply
0 Kudos
iliketurbos
Enthusiast
Enthusiast
Jump to solution

request has been modified some, if this is information you have easily available please share if you have the time, if not i shall report back. i've searched and couldn't find very much on the full inventory

VMName

Guest Name

Status

Power Status

vCPUs

vCPU

MemoryAssignedMB

vMem

NumberofVHD

# of VMDKs

VLanID1

PortGroup on primary interface

Cluster

Cluster

HostName

Hostname

OperatingSystem

VMware Version

TotalSizeGB

Total of allocated VMDKs

SerialNumber

VMware UUID

IPAddresses (only if easy)

All IPS as Array of Values; ‘xxx.xxx.xxx.xx’,’xxx.xxx.xxx.xxx’

MAC Addresses (only if easy)

All MAC as Array of Values; ‘xx:xx:xx:xx:xx:xx’,’xx:xx:xx:xx:xx:xx’

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$vc = 'AllVMs'

$date = (Get-Date -format u) -replace " ","_" -replace "-","_" -replace ":","_"

$name = "$vc"+"_$date"

$latest = "$vc"+"_Latest"

&{foreach($vcInst in $global:DefaultVIServers){

    Get-View -ViewType VirtualMachine -Property Name,Guest,Config.Hardware,Config.Uuid,Layout.Disk,Runtime,ResourcePool,Storage.PerDatastoreUsage,Network -Server $vcInst |

    select Name,

        @{N='Status';E={$_.Runtime.PowerState}},

        @{N="VMDnsName"; E={$_.Guest.Hostname}},

        @{N='NumCPUs';E={$_.Config.Hardware.NumCPU}},

        @{N='MemoryGB';E={$_.Config.Hardware.MemoryMB/1KB}},

        @{N='NumberofVHD';E={$_.Layout.Disk.Count}},

        @{N='VLanID1';E={Get-View -Id $_.Network[0] -Property Name | select -ExpandProperty Name}},

        @{N='Powerstate';E={$_.RunTime.PowerState}},

        @{N='VMHost';E={(Get-View -Id $_.Runtime.Host -property Name).Name}},

        @{N='Cluster';E={

            $parent = Get-View -Id $_.ResourcePool -Property Name,Parent

            while($parent -isnot [VMware.Vim.ClusterComputeResource] -and $parent.Parent){

                $parent = Get-View -Id $parent.Parent -Property Name,Parent

            }

            if($parent -is [VMware.Vim.ClusterComputeResource]){

                $parent.Name

            }

        }},

        @{N="vCenter";E={$vcInst.Name}},

        @{N='Hostname';E={$_.Guest.HostName}},

        @{N='OperatingSystem';E={$_.Guest.GuestId}},

        @{N='TotalSizeGB';E={

            [math]::Round(($_.Storage.PerDatastoreUsage.Committed |

                            Measure-Object -Sum |

                            Select -ExpandProperty Sum)/1GB,1)

        }},

        @{N='SerialNumber';E={$_.Config.Uuid}},

        @{N='IPAddresses';E={$_.Guest.IpAddress -join '|'}},

        @{N='MACAddresses';E={

            ($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]} |

            select -ExpandProperty MacAddress) -join '|'

        }}

}} |

Sort-Object -Property name | ConvertTo-Csv -NoTypeInformation -UseCulture |

Tee-Object -FilePath "C:\Temp\$($name).csv" |

Set-Content -Path "C:\Temp\$($latest).csv"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos