How to get server information from VMware

999 views Asked by At

I have access to the VMWare GUI and I can easily export all the columns such as UPtime, IPAddress, Notes, DNS, GuestOs, State, Name and so on.

I want to right a script that can automatically get this information daily. So gar I was only able to get the server name, power state and VMhost. for some reason VMware is making it so hard to extract that information. I used the script below and I thought by adding the columns I mentioned above to this script, I should be able to retireve the data I need. But it doesn't work that way. Can someone please tell me how I can get this information?

Thanks,

Add-PSSnapin vmware.vimautomation.core
Connect-VIServer SERVERNAME
Get-VM|Select Name, VMHost, Id, PowerState
Exit 0
2

There are 2 answers

0
mkatouzi On BEST ANSWER

After digging into the system and many hours of research I found the solution. I just wish VMWare would make it easier to retrieve data or at least improve the manual. The following code creates two files; one with the server information and another one with Uptime information.

Get-VM | select name,VMHost, @{ Name = "IP Addresses"; Expression = { $_.Guest.IPAddress }}, @{ Name = "PowerState"; Expression = { $_.Guest.State }} , @{ Name = "GuestOS"; Expression = { $_.Guest }}, Notes | Export-Csv -Path "HQstat.csv"
Get-Stat -Entity * -Stat sys.uptime.latest -Realtime -MaxSamples 1| Export-Csv -Path "HQtime.csv"
0
Wisman On

Why not use the views? They have all the information that you need. Code below assumes you are connected to the vCenter.

$vmView = Get-View -ViewType VirtualMachine -Property Name,Config,Guest,Runtime
$hostView = Get-View -ViewType HostSystem -Property Name
$date = Get-Date
Foreach ($vm in $vmView)
{
    If ($vm.Runtime.BootTime -ne $null)
    {
        $dateDiff = $date.Subtract($vmView.Runtime.BootTime)
    }
    Else
    {
        $dateDiff = $null
    }
    foreach ($h in $hostView)
    {
        If ($vm.Runtime.Host -eq $h.MoRef)
        {
            $tempHost = $($h.Name)
            Break
        }
    }
    $global:Export += @([PSCustomObject]@{
        VMName = $($vm.Name)
        ID = $($vm.Config.Uuid) #or use $($vm.MoRef)
        Host = $tempHost
        PowerState = $($vm.Guest.GuestState)
        IPAddress = $($vm.Guest.IPAddress)
        Notes = $($vm.Config.Annotations)
        UptimeMinutes = $($dateDiff.TotalMinutes)
        })

    $dateDiff = $null
    $tempHost = $null
}
$exportFileName = "C:\temp\VMInformation.csv"

$Export | Export-Csv $exportFileName -Force -NoTypeInformation