{"id":1059,"date":"2022-08-15T18:17:00","date_gmt":"2022-08-15T18:17:00","guid":{"rendered":"https:\/\/labiol.xyz\/?p=1059"},"modified":"2022-12-31T19:14:51","modified_gmt":"2022-12-31T19:14:51","slug":"powercli-code-to-gather-ip-and-portgroup-for-vm","status":"publish","type":"post","link":"https:\/\/www.labiol.xyz\/index.php\/2022\/08\/15\/powercli-code-to-gather-ip-and-portgroup-for-vm\/","title":{"rendered":"PowerCLI code to gather IP and portGroup for VM"},"content":{"rendered":"\n<p>It is not an easy task to collect those two information together as it can be read from two different objects. It is always an issue (for me) to merge two value from two different object as VM can have several network cards and at the same time be connected to the same portGroup &#8211; or of course, to different ones. <\/p>\n\n\n\n<p>Below you can find the code that should work for VMs with VMtools installed and that have up to 3 network cards:<\/p>\n\n\n\n<p><mark style=\"background-color:#fcb900\" class=\"has-inline-color has-black-color\">The code can be used at your own risk.<\/mark><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>$vms = Get-Content .\\Desktop\\vm_list.txt<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>foreach ($vmm in $vms){<br>$vmm = get-vm $vmm<br>$vmobj = New-Object -TypeName psobject<br>$vmobj | Add-Member -MemberType NoteProperty -Name &#8220;name&#8221; -Value $vmm.name<br>$vmobj | Add-Member -MemberType NoteProperty -Name &#8220;ipaddress0&#8221; -Value $vmm.Guest.Nics[0].IPAddress[0]<br>$vmobj | add-member -MemberType NoteProperty -Name &#8220;pg0&#8221; -value ($vmm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[0].NetworkName }).name<br>if((($vmm).Guest.Nics).count -gt 1){<br>$vmobj | Add-Member -MemberType NoteProperty -Name &#8220;ipaddress1&#8221; -Value $vmm.Guest.Nics[1].IPAddress[0]<br>$vmobj | add-member -MemberType NoteProperty -Name &#8220;pg1&#8221; -value ($vmm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[0].NetworkName }).name }<br>if((($vmm).Guest.Nics).count -gt 2){<br>$vmobj | Add-Member -MemberType NoteProperty -Name &#8220;ipaddress2&#8221; -Value $vmm.Guest.Nics[2].IPAddress[0]<br>$vmobj | add-member -MemberType NoteProperty -Name &#8220;pg2&#8221; -value ($vmm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[2].NetworkName }).name}<br>$vmobj | select *<br>}<\/p>\n<\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>at the beginning lists of VM can be read (from Destkop\\vm_list.txt in that example). That list should contans VMs name, every value per row<\/li>\n\n\n\n<li>for each vm in that file we are creating object with the following values:<br>* IP address from <code>vm.guest.nics[0].ipaddress[0]<\/code><br>* portGroup (pg) from: <code>($vm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[2].NetworkName }).name <\/code><\/li>\n<\/ol>\n\n\n\n<p>When this code will not work (completely):<\/p>\n\n\n\n<p>In scenario when VM has more than one IP per network card it will take just first value.<\/p>\n\n\n\n<p>If you would like to have more comprehensive information related to the network information from your vCenter as usual I can recommend to use rvtools or liveoptics<\/p>\n\n\n\n<p>[1] rvtools (<a href=\"https:\/\/www.robware.net\/rvtools\/download\/\">https:\/\/www.robware.net\/rvtools\/download\/<\/a>)<\/p>\n\n\n\n<p>[2] live optics (<a href=\"http:\/\/liveoptics.com\" data-type=\"URL\" data-id=\"liveoptics.com\">liveoptics.com<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is not an easy task to collect those two information together as it can be read from two different objects. It is always an issue (for me) to merge two value from two different object as VM can have several network cards and at the same time be connected &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1059","post","type-post","status-publish","format-standard","hentry","category-vmware"],"_links":{"self":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=1059"}],"version-history":[{"count":3,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1059\/revisions"}],"predecessor-version":[{"id":1068,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/posts\/1059\/revisions\/1068"}],"wp:attachment":[{"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=1059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=1059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.labiol.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=1059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}