2 Replies Latest reply on Feb 14, 2020 5:10 AM by OBTMOM

    PowerCLI - need to filter datastore and exclude some names

    OBTMOM Lurker

      Hello,

       

      our datastore have a naming convention and i want to filter out only the local and backup stores.

       

      Local Stores start with "A_"

      Backup Stores with "C_"

       

      We do have different cluster setups ones with 3 and another with more than 4 or 5 Datastores. So i wanted to set my action depending on how many datastores are in the cluster.

       

      here is what i tried so far. and YES i am not a powercli pro at all (mea culpa

       

      Count and comapre

      $countlocaldatastore = (Get-Cluster -Name $cluster | Get-Datastore A*).count

      $countnfsdatastore = (Get-Cluster -Name $cluster | Get-Datastore C*).count

      $countalldatastores = $countlocaldatastore + $countnfsdatastore

       

      if ($countalldatastores -gt 3) {echo "more than 3"}

      if ($countalldatastores -le 3) {echo "less or equal than 3"}

       

      This works do far but something dynamic would be much cooler and of course more flexible.

       

      Found this later:

      Get-Datastore -RelatedObject $cluster | where {$_.Name -notmatch "B_|D_"}

       

      used it to sort the for freespace:

      $mostfreespace = Get-Datastore -RelatedObject $cluster | where {$_.Name -match "A_|C_"} | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1

      $mostfreespace1 = Get-Datastore -RelatedObject $cluster | where {$_.Name -notmatch "B_|D_"} | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1

      $mostfreespace2 = Get-Datastore -RelatedObject $cluster | where {$_.Name -notmatch "B_|D_"} | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 2 | Select-Object -Skip 1

      $mostfreespace3 = Get-Datastore -RelatedObject $cluster | where {$_.Name -notmatch "B_|D_"} | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 3 | Select-Object -Skip 2

       

      i need at least 2 stores better 3. As the backup stores usually have more space compared to the local ones i wanted to start with them but if they are not availalble (different cluster) i would like to use the locals.

       

      So the plan is to list all datastores of a cluster but exclude the ones using B_ and D_ in their names.

       

      So far i was not able to get this right. Best would really be all in one which i could later sort for freespace and take the ones with the most first.

       

      Hope you can follow me

       

      thank you very much

      armin

       

        • 1. Re: PowerCLI - need to filter datastore and exclude some names
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Not really sure what exactly you want, but the following (except for the indication 'at the beginning of the name'), you already have.

          The following will just return the 3  (or less) datastores with the most free space, in decending order.

           

          Get-Cluster -Name $cluster |

          where{$_.Name -notmatch "^B_|^D_"} |

          Sort-Object -Property FreeSpaceGB -Descending |

          Select -First 3

           

          What do you want to do more?

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: PowerCLI - need to filter datastore and exclude some names
            OBTMOM Lurker

            LucD,

             

            you are a star!

             

            changed it to

            Get-Cluster -Name $cluster | Get-Datastore | where{$_.Name -notmatch "^B_|^D_"} | Sort-Object -Property FreeSpaceGB -Descending | Select -First 3

             

            and tested it. Exactly what i was looking for. I forgot the "^" and it always threw different results with names i did not want.

             

            THANK YOU for your swift reply!

            armin