VMware Cloud Community
Shalomon1991
Contributor
Contributor

Script to fetch multiple VM information from Multiple Vcenter server

Note: I have set my powercli configuration - DefaultVI server mode to multiple, inorder for the script to fetch information about vm's from multiple vcenter server

$outputFile = "C:\Temp\FetchVmInfo\Vminfo.csv"

$VMS = Get-Content C:\Temp\FetchVmInfo\VMS.txt

$VCenterlist = Get-Content C:\Temp\FetchVmInfo\VCenterlist.txt

Foreach ($vcenter in $VCenterlist)

{

"Connecting vCenter servers ..."

Connect-VIServer $vcenter

$vms = Get-VM -Name $VMS | Select-Object Name, NumCPU, MemoryMB | Export-Csv "vm-report.csv"

}

********* Script works fine, I was able to fectch information about VM's from different vcenter server and was able to export it to CSV File..

But i get an error message at the end after running the script

"Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and

then try the command again."

Can someone help me on this??

Thanks,

Shalomon

Tags (1)
35 Replies
LucD
Leadership
Leadership

Ok, I think I know why.

The $reportNotFound variable is an array of strings, not an array of objects, hence the issue with Export-Csv.


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

Shalomon1991
Contributor
Contributor

Again Thanks for your kindly help @LucD!

Reply
0 Kudos
Shalomon1991
Contributor
Contributor

Hi LucD

Among getting VM information's like powerstate, Vcpu and RAM.. Is it possible to get Operating system of the VM?

Using the above script?

Thanks,

Shalomon.N

Reply
0 Kudos
LucD
Leadership
Leadership

To get the correct value you will need the VMware Tools to be installed on the VMs.
And the VM needs to be powered on to get a correct value.

Is that the case?


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

Reply
0 Kudos
LucD
Leadership
Leadership

Try like this

$outputFile = "C:\Temp\FetchVmInfo\Vminfo.csv"

$notFoundFile = "C:\Temp\FetchVmInfo\Vm-notfound.csv"

$VMS = Get-Content C:\Temp\FetchVmInfo\VMS.txt

$VCenterlist = Get-Content C:\Temp\FetchVmInfo\VCenterlist.txt

"Connecting vCenter servers ..."

Connect-VIServer $VCenterlist

"Getting VM(s). Be patient, this can take some time ..."

$report = @()

$reportNotFound = @()

Foreach($vmName in $VMS)

{

    $vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue

    if($vm){

        $report += ($vm  | Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB,@{N='OS';E={$_.Guest.OSFullName}})

        "VM $($vmName) information has been fetched"

    }

    else{

        $reportNotFound += $vmName  

    }

}

$report | Export-CSV $outputFile

$reportNotFound | Out-file -append "C:\Temp\FetchVmInfo\VMerror.txt"

"File has been exported"


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

Shalomon1991
Contributor
Contributor

LucD Thanks for the help! Script is working and i was able to fetch OS information of VM.

Thanks,

Shalomon.N

Reply
0 Kudos
vmCalgary
Enthusiast
Enthusiast

LucD,

How do you capture the vcenter that a particular VM may be on when the environment has mulitiple vCenters? I'd like to add to this script to list the vcenter for each vm.

Thanks in advance,

Joy

Reply
0 Kudos
LucD
Leadership
Leadership

One way is like this

Get-VM |

Select Name,@{N='vCenter';E={([system.uri]$_.ExtensionData.Client.ServiceUrl).Host}}


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

Reply
0 Kudos
nitinh
Contributor
Contributor

Hi Luc, Do you have any script which used to fetch all virtual centers current capacity allocated vs used. I dont want detailed capacity just want name of the VC, Total hosts, CPU allocated vs used, Memory allocated vs Used and storage allocated vs used in VC level. we have 14 VC and just need high level information on each vc
Reply
0 Kudos
LucD
Leadership
Leadership

Is that allocated vs used per ESXi node? Or per cluster? Or per vCenter?

Btw, since this doesn't seem to be related to this thread, I propose you open a new thread.

Include what you already have?

And what is missing or failing?


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

Reply
0 Kudos
Jpentony
Contributor
Contributor

HI LucD,

where can I include this part (to fetch the vCenter of the VM attached to it) within the script ?

Get-VM |

Select Name,@{N='vCenter';E={([system.uri]$_.ExtensionData.Client.ServiceUrl).Host}}

Reply
0 Kudos
LucD
Leadership
Leadership

You can replace this line

$report += ($vm  | Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB,@{N='OS';E={$_.Guest.OSFullName}})

with this line

$report += ($vm  | Select-Object Name, @{N='vCenter';E={([system.uri]$_.ExtensionData.Client.ServiceUrl).Host}},
   Powerstate, NumCPU, MemoryMB, MemoryGB,@{N='OS';E={$_.Guest.OSFullName}})

 


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

Jpentony
Contributor
Contributor

Thanks LucD

I was able to tweak the script. 

Within the script how do I include the output of the Folder/Cluster name on which the VM is part of ?

Thanks in advance.

Reply
0 Kudos
kbhowmick123
Enthusiast
Enthusiast

Hi Lucd,

 

When I'm trying this script with a list of vm's as input through VMS file, it's giving information for all the vm's that are present in the vCenter.  

Reply
0 Kudos
LucD
Leadership
Leadership

Are you using a TXT file with names?
Or are you explicitly listing the names on the Get-VM cmdlet?
Perhaps show what exactly you changed.


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

Reply
0 Kudos
kbhowmick123
Enthusiast
Enthusiast

Hi Lucd,

This scripts works as expected, The problem persisted in the scripts that was shared earlier. Thank you.

 

Tags (1)
Reply
0 Kudos