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
You can pipe the result to a CSV file for example
Get-VM |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
Get-VM |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="Folder";E={$_.Folder.Name}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD!!! That appears to provide me with what I'm looking for. However, the above command is outputting the info screen.....what can be added to pipe it to a TXT file?
Thanks,
Charles
You can pipe the result to a CSV file for example
Get-VM |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, I've marked this as "correct answer". Is there anything else I need to do to get it closed?
Thanks,
Charles
No, when you mark a reply as Correct, the thread is marked Answered automatically.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, I have 2 additonal columns of info I forgot to mention. I need to know how much stoarge has been provisioned and how much storage is being used.
Example:
Servername Datastore Provisioned Used Folder Name
Server1 LUN0 80GB 45GB ActiveDirectory
Server2 LUN0 1.2TB 730GB ActiveDirectory
Thanks,
Charles
No problem, try this
Get-VM |
Select Name,
@{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}} |
Export-Csv C:\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi how's it going?
I haven't done any powershell for years and I'm trying to remember how to do this stuff...
The example script is pretty good bit it's missing something for me.
I'm looking for the following in a list form if possible;
HOST VM DATASTORE NAME DATASTORE CAPACITY DATASTORE FREE SPACE
I'd really appreciate some help
Thanks in advance.
You mean like this ?
foreach($vm in Get-VM){
Get-Datastore -RelatedObject $vm |
Select @{N='Host';E={$vm.VMhost.Name}},
@{N='VMName';E={$vm.Name}},
Name,
@{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},
@{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Champion thank you.
How do you pipe that to a .csv like your earlier commands? I keep getting this error:
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> foreach($vm in Get-VM){ Get-Datastore -RelatedObject $vm | Select @{N='Host';E={$vm.VMhost.Name}}, @{N='VMName';E={$vm.Name}}, Name,@{N='Capacity';E={[math]::Round($_.CapacityGB,1)}}, @{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture
At line:1 char:235
+ ... eSpaceGB,1)}}} | Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCultu ...
+ ~
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EmptyPipeElement
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
And is there a way to also list the RDMs for each VM if any?
Try like this, a ForEach doesn't place anything in the pipeline.
Use the call operator (&) to force that.
&{foreach($vm in Get-VM){
Get-Datastore -RelatedObject $vm |
Select @{N='Host';E={$vm.VMhost.Name}},
@{N='VMName';E={$vm.Name}},
Name,
@{N='Capacity';E={[math]::Round($_.CapacityGB,1)}},
@{N='Free';E={[math]::Round($_.FreeSpaceGB,1)}}}} |
Export-Csv C:\vm-full-report-2.csv -NoTypeInformation -UseCulture
You can get the RDM info through the Get-Harddisk cmdlet, but that would require a somewhat different organisation of the script.
Perhaps you better make a new thread for that.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello,
I am very new to powercli and I think that it is a great tool to manage vmware infrastructure.
however, I do not know where to start.
Could someone tell me what the following code does
Get-ResourcePool | Where {$_.Name -ne “Resources“ }| Select Name, @{N=“NumVM“;E={($_ | Get-VM).Count}} | Sort Name|Export-Csv -NoTypeInformation c:\res-numvm.csv
Globally I know that it list ressource pools etc, but what I want to know is
1. Why do we use "$_.xxx"
2. Why and where do we use the "@"
3. What the "N" means
4. What the "E" means
5. Why the codes "$_ | Get-VM" is like it is and not like "$_Get-VM"
6. What the use of "|"
Nicolas
Hi Luc,
Good to see that script you have provided. its worked for entire VM in a cluster/ VCenter.
I'm looking for the script that can provide the report, only list of the VMs.
Eg :
VM1
VM3
VM10
VM14 ....
Could you please help me out on this ?
On the Select-Object, only keep the VMName property.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Could you please gimme the correct syntax ?
Hi LucD
How about if I want it to be granular?
Need to extract this by Cluster.
Can you give a sample layout of the desired output ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Here is the sample output needed:
Cluster Name | Datastore Name | NAA | VM | Capacity | Free |
---|---|---|---|---|---|