Hello!! I'm hoping someone can assist me with a PowerCLI script that can pull the subject info into a txt or csv file. In an effort to get our environment cleaned up before our next DR exercise with SRM, I want to get a list that shows each VM, the datastore(s) it resides on, and the Folder name within VMs and Templates view. We keep our VMs organized by creating folder names that correspond to the app running on the VM. I mention SRM because last year our Protected Groups matched the Rcopy groups from our 3PAR array. However, we've added over 200 new VMs since that time and my team members basically placed these new VMs on the first available datastore that had enough free space. Unfortunately, we're not utilizing Storage Profiles...it's scheduled for next year.
The extracted info would look similiar to the following:
Servername Datastore Folder Name
Server1 LUN0 ActiveDirectory
Server2 LUN0 ActiveDirectory
Server3 LUN1 ActiveDirectory
Server4 LUN3 Argo
Server5 LUN7 Argo
Server6 LUN6 Lockbox
Server7 LUN5 Lockbox
Server8 LUN9 Citrix
etc...
Any help with extracting this info from vCenter would be greatly appreciated.
Thanks,
Charles
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc
I'd like to improve the script: orderding the output by folder name.
I tried to add this " | Sort-Object -descending " at the end of " @{N="Folder";E={$_.Folder.Name}} "
But I didn't worked out
Any idea?
Try like this
Select Name,
@{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},
@{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},
@{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},
@{N = "Folder"; E = {$_.Folder.Name}} |
Sort-Object -Property Folder
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It works! I was almost there!
Thank you Luc
Hello LucD
I know this work: Get-VM vmname | Get-NetworkAdapter | Select -ExpandProperty MacAddress
But, do you know how to put it to the script to also get the MacAddress of each vm?
Create an additional calculated property for that.
I added the -join in case you have VMs with more than 1 vNIC.
Select Name,
@{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},
@{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},
@{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},
@{N = "Folder"; E = {$_.Folder.Name}},
@{N = 'MAC';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}} |
Sort-Object -Property Folder
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you!
It works fine
I was able to notice that someone cloned a vm and I had 2 identicals mac addresses - -' and to see several mac addresses for a single vm...
In fact I had alarm on duplicated MacAddresses but this KB VMware Knowledge Base couldn't help me because my vCenters do not have the same ID..
Can you elaborate on the [string]::Join syntax? I get that you are calling a .net string.join method but what are you joining here and why?
The reason is because we want to have a single value in this calculated property.
So if the VM is located on several datastores, we join the names of the datastores (the 2nd operand to the Join method) with the 1 st operand character.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
oh gotcha, so if more than one item is returned it joins them into a comma delineated list. Thanks man!
Is it also possible to list the VM's on certain datastores. I want to look for those VM's who resides on datastores with for instance "NACL" in de datastore name, and to also show me the corresponding VM folder where the VM's resides in. Thank you in advance!
Use the Datastore parameter and OBN.
Like this
Select Name,
@{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}},
@{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N = "UsedSpaceGB"; E = {[math]::Round($_.UsedSpaceGB, 1)}},
@{N = "ProvisionedSpaceGB"; E = {[math]::Round($_.ProvisionedSpaceGB, 1)}},
@{N = "Folder"; E = {$_.Folder.Name}},
@{N = 'MAC';E={(Get-NetworkAdapter -VM $_).MacAddress -join '|'}} |
Sort-Object -Property Folder
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I get this after using the script:
Do you get the same error when you just do Get-VM?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No I don't, I then get a list of all the VM's. But most of the mentioned criteria in the query I don't need, like UsedSpace/MAC/ProvisionedSPace. I only need the VM name, Datastore name and the VM folder it resides in. If we were te exclude this, is the below query then valid. Also to view this in a comma delimited fashion with three columns next to eachother:
Get-VM -Datastore "*NACL*" |
Select Name,
@{N = 'GuestOS'; E = {$_.ExtensionData.Guest.GuestFullName}}
@{N = "Datastore"; E = {[string]::Join(',', (Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N = "Folder"; E = {$_.Folder.Name}} |
Sort-Object -Property Folder
I forgot to mention to also add ClusterName to the query, this would also be of good use.
Which PowerCLI version are you using?
There is no point in adapting the selection if you can't get the Get-VM working.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Version 6.3
Get-VM does work, I get a list of the VM's.
I would suggest you upgrade your PowerCLI installation.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you Luc, will do!