Hi,
I'm having trouble putting together a script that essentially seems simple enough but has me stupmed as I need to get the data from multiple locations.
I need to gather all VM's and their network config. I have seen a lot of similar scripts but none seem to aligh the IP and Mac per network card and include the vlan and device type for the network card. Also none seem to deal with vm's that have multiple nics.
I need this for an internal CMDB. They have requested:
VM Name
UUID
Network Name
Adapter Type
Mac Address
IP address
Output needs to be one line per vm in a csv format
Guest UUID Vlan0 IPAddress0 Mac0 AdapterType0 Vlan1 IPAddress1 Mac1 AdapterType1 Vlan2 IPAddress2 Mac2 AdapterType2
Any help appreciated.
Yeah.. it's my bad here i had bad example on my test env, will find vm with ipv6 address as well and will write updated version shortly.
ok here it is :
$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
$reportedvm = New-Object PSObject
Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}
$reportedvms|Export-Csv c:\myreport.csv
to get you started here is the UUID
Get-VM <vm_name> | %{(Get-View $_.Id).config.uuid}
Hi there,
i think i did not understand all requirements, or they were not described correctly but what i understood is:
data needed: uuid, vm name,network name, mac, ip
what do you mean by adapter type , do want to have info about e1000,vmxnet etc ??
--
$vms=get-view -viewtype virtualmachine -filter @{"name"="myvm1"}
foreach($vm in $vms){
$networkcards=$vm.guest.net
""|select @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net info";e={[string]::join(',', $($networkcards|%{[string]::join(',',$($_.network,$_.ipaddress,$_.Macaddress))}) )}}
}
--
try this first out for 1 vm where : "myvm1" should be change to some existing vm in your infra. If all is good , run this
$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
$networkcards=$vm.guest.net
""|select @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net info";e={[string]::join(',', $($networkcards|%{[string]::join(',',$($_.network,$_.ipaddress,$_.Macaddress))}) )}}
}
Greg
If you want that type card e1000,vmxnet etc.. you can try it like this
$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
$networkcards=$vm.guest.net
""|select @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net info";e={[string]::join(',', $($networkcards|%{$devid=$_.DeviceConfigId;[string]::join(',',$(($vm.config.hardware.device|?{$_.key -eq $devid}).gettype().name,$_.network,$_.ipaddress,$_.Macaddress))}) )}}
}
i forgot about the csv at the end so:
$(
foreach($vm in $vms){
$networkcards=$vm.guest.net
""|select @{n="VM name";e={$vm.name}},@{n="uuid";e={$vm.config.uuid}},@{n="net info";e={[string]::join(',', $($networkcards|%{$devid=$_.DeviceConfigId;[string]::join(',',$(($vm.config.hardware.device|?{$_.key -eq $devid}).gettype().name,$_.network,$_.ipaddress,$_.Macaddress))}) )}}
}
)|export-csv c:\rep321.csv
Thanks for the replys so far:
This script I found online (All credit to : http://www.blkmtn.org/Get_VMWare_guest_Network_and_IP_Address) is a good start.
To get the IP address's associated with each nic adapter I have added in :
Without this I was only getting the first IP reported against ever Nic.
I also need to get the Nic type like VMXnet3 or e1000 but don't know how to.
It's a work in progress and when finished it will should be useful to others.
Adapter Type = device type of the network card - e1000, vmxnet
Hi Grzesiekk
Thanks for the scripts they are very good and the last one where the output to csv is 90% of what I need.
The output needs to be broken up into individual cells in excel(csv) in the same way you have VM name and uuid.
Output needs to be one line per vm and needs to deal with vm's that have multiple Network cards in a csv format
VM Name, UUID, NetworkAdapterType0, Vlan0, IPAddress0, Mac0, NetworkAdapterType1, Vlan1, IPAddress1, Mac1, NetworkAdapterType2, Vlan2, IPAddress2, Mac2
Thanks for you help, really appreciated.
Hi
i guess that this was the most confusing in what you were trying to achieve. Some people wants exactly to have 1 column with all this info , so i thought you wanted the same. In this case it makes things even easier
$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
$reportedvm = New-Object PSObject
Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $ntwkcard.IpAddress
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}
$reportedvms|Export-Csv c:\myreport.csv
Let me know if this is 100% accurate
Hey, Thanks again for your help.
It's 99% there.
The IP address is outputting as System.String[] for all vm's.
So I changed the following to $($ntwkcard.IpAddress).ToString(). This works for some vm's but not all.
The ones that do not output the IP are listed as System.Object[]
Also some of my Network names are being output as : 62 f7 03 50 29 ff 5d e0-4c 38 90 b4 fb 64 bd 09
Thanks,
Niall.
Yeah.. it's my bad here i had bad example on my test env, will find vm with ipv6 address as well and will write updated version shortly.
ok here it is :
$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine
foreach($vm in $vms){
$reportedvm = New-Object PSObject
Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Network" -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.MacAddress" -Value $ntwkcard.Macaddress
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}
$reportedvms|Export-Csv c:\myreport.csv
Hi Grzesiekk,
Just saw the following :
Really should share with the forum if using the forum for motivation to write the script.
However I have tested it and it is working but one change I made was to put the following in quotes:
$($ntwkcard.IpAddress|?{$_ -like "*.*"})
without the quotes some of the output is like this if there are multiple address per adapter:
System.Object[]
Thanks for all your help. Here is final script:
I am glad that this worked for you.
One thing is that i did put the " "
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkcard${i}.IpAddress" -Value $($ntwkcard.IpAddress|?{$_ -like "*.*"})
it has the quoatation marks "*.*" i am bit confused now if that was something wrong while you have been copying text to your clipboard or did i make some mistake
Before i pasted to you i was testing source code It had to work, i guess
VM
Thanks for the update on this script.
When I run it I do get an error.
You cannot call a method on a null-valued expression.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\custom\Virtual
MachineNetworking.ps1:16 char:180
+ Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name "networkca
rd${i}.Device" -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.De
viceConfigId)}).gettype <<<< ().name)
+ CategoryInfo : InvalidOperation: (gettype:String) [], RuntimeEx
ception
+ FullyQualifiedErrorId : InvokeMethodOnNull
This reoccurs several time.
The output is created, however, I would love to know what is going on with this error.
After reviewing my output, it looks like if networkcard0 was not on the Virtual machine it causes a problem with the script.
Not sure if there is a way to address this.
Hi,
Can you please add VLAN id and Port Group in same script and let me know if this is feasible to have it ?
Regards,
Pradeep
Yes please, that'd be greatly appreciated.
Hi,
Can you please provide script which can generate file in below format ?
VM1, UUID, VNIC TYPE1, IP Address, MAC Address, Port Group, VLANid
VM1, UUID, VNIC TYPE2, IP Address, MAC Address, Port Group, VLANid
VM1, UUID, VNIC TYPE3, IP Address, MAC Address, Port Group, VLANid
Regards,
Pradeep