Automation

 View Only
  • 1.  Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 08:40 AM

    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



  • 2.  RE: Powercli scrip to export data in csv file from vcenter
    Best Answer

    Posted Mar 05, 2018 08:51 AM

    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



  • 3.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:16 AM
      |   view attached

    "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



  • 4.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:24 AM

    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?



  • 5.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:32 AM
      |   view attached

    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

    Attachment(s)

    zip
    CMDBextract.ps1.zip   480 B 1 version


  • 6.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:38 AM

    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.



  • 7.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:42 AM

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



  • 8.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:59 AM

    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



  • 9.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 09:29 AM

    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



  • 10.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Feb 03, 2021 09:48 AM

    Saved me a lot of time, thanks!



  • 11.  RE: Powercli scrip to export data in csv file from vcenter

    Posted Mar 05, 2018 10:49 AM

    i test with this script

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

    thank you