VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Get VM from two vcenter and generate a unique csv file

Hello, I have a script  where I already raise a post sometimes ago.

Basically I set this script to run in two different vcenter every day but in different time,

$VMS = get-vm

$report = @()

foreach ($vm in $VMS) {

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

$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

$report += $row

}

I have a request now to modify (if possible):

A unique script for both vcenter who generate only one file csv or a way to combine both file in a unique file  and send  via SCP to another server.

Also, date format should be like this: 'dd-mm-yyyy_hh-mm-ss' 

I'm testing if will work with this format (customer request) :

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

Thanks 🙂

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.

The script assumes you are connected to both vCenters.

$report = foreach($vc in $global:DefaultVIServers){

   Get-VM -Server $vc |

  Select @{N='vCenter';E={$vc.Name}},Name,

  @{N='DNS Name';E={$_.Guest.get_HostName()}},

  PowerState,NumCPU,MemoryGb,

  @{N='ProvisionedSpaceGB';E={[Math]::Round($_.ProvisionedSpaceGB,2)}},

  @{N='DiskUsed';E={[Math]::Round($_.UsedSpaceGB,2)}},

  @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

  Version

}


$report |

Export-Csv -Path "C:\Users\gemela\Desktop\reportVM__$(Get-Date -Format 'yyyy-MM-dd_HH-mm').csv" -NoTypeInformation -UseCulture


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

View solution in original post

7 Replies
LucD
Leadership
Leadership
Jump to solution

Can you reach the two vCenters from the station where you run the script?


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

0 Kudos
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Yes, I use to run from a jump server both script.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

The script assumes you are connected to both vCenters.

$report = foreach($vc in $global:DefaultVIServers){

   Get-VM -Server $vc |

  Select @{N='vCenter';E={$vc.Name}},Name,

  @{N='DNS Name';E={$_.Guest.get_HostName()}},

  PowerState,NumCPU,MemoryGb,

  @{N='ProvisionedSpaceGB';E={[Math]::Round($_.ProvisionedSpaceGB,2)}},

  @{N='DiskUsed';E={[Math]::Round($_.UsedSpaceGB,2)}},

  @{N='Cluster';E={Get-Cluster -VM $_ | Select -ExpandProperty Name}},

  Version

}


$report |

Export-Csv -Path "C:\Users\gemela\Desktop\reportVM__$(Get-Date -Format 'yyyy-MM-dd_HH-mm').csv" -NoTypeInformation -UseCulture


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

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Great, it works 🙂 Thanks a lot LucD

0 Kudos
antoniogemelli
Hot Shot
Hot Shot
Jump to solution

I would like to send every day the new file to another machine,

Is possible to put just the latest file every day?

Start-Process 'C:\Program Files (x86)\PuTTY\pscp.exe' -ArgumentList ("-scp -pw ******* C:\Smartlist\file smartlist@10.10.10.10 :/opt/invoicing/smartlist")

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can pick the most recent file like this

$file = Get-ChildItem -Path C:\Smartlist\file |

  Sort-Object -Property LastWriteTime -Descending |

  select -First 1 -ExpandProperty FullName


$sProc = @{

  FilePath = 'C:\Program Files (x86)\PuTTY\pscp.exe'

  ArgumentList = "-scp -pw ******* $file smartlist@10.10.10.10 :/opt/invoicing/smartlist"

}

Start-Process @sProc


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

antoniogemelli
Hot Shot
Hot Shot
Jump to solution

Thanks a lot LucD

0 Kudos