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
Could it be that you have a blank line at the end of the VMS.txt file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Example:
VMS.txt
xxxx-vm1
yyyy-vm2
zzzz-vm3
VCenterlist.txt
VC1
VC1
******************** could you guide me on how to fix that blank line issue????
Open the .txt file in Notepad, go to the end and <Backspace> till your cursor is just behind the last VMname in the file.
Save the file, then try the script again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for your kindly help, It did work and my issue is fixed.
I need to write a "if else" condition, Incase if the mentioned VM's are not found in Vcenter seerver. Because it throws error for VM's not found.
Can you help me out with it?
$outputFile = "C:\Temp\FetchVmInfo\Vminfo.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 ..."
Foreach($vm in $VMS)
{
$out = Get-VM -Name $VMS | Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB
"VM information has been fetched"
}
$out | Export-CSV $outputFile
"File has been exported"
Can you help me to modify this script ?
If you just want to ignore the VMs that are not there, you can use the ErrorAction parameter.
$outputFile = "C:\Temp\FetchVmInfo\Vminfo.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 = @()
Foreach($vm in $VMS)
{
$report += Get-VM -Name $VMS -ErrorAction SilentlyContinue |
Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB
"VM $($vm) information has been fetched"
}
$report | Export-CSV $outputFile
"File has been exported"
If you want to get a message oon screen when a VM is not found, you can use a Try-Catch construct.
$outputFile = "C:\Temp\FetchVmInfo\Vminfo.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 = @()
Foreach($vm in $VMS)
{
Try{
$report += Get-VM -Name $VMS -ErrorAction Stop |
Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB
"VM $($vm) information has been fetched"
}
Catch{
"VM $($vm) not found"
}
}
$report | Export-CSV $outputFile
"File has been exported"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd, once again thanks for your kindly help
First script with error action parameter is working fine. But it also shows the VM as found, which is not found in Vcenter server
*Output has been removed for information sensitivity & security purposes.*
Any suggestion over this?
Try this updated version
$outputFile = "C:\Temp\FetchVmInfo\Vminfo.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 = @()
Foreach($vm in $VMS)
{
$row = Get-VM -Name $VMS -ErrorAction SilentlyContinue |
Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB
if($row){
$report += $row
"VM $($vm) information has been fetched"
}
}
$report | Export-CSV $outputFile
"File has been exported"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
I tried with the update script, But still i get the VM information has fetched for VM which is not in the Vcenter server.
*Output has been removed for information sensitivity & security purposes.*
Inaddition to it. I get the output file with VM displaying multiple times.
*Output has been removed for information sensitivity & security purposes.*
Can you try the following.
Does this return the same VM multiple times?
$VMS = Get-Content C:\Temp\FetchVmInfo\VMS.txt
$VCenterlist = Get-Content C:\Temp\FetchVmInfo\VCenterlist.txt
Connect-VIServer $VCenterlist
Get-VM $vms | select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
It does not give the VM information multiple times.
Please find the output below.
*Output has been removed for information sensitivity & security purposes.*
Ok, yet another variation.
Does that make a difference?
$outputFile = "C:\Temp\FetchVmInfo\Vminfo.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 = @()
Foreach($vmName in $VMS)
{
$vm = Get-VM -Name $vmName -ErrorAction SilentlyContinue
if($vm){
$report += ($vm | Select-Object Name, Powerstate, NumCPU, MemoryMB, MemoryGB)
"VM $($vmName) information has been fetched"
}
}
$report | Export-CSV $outputFile
"File has been exported"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried the latest script, One good thing is that it is not showing up the VM which is not in the Vcenter, Please find the output below
*Output has been removed for information sensitivity & security purposes.*
Again in the CSV File, I get the VM information multiple time
*Output has been removed for information sensitivity & security purposes.*
I updated the last version, the duplicates should be gone now.
Can you try again?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks so much LucD Script seems to be working!
Hi @LucD
Sorry to bother again, I just have a small query!
Is it possible to adjust the above script in a way, Where we can get the VM in a text file which is not found in Vcenter server?
Try something 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)
"VM $($vmName) information has been fetched"
}
else{
$reportNotFound += $vmName
}
}
$report | Export-CSV $outputFile
$reportNotFound | Export-Csv $notFoundFile
"File has been exported"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
HI LucD
Please find below the Vm-notfound.csv file, It does not the give the VM Name which is not found in Vcenter server
PSPath | PSParentPath | PSChildName | PSDrive | PSProvider | ReadCount | Length |
C:\Temp\FetchVmInfo\VMS.txt | C:\Temp\FetchVmInfo | VMS.txt | C | Microsoft.PowerShell.Core\FileSystem | 1 | 14 |
Not sure what that file info is trying to show me.
Are you sure there is nothing the variable $reportNotFound?
Try changing this line
$reportNotFound | Export-Csv $notFoundFile
into
$reportNotFound
This will display the content of the variable on screen
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tied this line and this seems to be working.
$reportNotFound | Out-file -append "C:\Temp\FetchVmInfo\VMerror.txt"