user_pc
Enthusiast
Enthusiast

Скрипт powerCLI

Jump to solution

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

Необходимо выгрузить данные по всем 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

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

18 Replies
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

user_pc
Enthusiast
Enthusiast

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

0 Kudos
user_pc
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

user_pc
Enthusiast
Enthusiast

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}}

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

user_pc
Enthusiast
Enthusiast

hank you very much for your help! I will test

0 Kudos
user_pc
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

user_pc
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

user_pc
Enthusiast
Enthusiast

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

0 Kudos
LucD
Leadership
Leadership

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


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

user_pc
Enthusiast
Enthusiast

Class. Thank you for your help !

0 Kudos
user_pc
Enthusiast
Enthusiast

$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 -Encoding UTF8 -NoTypeInformation

0 Kudos
LucD
Leadership
Leadership

Does it get rid of the question marks in the CSV that way?


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

user_pc
Enthusiast
Enthusiast

Yes, open excel, click data from a text / CSV file. Choosing UTF-8 encoding

0 Kudos
LucD
Leadership
Leadership

Perfect!


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