I have a PowerCLI script that generates a PowerShell Array containing VM information. amongst other information, it includes the following for each hard disk, in every VM selected
As the number of Hard Disks per VM varies, so too does the number of Property fields per VM in the array.
This is making it difficult for me to extract specific data per hard disk based on Array location, e.g. $array[0] or $array[2+5]
Is there a way to search/extract data from specific Array Columns, based on the Column Name, e.g. $array.containskey("Datastore x Name")
What I want to ultimately do is
Thanks
Thanks, glad you enjoyed it.
Try something like this, it uses a RegEx expression to extract the datastore name properties.
Then it uses Sort-Object -Unique to find the list of unique datastores
$cluster = (Read-Host "Enter Target Cluster name")
$outputfile = "./$cluster-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".csv"
$report=@()
$datastores = @()
foreach ($v in (Get-Cluster $cluster | Get-VM)) {
$ReportProp=[ordered]@{
'VMname'=$v.name;
'Cluster'=$v.VMHost.Parent
}
$ds_count = 1
$ds = $v | Get-Datastore | foreach {
$ReportProp.Add("DataStore$($ds_count) Name",$_.Name)
$ReportProp.Add("DataStore$($ds_count) VMFS Version",$_.FileSystemVersion.split(".")[0])
$ReportProp.Add("DataStore$($ds_count) Capacity GB",[math]::round($_.CapacityGB,2))
$ReportProp.Add("DataStore$($ds_count) Free Space GB",[math]::round($_.FreeSpaceGB,2))
$ds_count++
}
$datastores += ($ReportProp[$ReportProp.Keys -match "DataStore\d+ Name"])
$report += New-Object -TypeName psobject -Property $ReportProp
}
$dsUnique - $datastores | Sort-Object -Unique
$report |
Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending |
Export-Csv $outputfile -NoTypeInformation -UseCulture
Invoke-Item $outputfile
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Not entirely sure what you want to here, but does this help?
Checking to see if column 'column2' contains a value equal as the value in $tgt
$t = @"
column1,column2,column3
1,2,3
4,5,6
7,8,9
"@
$a = ConvertFrom-Csv -InputObject $t
$tgt = 5
$a.column2 -contains $tgt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
I don't think that will help me (or should I say, I don't yet understand how it will help me)
My script is below. From it, I want to collect all unique datastore names, so that in turn I can get the NAA ID's for these DataStores
$cluster = (Read-Host "Enter Target Cluster name")
$outputfile = "./$cluster-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".csv"
$report=@()
foreach ($v in (Get-Cluster $cluster | Get-VM)) {
$ReportProp=[ordered]@{
'VMname'=$v.name;
'Cluster'=$v.VMHost.Parent
}
$ds_count = 1
$ds = $v | Get-Datastore | foreach {
$ReportProp.Add("DataStore$($ds_count) Name",$_.Name)
$ReportProp.Add("DataStore$($ds_count) VMFS Version",$_.FileSystemVersion.split(".")[0])
$ReportProp.Add("DataStore$($ds_count) Capacity GB",[math]::round($_.CapacityGB,2))
$ReportProp.Add("DataStore$($ds_count) Free Space GB",[math]::round($_.FreeSpaceGB,2))
$ds_count++
}
$report += New-Object -TypeName psobject -Property $ReportProp
}
$report |
Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending |
Export-Csv $outputfile -NoTypeInformation -UseCulture
Invoke-Item $outputfile
By the way, it was great hearing you speak in person at VMworld last week.
Thanks, glad you enjoyed it.
Try something like this, it uses a RegEx expression to extract the datastore name properties.
Then it uses Sort-Object -Unique to find the list of unique datastores
$cluster = (Read-Host "Enter Target Cluster name")
$outputfile = "./$cluster-" + (Get-Date -Format yyyy-MMM-dd-HHmm) + ".csv"
$report=@()
$datastores = @()
foreach ($v in (Get-Cluster $cluster | Get-VM)) {
$ReportProp=[ordered]@{
'VMname'=$v.name;
'Cluster'=$v.VMHost.Parent
}
$ds_count = 1
$ds = $v | Get-Datastore | foreach {
$ReportProp.Add("DataStore$($ds_count) Name",$_.Name)
$ReportProp.Add("DataStore$($ds_count) VMFS Version",$_.FileSystemVersion.split(".")[0])
$ReportProp.Add("DataStore$($ds_count) Capacity GB",[math]::round($_.CapacityGB,2))
$ReportProp.Add("DataStore$($ds_count) Free Space GB",[math]::round($_.FreeSpaceGB,2))
$ds_count++
}
$datastores += ($ReportProp[$ReportProp.Keys -match "DataStore\d+ Name"])
$report += New-Object -TypeName psobject -Property $ReportProp
}
$dsUnique - $datastores | Sort-Object -Unique
$report |
Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count } -Descending |
Export-Csv $outputfile -NoTypeInformation -UseCulture
Invoke-Item $outputfile
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc,
That worked perfectly. I'd never come across RegEx expressions before, I definitely need to spend more time on them !
Regards,
Jason