VMware Cloud Community
CG21
Enthusiast
Enthusiast
Jump to solution

Powercli scrip to export data in csv file from vcenter

Hi,

i would like, since the vcenter, to export in csv file the following data :  platform, host, vm name, started yes / no, Operating system, category Tag 1, category Tag2

I need help

please,

thank you

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean by 'platform'.

Also not sure what you want to be listed under 'category Tag 1' and 'category Tag 2'?

I used the names of the tags in that category that are assigned to the VM.

This should get you already some of the required info

$report = foreach($esx in Get-VMHost){

    Get-VM -Location $esx |

    Select @{N='VMHost';E={$esx.Name}},Name,PowerState,

        @{N='GuestOS';E={$_.Guest.OSFullName}},

        @{N='CatTag1';E={(Get-TagAssignment -Entity $_ -Category (Get-TagCategory -Name Tag1)).Tag.Name -join '|'}},

        @{N='CatTag2';E={(Get-TagAssignment -Entity $_ -Category (Get-TagCategory -Name Tag2)).Tag.Name -join '|'}}

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

10 Replies
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean by 'platform'.

Also not sure what you want to be listed under 'category Tag 1' and 'category Tag 2'?

I used the names of the tags in that category that are assigned to the VM.

This should get you already some of the required info

$report = foreach($esx in Get-VMHost){

    Get-VM -Location $esx |

    Select @{N='VMHost';E={$esx.Name}},Name,PowerState,

        @{N='GuestOS';E={$_.Guest.OSFullName}},

        @{N='CatTag1';E={(Get-TagAssignment -Entity $_ -Category (Get-TagCategory -Name Tag1)).Tag.Name -join '|'}},

        @{N='CatTag2';E={(Get-TagAssignment -Entity $_ -Category (Get-TagCategory -Name Tag2)).Tag.Name -join '|'}}

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

CG21
Enthusiast
Enthusiast
Jump to solution

"Not sure what you mean by 'platform'."
--> This is actually the cluster

"Also not sure what you want to see under 'category Tag 1' and 'category Tag 2'?
I used the names of the tags in that category assigned to the VM."
--> I have several tags assigned to the vms, I would like each tag value of each category.
example: category Tag 1 with tag1, tag2, tag3

with this script I have an error that I attach

thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have a varying number of Tags assigned to each VMN, then that could be an issue for the Export-Csv.

Does each VM have the same number of Tag assignments?

Do you want the Category and Tag in that column? Like Cat1/Tag1?

I suspect something went wrong in your copy/paste of the code.

Can you attach the .ps1 file you are using?


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

0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

in fact I would like a csv like this :

Cluster |  Host   | vm name | Powerstate | OS      | Tag Category 1             | Tag Category 2

DMZ    |  esx01 | vm1         |  running      | 2008    | tag name in category1 | tag name in category2

PROD  |  esx02 | vm2         |  running      | centos | tag name in category1 | tag name in category2

0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

Does each VM have the same number of Tag assignments?
No

Do you want the Category and Tag in that column? Like Cat1/Tag1?
No just the tag

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, is there a maximum number of Tags a VM can have assigned?

It will make the script a lot simpler if we know in advance how many Tag columns there will be.

For example, if the max number is 3, we can leave columns blank, but at least each row in the CSV would have the same number of columns.

The error is due to the line you changed (CatTag2), it has a comma at the end that shouldn't be there.


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

0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

"Ok, is there a maximum number of Tags to VM can have assigned?"
I have 6 category, we will have maximum 6 columns

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = foreach($cluster in Get-Cluster){

    foreach($esx in Get-VMHost -Location $cluster){

        foreach($vm in Get-VM -Location $esx){

            $obj = New-Object PSObject -Property @{

                Cluster = $cluster.Name

                VMHost = $esx.Name

                VM = $vm.Name

                PowerState = $vm.PowerState

                GuestOS = $vm.Guest.OSFullName

                TagCat1 = ''

                TagCat2 = ''

                TagCat3 = ''

                TagCat4 = ''

                TagCat5 = ''

                TagCat6 = ''

            }

            if($cat = Get-TagCategory -Name "Cat1" -ErrorAction SilentlyContinue){

                $obj.TagCat1 = (Get-TagAssignment -Entity $vm -Category $cat).Tag.Name -join '|'

            }

            if($cat = Get-TagCategory -Name "Cat2" -ErrorAction SilentlyContinue){

                $obj.TagCat2 = (Get-TagAssignment -Entity $vm -Category $cat).Tag.Name -join '|'

            }

            if($cat = Get-TagCategory -Name "Cat3" -ErrorAction SilentlyContinue){

                $obj.TagCat3 = (Get-TagAssignment -Entity $vm -Category $cat).Tag.Name -join '|'

            }

            if($cat = Get-TagCategory -Name "Cat4" -ErrorAction SilentlyContinue){

                $obj.TagCat4 = (Get-TagAssignment -Entity $vm -Category $cat).Tag.Name -join '|'

            }

            if($cat = Get-TagCategory -Name "Cat5" -ErrorAction SilentlyContinue){

                $obj.TagCat5 = (Get-TagAssignment -Entity $vm -Category $cat).Tag.Name -join '|'

            }

            if($cat = Get-TagCategory -Name "Cat6" -ErrorAction SilentlyContinue){

                $obj.TagCat6 = (Get-TagAssignment -Entity $vm -Category $cat).Tag.Name -join '|'

            }

            $obj

        }

    }

}

$report |

Select Cluster,VMHost,VM,PowerState,GuestOS,TagCat1,TagCat2,TagCat3,TagCat4,TagCat5,TagCat6 |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

i test with this script

so it's ok, and i don't test the another script

thank you

0 Kudos
timrsa
Contributor
Contributor
Jump to solution

Saved me a lot of time, thanks!

0 Kudos