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
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
Again Thanks for your kindly help @LucD!
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
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
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
LucD Thanks for the help! Script is working and i was able to fetch OS information of VM.
Thanks,
Shalomon.N
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
One way is like this
Select Name,@{N='vCenter';E={([system.uri]$_.ExtensionData.Client.ServiceUrl).Host}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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}}
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
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.
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.
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
Hi Lucd,
This scripts works as expected, The problem persisted in the scripts that was shared earlier. Thank you.