Hello,
I would like to run a script (daily) to get vm and host,
Something base of this:
Get-VM | Select Name,VMHost | Export-Csv -Path C:\Users\mydiir\Desktop\vm_hostlists.csv -NoTypeInformation -UseCulture
I would like to add Ip addresses and to filter for specific server list (some machines name).
Thanks
You could do something like this.
The vmnames.txt file contains the displaynames of the VMs you want to include.
For the IP address you need to have the VMware Tools installed.
$vmNames = Get-Content -Path vmnames.txt
Get-VM -Name $vmNames | Select Name,VMHost,@{N='IP';E={$_.Guest.IPAddress[0]}} |
Export-Csv -Path C:\Users\mydiir\Desktop\vm_hostlists.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for tips,
I used:
Get-VM | Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress[0])}} |
Export-Csv -NoTypeInformation C:\Users\xxgemela\Desktop\machine_ip.csv
This works, but if I try to add $vmNames = Get-Content -Path vmnames.txt
$vmNames = Get-Content -Path C:\Users\xxgemela\Desktop\vmnames.txt
Get-VM -Name $vmNames | Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress[0])}} |
Export-Csv -Path C:\Users\xxgemela\Desktop\machine_ip.csv -NoTypeInformation -UseCulture
Doesn't work..
Can you show us a (sample) content of the vmnames.txt file?
And do you get an error message? Can you share it?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Records have a format like it:
webfront-LB-10-30-49-1
webfront-CM-10-20-49-5
webfront-CM-10-30-49-2
And no error message?
Any blank lines in that file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Error message:
Get-VM : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is not null
or empty and then try the command again.
At line:4 char:14
+ Get-VM -Name $vmNames | Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAdd ...
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
No Blank line or space in txt file
Looks like $vmNames is empty. Can you check?
What is Get-Content -Path ... showing?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It's strange, file is filled and if I run:
Get-Content -Path C:\Users\xxgemela\Desktop\vmname.txt
there is list of server in output
Ok, next step.
After you do
$vmNames = Get-Content -Path C:\Users\xxgemela\Desktop\vmname.txt
do a
$vmNames.Count
Does that number correspond with the number of names in the .txt file?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, corresponding (68 VMs)
Ok, now do
Get-VM -Name $vmNames.
Does this show the VMs from the .txt files?
And does
Get-VM -Name $vmNames.| Measure-Object -Sum
show the same number?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Your script work,
I made a new test with a new file and I put only few VMs to see if work,
#filter by server
$vmNames = Get-Content -Path C:\Users\xxgemela\Desktop\vmname.txt
Get-VM -Name $vmNames | Select Name,VMHost, @{N="IP Address";E={@($_.guest.IPAddress[0])}} |
Export-Csv -Path C:\Users\xxgemela\Desktop\machine_ip.csv -NoTypeInformation -UseCulture
Most probably issue is in some VMs Name, I will get new list and make comparison, because some old machine have a different naming convention.
I will update you, but for now Thank you very much for your patience and support.
I can see now in some VMware Tool is not proper configured.
You could add the parameter -ErrorAction SilentlyContinue on the Get-VM cmdlet, that way the script will continue after a faulty VM.
Or capsulate the Get-VM in a Try-Catch construct.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference