VMware Cloud Community
kakekmuda
Enthusiast
Enthusiast
Jump to solution

How to filter vm by datacenter name

[ASK]

How to grab vm by datacenter name and grab status vm on/off  ?

i just want to grab KP2 Virtual Data Center/ another data center

pastedImage_0.png

this is my code please help, i was try to change/add code but its still error..

&{foreach($vm in (Get-VM)) {

    $vm.ExtensionData.Guest.Net | select -Property @{N='VM';E={$vm.Name.split('(')[0].TrimEnd(' ')}},

   @{N='PIC Aplikasi';E={$vm.Name.split('()')[1]}},

    @{N='OS';E={$vm.Guest.OSFullName}},

@{N="NumCPU";E={$vm.NumCPU}},

@{N="MemoryMB";E={$vm.MemoryMB}},

    @{N='IP';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}},

    @{N='Gateway';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},

    @{N='Subnet Mask';E={

                $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

                $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {

                        $Remainder = $dec % [Math]::Pow(256, $i)

                        (                        $dec - $Remainder) / [Math]::Pow(256, $i)

                        $dec = $Remainder

                    } )

                [String]::Join('.', $DottedIP)

            }},

        @{N='MAC';E={[string]::Join(',',$_.MacAddress)}}

  }

} | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv

Thank You So Much

Tomi Irawan

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean like this?

$dcName = 'KP2 Virtual Data Center'

& { foreach ($vm in (Get-Datacenter -Name $dcName | Get-VM))

   {

   $vm.ExtensionData.Guest.Net | select -Property @{N = 'VM'; E = { $vm.Name.split('(')[0].TrimEnd(' ') } },

   @{N = 'State'; E = { $vm.PowerState } },

   @{N = 'PIC Aplikasi'; E = { $vm.Name.split('()')[1] } },

   @{N = 'OS'; E = { $vm.Guest.OSFullName } },

   @{N = "NumCPU"; E = { $vm.NumCPU } },

   @{N = "MemoryMB"; E = { $vm.MemoryMB } },

   @{N = 'IP'; E = { [string]::Join(',', ($vm.Guest.IPAddress | Where { ($_.Split(".")).length -eq 4 })) } },

   @{N = 'Gateway'; E = { [string]::Join(',', ($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | % { if ($_.Gateway.IpAddress) { $_.Gateway.IpAddress } })) } },

   @{N = 'Subnet Mask'; E = {

   $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

   $DottedIP = $( For ($i = 3; $i -gt -1; $i--)

   {

   $Remainder = $dec % [Math]::Pow(256, $i)

   (   $dec - $Remainder) / [Math]::Pow(256, $i)

   $dec = $Remainder

   } )

   [String]::Join('.', $DottedIP)

   }

   },

   @{N = 'MAC'; E = { [string]::Join(',', $_.MacAddress) } }

   }

} | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv


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

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

You mean like this?

$dcName = 'KP2 Virtual Data Center'

& { foreach ($vm in (Get-Datacenter -Name $dcName | Get-VM))

   {

   $vm.ExtensionData.Guest.Net | select -Property @{N = 'VM'; E = { $vm.Name.split('(')[0].TrimEnd(' ') } },

   @{N = 'State'; E = { $vm.PowerState } },

   @{N = 'PIC Aplikasi'; E = { $vm.Name.split('()')[1] } },

   @{N = 'OS'; E = { $vm.Guest.OSFullName } },

   @{N = "NumCPU"; E = { $vm.NumCPU } },

   @{N = "MemoryMB"; E = { $vm.MemoryMB } },

   @{N = 'IP'; E = { [string]::Join(',', ($vm.Guest.IPAddress | Where { ($_.Split(".")).length -eq 4 })) } },

   @{N = 'Gateway'; E = { [string]::Join(',', ($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | % { if ($_.Gateway.IpAddress) { $_.Gateway.IpAddress } })) } },

   @{N = 'Subnet Mask'; E = {

   $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

   $DottedIP = $( For ($i = 3; $i -gt -1; $i--)

   {

   $Remainder = $dec % [Math]::Pow(256, $i)

   (   $dec - $Remainder) / [Math]::Pow(256, $i)

   $dec = $Remainder

   } )

   [String]::Join('.', $DottedIP)

   }

   },

   @{N = 'MAC'; E = { [string]::Join(',', $_.MacAddress) } }

   }

} | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv


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

kakekmuda
Enthusiast
Enthusiast
Jump to solution

Dear LuCD

Thank You for your fast respond. i wil try it first thank you so much

0 Kudos
kakekmuda
Enthusiast
Enthusiast
Jump to solution

Dear LucD

Please help me how to split this table,

pastedImage_0.png

sample i have vm who have a lot of ip, ip1,ip2 and gateway 1,gateway2.etc , can i split this ?

thank you so much

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The question you will have to answer first is, how do you want to show this in your output?
If an array has rows with different numbers of properties, there is a problem with for example Export-Csv.
To guarantee the same number of properties/columns, you could for example create columns like IP1, IP2, IP3, IP4.
But that would mean that several rows have no values in some columns.
You decide.


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

0 Kudos
kakekmuda
Enthusiast
Enthusiast
Jump to solution

Dear LucD

Thank you for your fast resond .

look like this is no problem because there is some vm who have a more IP & Gateway and some server have the only one IP & Gateway.

Header 1Header 2Header 3Header 4
vm AIP1IP2
vm BIP1
vm CIP1IP2

IP3

Thank You For your help.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you are trying to say here.
But the point I'm trying to make is demonstrated by this example

$t1 = @(

   "" | Select @{N='F1';E={'abc'}}

   "" | Select @{N='F1';E={'abc'}},@{N='F2';E={'xyz'}}

)

$t2 = @(

   "" | Select @{N='F1';E={'abc'}},@{N='F2';E={'xyz'}}

   "" | Select @{N='F1';E={'abc'}}

)


$t1 | Export-Csv -Path .\report1.csv -NoTypeInformation -UseCulture

Invoke-Item -Path .\report1.csv

$t2 | Export-Csv -Path .\report2.csv -NoTypeInformation -UseCulture

Invoke-Item -Path .\report2.csv


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

0 Kudos
kakekmuda
Enthusiast
Enthusiast
Jump to solution

Dear LucD

thank you for the answer but, i want the text between in a separate column in one report.?

pastedImage_0.png

Thank You Mr. LucD

0 Kudos
kakekmuda
Enthusiast
Enthusiast
Jump to solution

up

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You didn't really answer my question.
For Export-Csv all the rows should have the same number of properties.
How do you intend to display that?
Would you go for a fixed number of (potentially empty) columns for IP, Gateway and netmask?


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

0 Kudos
kakekmuda
Enthusiast
Enthusiast
Jump to solution

Dear Mr. LucD

Sorry for that,

it doesn't matter for a few empty columns because it doesn't mean the number of ip and gateway in one vm.

can separate columns according to the number of texts ?

Thank You So Much

0 Kudos