VMware Cloud Community
bpbhai
Contributor
Contributor

Power shell script to retrieve information on VM

Hi , I would like to create a report in which i would like to retrieve information about VM's from a Data Center , the information on VM should contain

1. The Operating system 

2. Name of VM

3. VM host

4. Ip address

5. Mac address

and if possible custom attributes

can some one help me here:  i already tried this script but somehow for some machines OS info is not coming and hence report is not getting published , also i have not included custom attributes in this script

$VCServerName = "..................."

$VC = Connect-VIServer $VCServerName

$datacenter= "datacenter name"

$ExportFilePath = "D:\Export-VMInfo.csv"

$Report = @()

$VMs = Get-Datacenter $datacenter | Get-VM

ForEach ($VM in $VMs) {

      $VMView = $VM | Get-View

  write-host $VMView

      $VMInfo = {} | Select VMName,OS,IPAddress,Host

      $VMInfo.VMName = $vm.name

     

      $VMInfo.OS = $vm.Guest.OSFullName

    

      $VMInfo.IPAddress = $vm.Guest.IPAddress[0]

  

      $VMInfo.Host = $vm.host.name

     

      $Report += $VMInfo

}

$Report = $Report | Sort-Object VMName

Reply
0 Kudos
4 Replies
Alex_Romeo
Leadership
Leadership

Hi,

these links may be useful to you:

VMware PowerCLI Forum - VMware {code}

Script to get VMs name, host and IP filtered

Script to get memory, cpu and disk allocate

Alessandro Romeo

Blog: https://www.aleadmin.it/
Reply
0 Kudos
lmoglie
Enthusiast
Enthusiast

Hi,

something like this??

Connect-VIServer -Server <VCSA> -User <Userrname> -Password <Password>

$DTC = "<Datacenter>"

$Report = @()

ForEach ($VM in (Get-Datacenter $DTC) | Get-VM) {

$tempvm=@{}

$tempvm.Name = $VM.Name

$tempvm.GuestOS = If (!$VM.Guest.OSFullName) {"Tools Not Running\Unknown"} Else {$VM.Guest.OSFullName}

$tempvm.IP = If (!$VM.Guest.IPAddress[0]) {"Tools Not Running\Unknown"} Else {$VM.Guest.IPAddress[0]}

$tempvm.FullName = If (!$VM.Guest.hostname) {"Tools Not Running\Unknown"} Else {$VM.Guest.hostname}

$tempvm.MacAddress = (Get-NetworkAdapter -VM $VM.Name).MacAddress

#$tempvm.CustomFields = $VM.CustomFields  # Custom Attributes

  $temp = New-Object -TypeName PSObject -Property $tempvm

$Report += $temp

}

$Report | Select Name, GuestOS, IP, FullName, MacAddress |  Sort Name | export-csv ".\Export-VMInfo.csv"

Best Regards

Lorenzo Moglie

Reply
0 Kudos
bpbhai
Contributor
Contributor

Thanks for this bout it's not printing the OS of the VM which are switched off, as I have to get all the machines which have windows 7 as their OS, this information is not coming

I tried with this but it's not working , maybe I am doing some mistake.

$tempvm.ConfigGuestOS = $VM.Config.GuestFullName

Reply
0 Kudos
mouraMXmouraMX
Contributor
Contributor

Hi,

Why don't you use RVTools instead? (https://www.robware.net/rvtools/ )

It's easier if you only want a report.

Reply
0 Kudos