VMware Cloud Community
CG21
Enthusiast
Enthusiast

extract list of vms with powercli for several esx or vcenter

Hi,

I would like to export the vm list of my vcenter and my esx with the name of the vm, the ip address, the description and the name of the vcenter or esx to which it is attached.
all in tabular form or csv.

if it is possible that the script get the esx and vcenter name in a csv file that I prepare before.

thank you for your help.

0 Kudos
25 Replies
LucD
Leadership
Leadership

You mean something like this?

Get-VM |

Select Name, Notes,

    @{N='vCenter';E={([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host}},

    @{N='ESXi';E={$_.VMHost.Name}},

    @{N='IP';E={$_.Guest.IPAddress -join '|'}} |

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


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

0 Kudos
CG21
Enthusiast
Enthusiast

yes it is partly that.
but I have a vcenter and several other independent esx.
he would have to look for all that.
is it possible to put a variable in the script that will look for the name of the vcenter and esx and their identifier in a csv file ?

currently I did that on the vcenter with
connect-viserver vcentername -user vcenteruser -pass vcenterpassword

0 Kudos
LucD
Leadership
Leadership

What would you be specifying in that CSV, both a vCenter and an ESXi name?

And would you have multiple rows in that CSV?

And should all results go to the same CSV?

If yes, then you could do

Import-Csv -Path .\names.csv -UseCulture |

ForEach-Object -Process {

    Get-VMHost -Name $_.ESXName -Server $_.vcName |

    Get-VM -Server $vCenterName |

    Select Name, Notes,

        @{N='vCenter';E={([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host}},

        @{N='ESXi';E={$_.VMHost.Name}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}}

} |

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

The CSV file should look like this

vcName,ESXName

vc1,esx1

vc1,esx2

vc2,esx3

The connections to the vCenters shall be done before running the script


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

0 Kudos
CG21
Enthusiast
Enthusiast

What would you be specifying in that CSV, both a vCenter and an ESXi name?

--> yes . i have aone vcenter with multiple esx, but i also have esxi independent.

And would you have multiple rows in that CSV?

--> yes like this:

vcenter1

esxi1

esxi2

esxi3

...

And should all results go to the same CSV?

Yes, that's why I would like in the result a column with  the ratachement to esxiname or vcentername

I have an error in this script. see Attachment.
and where can I specify the connections to vcenter or esxi with the username and password?

0 Kudos
LucD
Leadership
Leadership

That layout of the proposed CSV would be difficult to handle.

How would the script know which line is a vCenter and which line is an ESXi servers?

You could leave the columns blank, something like this

vcName,ESXName

,esx1

vc1,

vc1,esx2

Would that work for you?

The errors you see are most probable due to the fact that some fields are not filled in.
What did you have in the CSV you used?


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

0 Kudos
CG21
Enthusiast
Enthusiast

No it doesn't work .i test with colums blank and  i have the same error.

i  think it is possible and not difficult.

so I have to do 2 scripts one for vcenter and another one for all esxi.
I do not mind if I need 2 scripts and 2 results files

for the vcenter is not a problem because I get vms for one vcenter,

but how can I do with my multiple esxi ?

thank you

0 Kudos
LucD
Leadership
Leadership

The previous version does not work with blank columns, this version should work.

Import-Csv -Path .\names.csv -UseCulture |

ForEach-Object -Process {

    if($_.vcName){

        if($_.ESXName){

            $vm = Get-VMHost -Name $_.ESXName -Server $_.vcName | Get-VM

        }

        else{

            $vm = Get-VM -Server $_.vcName

        }

    }

    else{

        if($_.esxName){

            $vm = Get-VMHost -Name $_.ESXName | Get-VM

        }

        else{

            $vm = Get-VM

        }

    }

    $vm | Select Name, Notes,

        @{N='vCenter';E={([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host}},

        @{N='ESXi';E={$_.VMHost.Name}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}}

} |

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


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

0 Kudos
CG21
Enthusiast
Enthusiast

here is the file with my vcenter and my esxi independent :

vcenter01,

, esxi01

, esxi02

and the result gives me twice the list of vm vcenter but not the vms of esxi independant

0 Kudos
LucD
Leadership
Leadership

Do you have the column headers in the CSV (as I showed earlier)?


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

0 Kudos
CG21
Enthusiast
Enthusiast

Yes I have the column headers in the csv (as you showed earlier), like this :

vcName,ESXName

,esxi01

,esxi02

vcenter1,

and it gives me only the list of vcenter vms twice , but not give me the esxi independent .

0 Kudos
LucD
Leadership
Leadership

Are these ESXi nodes in vCenter1?

It would help if you could show or explain the environment.


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

0 Kudos
LucD
Leadership
Leadership

You just marked the thread as Answered, with your latest reply as the Correct Answer.

Not sure I'm following?


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

0 Kudos
CG21
Enthusiast
Enthusiast

sorry, it's a bad manipulation.

0 Kudos
LucD
Leadership
Leadership

No problem, can you reply to my question before?

How does your environment look like?

Are those 2 ESXi ndoes in that vCenter?


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

0 Kudos
CG21
Enthusiast
Enthusiast

we have a vcenter with 5 esxi in our offices and we have 2 remote centers with one esxi each who are not in our vcenter

0 Kudos
LucD
Leadership
Leadership

Are you connected to all these vSphere servers  (ESXi + vCenter) when you run the script?

What is in $global:defaultviservers?


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

0 Kudos
CG21
Enthusiast
Enthusiast

It's actually ok, i forgot to connect to the other esxi.

but it gives me twice the result. not important.

thanks a lot LucD.

0 Kudos
LucD
Leadership
Leadership

I can't replicate what you are seeing with the double result.

Are you sure that you are not connected multiple times?

You can see that in the $global:defaultviservers variable.


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

0 Kudos
CG21
Enthusiast
Enthusiast

Hi,

for information, after my tests and some changes here is the script that works.

I have 4 files in my folder :

cred.txt  ( this is a file with the crypted password for my first esx)

cred2.txt  ( this is a file with the crypted password for my seconde esx)

names.csv (this is a file with the list of th esx server and the country and the description)

vcName; Country; description
esx01; PARIS; Le Chapitre 01

vcenter01; LYON; HDD

extraction.ps1

$username = "vcenter"
$username2 = "root"


$pass = Get-Content C:\temp\Cred.txt | Convertto-SecureString #Chemin du mot de passe crypté.
$pass2 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($pass2)


$pass3 = Get-Content C:\temp\Cred2.txt | Convertto-SecureString #Chemin du mot de passe crypté.
$pass4 = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass3)
$password2 = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($pass4)


Import-Csv -Path "c:\temp\names.csv" -UseCulture |

ForEach-Object -Process{

    if($_.vcName -Match "vcenter")
{
                    Connect-ViServer $_.vcName -user $username -password $password
}
else
{
     Connect-ViServer $_.vcName -user $username2 -password $password2
}
}

Import-Csv -Path "c:\temp\names.csv" -UseCulture |

ForEach-Object -Process{

    if($_.vcName)
{
                    $vm = Get-VM -Server $_.vcName
}

    $vm | Select Name, Notes,

        @{N='vCenter';E={([System.Uri]$_.ExtensionData.Client.ServiceUrl).Host}},

        @{N='IP';E={$_.Guest.IPAddress -join '|'}},

        @{N="MAC";"Expression"={($_ | Get-NetworkAdapter).MacAddress}}

} |


Export-Csv "c:\temp\report.csv" -NoTypeInformation -UseCulture

And so i have the good report like this:

NameNotesvCenterIPMAC
CONVERTEResx01xx,xxx,xxx,xx
VM-TESTesx01
test-2esx01
APPLI178 JAVA vcenter01xxx,xxx,xxx,xxx
SVN01SVN :vcenter01xxx,xxx,xxx,xxx
APPLI230LVNET vcenter01

I would just add 2 column with the city where the esx and the descrition (2nd and 3rd field in names.csv), how to do pls?

0 Kudos