PowerCLI

 View Only
  • 1.  Working with PowerShell Array

    Posted Sep 07, 2016 03:06 PM

    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

    • Hard Disk 1, Datastore Name
    • Hard Disk 1, Datastore Capacity
    • Hard Disk 1, Datastore Free Space
    • Hard Disk 1, Datastore VMFS Version

    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

    1. Identify all unique Datastore Names
    2. identify the NAA ID's for these Datastores (I've already found a way to do this second part)

    Thanks



  • 2.  RE: Working with PowerShell Array

    Posted Sep 07, 2016 07:08 PM

    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



  • 3.  RE: Working with PowerShell Array

    Posted Sep 07, 2016 07:30 PM

    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.



  • 4.  RE: Working with PowerShell Array
    Best Answer

    Posted Sep 07, 2016 08:34 PM

    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



  • 5.  RE: Working with PowerShell Array

    Posted Sep 09, 2016 10:07 AM

    Thanks Luc,

    That worked perfectly. I'd never come across RegEx expressions before, I definitely need to spend more time on them !

    Regards,

    Jason