VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot

Script to get VMs name, host and IP filtered

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

14 Replies
LucD
Leadership
Leadership

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

antoniogemelli
Hot Shot
Hot Shot

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..

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Records have a format like it:

webfront-LB-10-30-49-1

webfront-CM-10-20-49-5

webfront-CM-10-30-49-2

Reply
0 Kudos
LucD
Leadership
Leadership

And no error message?

Any blank lines in that file?


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

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

Reply
0 Kudos
LucD
Leadership
Leadership

Looks like $vmNames is empty. Can you check?

What is Get-Content -Path ... showing?


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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

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

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Yes, corresponding (68 VMs)

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

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.

Reply
0 Kudos
antoniogemelli
Hot Shot
Hot Shot

I can see now in some VMware Tool is not proper configured.

Reply
0 Kudos
LucD
Leadership
Leadership

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

Reply
0 Kudos