1 2 Previous Next 18 Replies Latest reply on Feb 16, 2020 12:31 PM by LucD

    Скрипт powerCLI

    user_pc Novice

      Добрый день, есть задача:

       

      Необходимо выгрузить данные по всем VM, которые есть в VMware. Установил PowerCLI

      Нужна таблица в виде: 

       

      on english:

      Good day, there is a task:

       

       

      You need to upload data for all VMS that are available in VMware. Installed PowerCLI

      Need a table in the form:

      vmname-ttl (lifetime) - project-project owner-admin-real hdd volume - maximum hdd volume.

       

       

      vmname - ttl (время жизни) - проект - владелец проекта - админ - объем hdd реальный - объем hdd максимальный.

       

      Нашел похожий скрипт:

       

      PowerShellВыделить код
      1 
      get-VM | select Name, ProvisionedSpaceGB, UsedSpaceGB | Export-Csv -Path C:\provision.csv

       

      Но вывод в файлик, не совсем понятного вида.

       

      PowerShellВыделить код
      1 
      fd-c0r-as01-win-ult,"40,125372051261365413665771485","17,164434033446013927459716797"

       

      Если кто делал подобное, пожалуйста подскажите.

      На просторах нашел, что ttl не удастся вытащить. Ищу решение, чтобы данные были читабельны.      

       

       

      Есть такой:

      get-VM | select Name, @{ l="ProvisionedSpaceGB"; e={[int]$_.ProvisionedSpaceGB}}, @{l="UsedSpaceGB";e={[int]$_.UsedSpaceGB}} | Export-Csv -Path C:\provision.csv

        • 1. Re: Скрипт powerCLI
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          What do you mean by 'readable'?

          The CSV format is clear text and readable.
          The name of each column is shown in the 1st row.

           

          If TTL means how old a VM is, that is currently not really possible.
          You could look at the events (Get-VIEvent) to find out when a VM was created.

          But events are only kept for maximum 1 year.

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: Скрипт powerCLI
            user_pc Novice

            referring to the figures, for example, 40, not 40,125372051261365413665771485.

            I solved this problem with the script below. Now the display in Excel is normal.

            It turns out that you need to correct the script so that it pulls out the data that is on the screenshot.  Not all, but what are possiblettl.jpg

            • 3. Re: Скрипт powerCLI
              user_pc Novice

              it turns out that ttl is the period until which the VM is valid

              • 4. Re: Скрипт powerCLI
                LucD Guru
                User ModeratorsvExpertCommunity Warriors

                That is a Custom Attribute.

                You can retrieve the Value with something like this

                 

                 

                $ttlAttr = Get-CustomAttribute -Name TTL

                Get-VM |

                Select Name,

                    @{N='TTL';E={(Get-Annotation -CustomAttribute $ttlAttr -Entity $_).Value}}

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                1 person found this helpful
                • 5. Re: Скрипт powerCLI
                  user_pc Novice

                  Thank you very much, it works!

                  I will try to bring to mind that all parameters were:

                   

                   

                  $ttlAttr = Get-CustomAttribute -Name TTL -Admin -Owner -ProjecrName -ProvisionedSpaceGB -UsedSpaceGB | Export-Csv -Path C:\provision.csv

                   

                   

                  Get-VM |

                   

                   

                  Select Name,

                   

                   

                      @{N='TTL';E={(Get-Annotation -CustomAttribute $ttlAttr -Entity $_).Value}}, @{N='Admin';E={(Get-Annotation -CustomAttribute $AdminAttr -Entity $_).Value}}

                  • 6. Re: Скрипт powerCLI
                    LucD Guru
                    User ModeratorsvExpertCommunity Warriors

                    You will have to get those one by one.

                    Something like this

                     

                    $ttlAttr = Get-CustomAttribute -Name TTL

                    $adminAttr = Get-CustomAttribute -Name Admin

                    $ownerAttr = Get-CustomAttribute -Name Owner

                    $projectNameAttr = Get-CustomAttribute -Name ProjectName


                    Get-VM |

                    Select Name,

                        @{N='TTL';E={(Get-Annotation -CustomAttribute $ttlAttr -Entity $_).Value}},

                        @{N='Admin';E={(Get-Annotation -CustomAttribute $adminAttr -Entity $_).Value}},

                        @{N='Owner';E={(Get-Annotation -CustomAttribute $ownerAttr -Entity $_).Value}},

                        @{N='ProjectName';E={(Get-Annotation -CustomAttribute $projectNameAttr -Entity $_).Value}}

                      

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    1 person found this helpful
                    • 7. Re: Скрипт powerCLI
                      user_pc Novice

                      hank you very much for your help! I will test

                      • 8. Re: Скрипт powerCLI
                        user_pc Novice

                        If you have time please take a look

                         

                         

                         

                        $ttlAttr = Get-CustomAttribute -Name TTL

                        $adminAttr = Get-CustomAttribute -Name Admin

                         

                         

                        $ownerAttr = Get-CustomAttribute -Name Owner

                         

                         

                        $projectNameAttr = Get-CustomAttribute -Name ProjectName

                         

                         

                         

                         

                         

                         

                        Get-VM |

                         

                         

                        Select Name,

                         

                         

                            @{N='TTL';E={(Get-Annotation -CustomAttribute $ttlAttr -Entity $_).Value}},

                         

                         

                            @{N='Admin';E={(Get-Annotation -CustomAttribute $adminAttr -Entity $_).Value}},

                         

                         

                            @{N='Owner';E={(Get-Annotation -CustomAttribute $ownerAttr -Entity $_).Value}},

                         

                         

                            @{N='ProjectName';E={(Get-Annotation -CustomAttribute $projectNameAttr -Entity $_).Value}},

                         

                            @{ l="ProvisionedSpaceGB"; e={[int]$_.ProvisionedSpaceGB}}, @{l="UsedSpaceGB";e={[int]$_.UsedSpaceGB}} | Export-Csv -Path C:\provision.csv

                         

                         

                         

                         

                        issues on scrin
                        how to make text instead of question marks

                        • 9. Re: Скрипт powerCLI
                          LucD Guru
                          vExpertCommunity WarriorsUser Moderators

                          That is most probably due to the fact that the encoding is set to ASCII, which changes special characters to question marks.

                          Can you try setting the default encoding to UTF8?

                           

                          [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          1 person found this helpful
                          • 10. Re: Скрипт powerCLI
                            user_pc Novice

                            Understood, I'll look for. Version of Excel 2013. Perhaps in later versions it is possible to set the default.

                            I tried to open this "provision" file in notepad, then convert it to UTF-8 and save it. Then opened in Excel, displayed also-question marks

                            • 11. Re: Скрипт powerCLI
                              LucD Guru
                              User ModeratorsvExpertCommunity Warriors

                              When it is saved as a CSV its probably too late.
                              Excel only sees question marks.

                              You will have to do it in the PowerShell session where you create the CSV.

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              1 person found this helpful
                              • 12. Re: Скрипт powerCLI
                                user_pc Novice

                                Thanks!  Got it !

                                That is, the command

                                 

                                 

                                [Console]:: OutputEncoding = [Text.UTF8Encoding]:: UTF8

                                 

                                 

                                insert into the script ? or before the script?

                                I Use Vmware PowerCLI 6.5

                                • 13. Re: Скрипт powerCLI
                                  LucD Guru
                                  vExpertCommunity WarriorsUser Moderators

                                  Both would work, just make sure it is executed before you run the Get-VM and the Select from the script.

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  1 person found this helpful
                                  • 14. Re: Скрипт powerCLI
                                    user_pc Novice

                                    Class. Thank you for your help !

                                    1 2 Previous Next