VMware Cloud Community
kakekmuda
Enthusiast
Enthusiast

Split VM NAME in csv report use powercli

any body can help me ?

split.PNG

the real condition

the real kondision.PNG

like this

pastedImage_7.png

vmname (xyz) , can i split this ?

i use this code

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

    $vm.ExtensionData.Guest.Net | select -Property @{N='VM';E={$vm.Name}},

    @{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="DNS";E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))}},

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

  }

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

Thank you all

11 Replies
LucD
Leadership
Leadership

Try replacing that line with

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


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

kakekmuda
Enthusiast
Enthusiast

Dear LucD, thank you sir its work,

pastedImage_0.png

please help me more about pic name ? ANH .PAS etc

split.PNG

Thank You so much

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure what you mean.
Do you want the text between parenthesis in a separate column?


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

kakekmuda
Enthusiast
Enthusiast

yes yes yes,

how  great you are sir, thx u

Reply
0 Kudos
LucD
Leadership
Leadership

Replace that one line with these 2 lines.

You can change the name of the additional column (here 'VMInfo') to whatever you want.

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

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


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

kakekmuda
Enthusiast
Enthusiast

Dear Mr LucD

How are you? I hope you are well.

i have problem with this case,

example :

Header 1Header 2Header 3
Before abcxyz
Afterabc(xyz)xyz

im still get error after i try to edit this code i want to in my table like after.

this is the code

$dcName = 'XYZ'

$vms = Get-Datacenter -Name $dcName | Get-VM | where { $_.Name -notmatch "test|clone" }

$maxIP = 2

$maxGateway = 2

$maxMask = 2

$vms | ForEach-Object -Process {

    $vm = $_

    $vm.ExtensionData.Guest.Net |

Sort-Object -Unique |

       

ForEach-Object -Process {

            $obj = [ordered]@{

                Hostname        = $vm.Name.split('(')[0].TrimEnd(' ')

                State          = $vm.PowerState

                'PIC Aplikasi' = $vm.Name.split('()')[1]

                OS              = $vm.Guest.OSFullName

                CoreCPU        = $vm.NumCPU

                MemoryGB        = $vm.MemoryGB

StatusVM = "Production"

            }

            1..$maxIP | % { $obj.Add("IP$($_)", '') }

            1..$maxGateway | % { $obj.Add("GW$($_)", '') }

            1..$maxMask | % { $obj.Add("Mask$($_)", '') }

            $i = 1

            $vm.Extensiondata.Guest.Net.IPAddress | Where { $_ -and ($_.Split(".")).length -eq 4 -and $i -le $maxIP } |

            Sort-Object -Unique |

            ForEach-Object -Process {

                $obj.Item("IP$($i)") = $_

                $i++

            }

        $i = 1

        $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress | Where { $_ -and ($_.Split(".")).length -eq 4 -and $i -le $maxGateway } |

        Sort-Object -Unique |

        ForEach-Object -Process {

            $obj.Item("GW$($i)") = $_

            $i++

        }

    $j = 1

    $vm.ExtensionData.Guest.Net.IpConfig.IpAddress.PrefixLength | where { $_ -le 32 -and $j -le $maxMask } |

    Sort-Object -Unique |

    ForEach-Object -Process {

        $dec = [Convert]::ToUInt32($(('1' * $_).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

            })

        $obj.Item("Mask$($j)") = [String]::Join('.', $DottedIP)

        $j++

    }

New-Object PSObject -Property $obj

}

} | Export-Csv -NoTypeInformation D:\xyz.csv

thx u so much for you helping me to solve this problem

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure I'm following.

What is the actual name of that VM?

What error are you getting?


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

Reply
0 Kudos
kakekmuda
Enthusiast
Enthusiast

Dear Mr LucD

Thank you for your fast response.

if im running this code my report look like this

Header 1Header 2
AppOnexyz

and what i want see look like

Header 1Header 2
AppOne(xyz)xyz

Can you help me for this case ?

Thank You

Tomi Irawan

Reply
0 Kudos
LucD
Leadership
Leadership

Ok, try changing the line to

Hostname        = $vm.Name


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

Reply
0 Kudos
kakekmuda
Enthusiast
Enthusiast

Dear Mr LucD

Thank you for the answer but i mean like this

vm name
PIC
AppTest (xyz)xyz)

So AppTest (xyz) have another coloum to pic look like table Mr LucD.

if i try command you give me it will only show

AppTest (xyz) only ther is not another coloum.

Thank You

Tomi Irawan

Reply
0 Kudos
LucD
Leadership
Leadership

In the original script, you only replace this line

Hostname        = $vm.Name.split('(')[0].TrimEnd(' ')

by this line

Hostname        = $vm.Name

The rest of the script stays the same.


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

Reply
0 Kudos