Is there a way/script to export all VMs from a vCenter with "all common" data as, name, state, space, vcpu, mem, uptime etc...AND which folder the VM resides in?
If I (in GUI) manually export data in the VMs view, I can select all these parameters, but NOT the "Folder" option.
I need a list (csv?) of all VMs with this info:
Name - State - Status - Host - Cluster - Provisioned Space - Guest OS - Memory Size - CPUs - Uptime - IP Address - DNS Name AND Folder
This is so I can sort out and sum up resources for all VMs in for example the "Development Team" folder
Any bright ideas?
You could do something like this
Get-VM |
select Name, @{N="FolderName";E={ $_.Folder.Name}},
@{N='FolderPath';E={
$path = $_.Name
$parent = Get-View -Id $_.ExtensionData.Parent
while($parent){
$path = "$($parent.Name)\$path"
if($parent.Parent){
$parent = Get-View -Id $parent.Parent
}
else{
$parent = $null
}
}
$path}},
NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost,
@{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}},
@{N="IPAddress";E={$_.Guest.IPAddress[0]}},
@{N="DNSName";E={$_.ExtensionData.Guest.Hostname}} |
Export-Csv C:\Temp\Result2.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you search this community?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, but I see that I left something out in my question;
Regarding the folder, I want to output all VMs from the "top" folder.
As in
-Cuscomer1
-test
-dev
-Customer2
-test
-dev
-Customer3
-test
-dev
I want the "folder" column in the output csv to list if the VMs is located in the folder Customer1, 2 or 3 (not test or dev)
If I use this;
I only get the direct folder the VM is located under (test or dev)
$VMs = Get-VM
$Output = foreach ($VM in $VMs){
Get-VM $VM | select Name, @{N="FolderName";E={ $_.Folder.Name}}, NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost, @{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}}, @{N="IPAddress";E={$_.Guest.IPAddress[0]}}, @{N="DNSName";E={$_.ExtensionData.Guest.Hostname}}
}
$Output | Export-Csv C:\Temp\Result2.csv -NoTypeInformation
You might have a look at the function in Get the folderpath
With the full path you can check for Customer1, Customer2, ...
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks.
Do you know if there is a way to "bake" that into my script, so the folderpath for each VM will show as a column (as all the other values) in the output csv file?
You can use the Get-FolderPath function in a calculated property.
This assumes you do not have folders with the same name in different locations in your inventory.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry...not the most experienced scripter here...
Can you give an example?
You could do something like this
Get-VM |
select Name, @{N="FolderName";E={ $_.Folder.Name}},
@{N='FolderPath';E={
$path = $_.Name
$parent = Get-View -Id $_.ExtensionData.Parent
while($parent){
$path = "$($parent.Name)\$path"
if($parent.Parent){
$parent = Get-View -Id $parent.Parent
}
else{
$parent = $null
}
}
$path}},
NumCpu, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, PowerState, OS, VMHost,
@{N="Datastore";E={$_.ExtensionData.Config.DatastoreUrl.Name}},
@{N="IPAddress";E={$_.Guest.IPAddress[0]}},
@{N="DNSName";E={$_.ExtensionData.Guest.Hostname}} |
Export-Csv C:\Temp\Result2.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Almost....but not quite.
Now it gives me "Customer1-nono" on all VMs. (only one VM are located in that folder)
Name - FolderName - FolderPath
vCenter | Prod | Datacenters\Nilzen\vm\Customer1\Customer1-nono\vCenter |
NilzenRDS | Prod | Datacenters\Nilzen\vm\Customer1\Customer1-nono\NilzenRDS |
Download | Prod | Datacenters\Nilzen\vm\Customer1\Customer1-nono\Download |
SMTP | Prod | Datacenters\Nilzen\vm\Customer1\Customer1-nono\SMTP |
Customer1VM | Customer1-nono | Datacenters\Nilzen\vm\Customer1\Customer1-nono\Customer1VM |
DC02 | DCs | Datacenters\Nilzen\vm\Customer1\Customer1-nono\DC02 |
DC01 | DCs | Datacenters\Nilzen\vm\Customer1\Customer1-nono\DC01 |
My bad, a typo.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Now it works like a charm!
Thanks a lot for your excellent help, and have a great weekend 🙂
Hi.
Is there a way to alter your script to give me an output that don't add the VM name?
Right now it gives:
Name - FolderName - FolderPath
PAL_Oracle | Folder0 | Datacenters\DC1\vm\Customer1\Folder0\PAL_Oracle |
HUSPRO01 | Folder1 | Datacenters\DC1\vm\Customer1\Folder1\HUSPRO01 |
HEIPMFPRO01 | Folder1 | Datacenters\DC1\vm\Customer1\Folder1\HEIPMFPRO01 |
I need the folderpath to view "Datacenters\DC1\vm\Customer1\Folder0" (not include the hostname)
Or even better; just dispay the "Customer1" value?
The script I'm using now:
Get-VM |
select Name, @{N="FolderName";E={ $_.Folder.Name}},
@{N='FolderPath';E={
$path = $_.Name
$parent = Get-View -Id $_.ExtensionData.Parent
while($parent){
$path = "$($parent.Name)\$path"
if($parent.Parent){
$parent = Get-View -Id $parent.Parent
}
else{
$parent = $null
}
}
$path}} |
Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformation
Thanks
Something like this?
Get-VM |
select Name, @{N = "FolderName"; E = { $_.Folder.Name } },
@{N = 'Folder'; E = {
$parent = Get-View -Id $_.ExtensionData.Parent
while ($parent) {
if ($parent.Name -match "^Customer") {
$parent.Name
}
if ($parent.Parent) {
$parent = Get-View -Id $parent.Parent
} else {
$parent = $null
}
}
}
} |
Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformationAp
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not quite;
The "Customer1" name are dynamic.
I have a lot of folders in vCenter, like
Customer1name
-test
-dev
Customer2
-mgmt
-user
Customer3
-abc
-def
I need the folderpath to show the SE00...., SE02...., SE03....
Or at least stop with that (not include hostname in the path)
Without the VM name
Get-VM |
select Name, @{N = "FolderName"; E = { $_.Folder.Name } },
@{N = 'FolderPath'; E = {
$path = ''
$parent = Get-View -Id $_.ExtensionData.Parent
while ($parent) {
if($path -ne ''){
$path = "$($parent.Name)\$path"
}
else{
$path = $parent.Name
}
if ($parent.Parent) {
$parent = Get-View -Id $parent.Parent
} else {
$parent = $null
}
}
$path }
} |
Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformationAp
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That looks good.
One final (I hope) question;
Is it possible to remove the last part in the folderpath?
So that is stops with the customername (SE02....)
Like:
Datacenters\Sto1\vm\SE00-PSCloud_SE\
Instead of
Datacenters\Sto1\vm\SE00-PSCloud_SE\MGMT
Try like this
Get-VM |
select Name, @{N = "FolderName"; E = { $_.Folder.Name } },
@{N = 'FolderPath'; E = {
$path = ''
$parent = Get-View -Id $_.ExtensionData.Parent
while ($parent) {
if($path -ne ''){
$path = "$($parent.Name)\$path"
}
else{
$path = $parent.Name
}
if ($parent.Parent) {
$parent = Get-View -Id $parent.Parent
} else {
$parent = $null
}
}
Split-Path -Path $path }
} |
Export-Csv "C:\Reporting\foldertest.csv" -NoTypeInformationAp
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not quite...now the folderpath is blank>
My bad, I had a typo in the Split-Path cmdlet.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi.
The VM name is gone, but it still reports folders after the SE00-xyz\