VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot

VM report daily

Hello, I would like something like this:

$VMS = get-vm

$report = @()

foreach ($vm in $VMS) {

$row = "" | select  "VM","DNS Name",PowerState,CPU,Memory,DiskProvisioned,DiskUsed,Cluster,Environment

$row."VM" = $vm.Name

$row."DNS Name" = $vm.Guest.get_HostName()

$row.PowerState = $vm.PowerState

$row.CPU = $vm.NumCPU

$row.Memory = $vm.MemoryGb

$row.DiskProvisioned = $vm.ProvisionedSpaceGB

$row.DiskUsed = [Math]::Round($vm.UsedSpaceGB,2)

$row.Cluster = ($vm | get-cluster).Name

$row.Environment = "PROD"

$report += $row

}

$report |Export-Csv C:\Users\gemela\Desktop\report.csv -NoTypeInformation

Also add info like: Version, Vmtools status and IP,

I would like to set this report to create new file (with date) every day (append) , example: report_2018_02_16.csv

Thanks for support

Tags (1)
24 Replies
LucD
Leadership
Leadership

Try like this

$VMS = Get-VM 

$report = @() 

foreach ($vm in $VMS) { 

    $row = "" | select  "VM","DNS Name",Version,IPAddress,VMToolsStatus,PowerState,CPU,Memory,DiskProvisioned,DiskUsed,Cluster,Environment 

    $row."VM" = $vm.Name 

    $row."DNS Name" = $vm.Guest.get_HostName()

    $row.Version = $vm.Version

    $row.IPAddress = $vm.ExtensionData.Guest.IpAddress -join '|'

    $row.VMToolsStatus = $vm.guest.State

    $row.PowerState = $vm.PowerState 

    $row.CPU = $vm.NumCPU  

    $row.Memory = $vm.MemoryGb 

    $row.DiskProvisioned = $vm.ProvisionedSpaceGB 

    $row.DiskUsed = [Math]::Round($vm.UsedSpaceGB,2

    $row.Cluster = ($vm | get-cluster).Name 

    $row.Environment = "PROD" 

    $report += $row

}

$report |

Export-Csv -Path "C:\Temp\report_$(Get-Date -Format 'yyyy_MM_dd').csv" -NoTypeInformation -UseCulture


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

antoniogemelli
Hot Shot
Hot Shot

It works but I think I made some mistake in DNS, I don't get dns.

Reply
0 Kudos
LucD
Leadership
Leadership

Seems to work for me.

Are you sure the VMware Tools are installed and running on the VM where DNS doesn't work?


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

I will run again to check but I'm quite sure all dns were wrong and vmtools is installed and running in 90% of them.

Reply
0 Kudos
LucD
Leadership
Leadership

Fyi: I ran my last test in vSPhere 6.5 with the PowerCLI Beta.

Can you check if $_.Guest.HostName is providing any values for the DNS property?


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

The columns DNS doesn't contain any dns, no idea why, getting like machine IP or in same cases part of FQDN

Reply
0 Kudos
LucD
Leadership
Leadership

Could this be a DNS issue?

Can you check DNS resolution on one of the VMs that doesn't report correctly?


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Always report SC + Ip, I tried comparison:

get-vm | select name, @{N="IPAddress"; E={$_.Guest.IPAddress[0]}}, @{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}

Script is fine for the rest.

Reply
0 Kudos
LucD
Leadership
Leadership

Could it be related to the guest OS you have running on the VMs?

Is it always the same guest OS for the VMs that do not report the hostname?


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

It's always RHEL (maybe different version) 5,6 and 7

Reply
0 Kudos
LucD
Leadership
Leadership

Could the Related Information at the bottom of KB2068759 explain what you are seeing?


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

antoniogemelli
Hot Shot
Hot Shot

Hi LucD,

I schedule this script to run with Task Schedule in windows jump,

$logincred = Get-VICredentialStoreItem -Host 0.0.0.0 -File C:\Users\gemela\my1.xml

$VMS = get-vm

$report = @()

foreach ($vm in $VMS) {

$row = "" | select  "VM","DNS Name",PowerState,CPU,Memory,DiskProvisioned,DiskUsed,Cluster,Version,VMToolsStatus,IPAddress

$row."VM" = $vm.Name

$row."DNS Name" = $vm.Guest.get_HostName()

$row.PowerState = $vm.PowerState

$row.CPU = $vm.NumCPU

$row.Memory = $vm.MemoryGb

$row.DiskProvisioned = $vm.ProvisionedSpaceGB

$row.DiskUsed = [Math]::Round($vm.UsedSpaceGB,2)

$row.Cluster = ($vm | get-cluster).Name

$row.Version = $vm.Version

$row.VMToolsStatus = $vm.guest.State

$row.IPAddress = $vm.ExtensionData.Guest.IpAddress -join '|'

$report += $row

}

$report | export-csv -Path  "C:\temp\smartlist_backup\reportVM__$(Get-Date -Format 'yyyy_MM_dd_hh_mm').csv"  -NoTypeInformation -UseCulture

If i run with Connect-VIServer 0.0.0.0 -User Domain\gemela -Password **********

it works, if I try to run with xml file I got error:

Get-VM        You are not currently connected to any servers. Please connect first using a Connect cmdlet.   

At C:\Users\gemela\Desktop\Backup_Smartlist\dailyVMreport.ps1:2 char:8

+ $VMS = get-vm

+        ~~~~~~

    + CategoryInfo          : ResourceUnavailable: (:) [Get-VM], ViServerConnectionException

    + FullyQualifiedErrorId : Core_BaseCmdlet_NotConnectedError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

I created file with:

Add-PSSnapin vmware.VimAutomation.core

New-VICredentialStoreItem –Host 0.0.0.0 –User Domanin\gemela –Password ********************* –File C:\Users\gemela\my1.xml

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Your script is missing the Connect-VIServer command. After line 1 you should insert the following line to your script:

Connect-VIServer -Server 0.0.0.0 -Credential $logincred

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
antoniogemelli
Hot Shot
Hot Shot

Thanks a lot 🙂

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Hello,

I just notice this script report VM name like in vcenter (normal),

But I have some VM with old naming convention and I would like to replace in each daily file the VM name from this list.

[io.file]::readalltext(“C:\temp\smartlist_backup\smartlist_”).replace("10-OS Node-150","te-150") | Out-File C:\temp\smartlist_backup\smartlist_ -Encoding ascii –Force

I did not test yet because there are around 100 VMs to replace, I'm wondering about Path and file name,

Each csv is different day by day because is generated with date/time

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Can't you just read all the CSV files in that folder?

foreach($csv in (Get-ChildItem -Path C:\Temp -Filter *.csv)){

   [io.file]::ReadAllText($csv.FullName).replace("10-OS Node-150","te-150") | Out-File -FilePath $csv.FullName -Encoding ascii –Force

}


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

I made like that and it works:

$report | Export-Csv C:\temp\smartlist_backup\smartlist -append

Then I add lines for each VM need to replace name:

[io.file]::readalltext(“C:\temp\smartlist_backup\smartlist”).replace("VM_120","VM120_test") | Out-File C:\temp\smartlist_backup\smartlist -Encoding ascii –Force

And then

$CurrentDate = Get-Date

$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_HH-mm-ss')

$list = Import-Csv C:\temp\smartlist_backup\smartlist

$list | Export-Csv C:\temp\smartlist_backup\smartlist_$CurrentDate.csv -Encoding ascii –Force

#remove  smartlist

cd C:\temp\smartlist_backup\smartlist

Remove-Item C:\temp\smartlist_backup\smartlist

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Hello again,

Not sure if is possible but I would like to know if is possible to get which kind of disk are using those VM, if tier0 or tier1 etc.

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership

Where and how would the script be able to find that out?

Through the datastorenames, the storage policy, interpolating the storage provider...?


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

Reply
0 Kudos