6 Replies Latest reply on Oct 4, 2019 3:21 AM by ganapa2000

    Validating Memory and Datastore space

    ganapa2000 Hot Shot

      Hi,

       

      I am trying to validate host memory and datastore space before I continue with script, I would like to exit the script, even anyone of the validation fail

       

      How can I achieve this from below script,

       

      please help

       

      $vmhowime = 300

      $intNewVMDiskSize = 1000

       

      $vmh = Get-Cluster MyClus | Get-VMHost | Sort-Object -Property {$_.MemoryTotalGB - $_.MemoryUsageGB} -Descending:$true | Select-Object -First 1

      if (($vmh.MemoryTotalGB - $vmh.MemoryUsageGB + 20) -gt $vmhowime) {

          $($vmh.Name)

      }

      else {

          "oh, no -- not enough freememory on any of the host '$($vmh.Name)' to provision new VM, hence exiting the script"

          disconnect-viserver -server * -force -confirm:$false

      }

       

      $oDatastoreWithMostFree = Get-Datastore | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1

      if (($oDatastoreWithMostFree.FreespaceGB + 20) -gt $intNewVMDiskSize) {$($oDatastoreWithMostFree.Name)} else {"oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"}

       

      Get-folder | get-vm

        • 1. Re: Validating Memory and Datastore space
          LucD Guru
          Community WarriorsvExpertUser Moderators

          The return statement should do that.

           

          $vmhowime = 300

          $intNewVMDiskSize = 1000


          $vmh = Get-Cluster MyClus | Get-VMHost | Sort-Object -Property {$_.MemoryTotalGB - $_.MemoryUsageGB} -Descending:$true | Select-Object -First 1

          if (($vmh.MemoryTotalGB - $vmh.MemoryUsageGB + 20) -gt $vmhowime) {

              $($vmh.Name)

          }

          else {

              "oh, no -- not enough freememory on any of the host '$($vmh.Name)' to provision new VM, hence exiting the script"

              Disconnect-VIServer -server * -force -confirm:$false

              return

          }

           

          $oDatastoreWithMostFree = Get-Datastore | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1

          if (($oDatastoreWithMostFree.FreespaceGB + 20) -gt $intNewVMDiskSize) {

              $($oDatastoreWithMostFree.Name)

          }

          else {

              "oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"

              return

          }


          Get-folder | Get-VM

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          • 2. Re: Validating Memory and Datastore space
            ganapa2000 Hot Shot

            Perfect LucD,

             

            But I tried using set of few datastores, but it is not working

             

            $vmhowime = 100

            $intNewVMDiskSize = 3000

             

            $ds = @'

            DS35_UDEV

            DS36_UDEV

            DS37_UDEV

            '@

             

            $vmh = Get-Cluster POC-Cluster | Get-VMHost | Sort-Object -Property {$_.MemoryTotalGB - $_.MemoryUsageGB} -Descending:$true | Select-Object -First 1

            if (($vmh.MemoryTotalGB - $vmh.MemoryUsageGB + 20) -gt $vmhowime) {

                $($vmh.Name)

            }

             

            else {

                "oh, no -- not enough freememory on any of the host '$($vmh.Name)' to provision new VM, hence exiting the script"

                #Disconnect-VIServer -server * -force -confirm:$false

                return

            }

             

             

            $oDatastoreWithMostFree = Get-Datastore $ds | Sort-Object -Property FreespaceGB -Descending:$true | Select-Object -First 1

            if (($oDatastoreWithMostFree.FreespaceGB + 20) -gt $intNewVMDiskSize) {

                $($oDatastoreWithMostFree.Name)

            }

             

            else {

                "oh, no -- not enough freespace on datastore '$($oDatastoreWithMostFree.Name)' to provision new VM"

                return

            }

             

            Get-folder | Get-VM

             

             

            Output :

             

            Get-Datastore : 04-10-2019 15:05:17 Get-Datastore       Datastore with name 'VNX9112_DS35_UDEV

            DS36_UDEV

            DS37_UDEV' was not found using the specified filter(s).

            At D:\check_host_mem_datastore_freespace.ps1:22 char:27

            + $oDatastoreWithMostFree = Get-Datastore $ds | Sort-Object -Property F ...

            +                       ~~~~~~~~~~~~~~~~~
            + CategoryInfo      : ObjectNotFound: (:) [Get-Datastore], VimException
            + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat

               astore

             

            oh, no -- not enough freespace on datastore '' to provision new VM

            • 3. Re: Validating Memory and Datastore space
              LucD Guru
              vExpertUser ModeratorsCommunity Warriors

              It looks like a datastore with that name was not found.

              As a consequence, the If took the else branch.

              And the script stopped.

               

              Is that not the behaviour you desire?

              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 4. Re: Validating Memory and Datastore space
                ganapa2000 Hot Shot

                LucD,

                 

                first script works without any issues, thanks for that.

                 

                now we have around 40+ datastores out of I need to validate only around 3 datastores space, so I was trying as above.

                 

                the datastore with that name exists and has space, but validation is failing

                • 5. Re: Validating Memory and Datastore space
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  Ok, I see what is happening.
                  The here-string is read as one single string, which is not what the Name parameter on Get-Datastore can handle.

                  Try like this. specify the datastorenames as an array of strings

                   

                  $ds = 'DS35_UDEV','DS36_UDEV','DS37_UDEV'

                   


                  Get-Datastore -Name $ds

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: Validating Memory and Datastore space
                    ganapa2000 Hot Shot

                    Thank you very much.

                     

                    That worked perfectly.