In my PowerShell script, I have built a query that should list me various details about VMs in our two vCenters and then save the output of it to an Excel file. Specifically, the script queries the following things:
- ResourcePool
- HardDisk
- Datastore
- Storage Tier (Custom Attribute)
The code of this script is as follows:
$allvcs = @("vCenter1", "vCenter2")
foreach ($vCenterInstance in $allvcs) {
$Credential = Import-Clixml -Path C:\Scripts\credentials.cred
$vcconnect = Connect-VIServer -Server $vCenterInstance -Credential $Credential
$CLN = Get-Cluster | Sort-Object -Property Name
write-host "Currently working on: $vCenterInstance $CLN... "
#Request VMs
Get-vm -name * | Where-Object { $_.Name -notlike '*_replica'} | Where-Object {$_.powerstate -eq 'PoweredOn'} |
Select @{n="ResourcePool"; e={(Get-ResourcePool -VM $_)}},
Name,
@{n="HardDisk"; e={(Get-HardDisk -VM $_)}},
@{n="Datastore"; e={(Get-Datastore -RelatedObject $_)}},
@{n="Storage Tier (VM)"; e={(Get-Annotation -Name "Storage Tier" $_).Value}} |
Export-Csv C:\Scripts\vcoutput\clc_csv_xtended\$vCenterInstance.csv -NoTypeInformation -UseCulture
Disconnect-VIServer -Confirm:$false
}
The output looks like this:
Unfortunately, that is not what I would like to see. How can the script be supplemented so that I get an independent field per 'HardDisk' and only the associated datastore? Output should look like this:
What can I do to reach my goal? I would be very happy to receive answers.
Cheers, James
Presumably it will be possible with a Foreach loop, though unfortunately I couldn't get it to work.