9 Replies Latest reply on Nov 12, 2019 6:40 AM by vespavbb

    get-vm where $guestConfig.memoryReservationLockedToMax = $True

    vespavbb Novice

      Hi,

       

      I want to create a list with all VM´s where the MAx Reservation of RAM is enabled and Locked

       

      I just have this and i´m able to to a where with reservation like 0, but i need vm´s whith ram locked enabled as well

      And I guess something is wrong with my if else or? How can i dot the exit better? :-)

       

       

      #########

      $vms = get-vm -name vmtest0* | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -like "0"}

      $guestConfig = New-Object VMware.Vim.VirtualMachineConfigSpec

      $guestConfig.memoryReservationLockedToMax = $True

      ForEach ($vm in $vms)

      {

       

       

      if ( get-vm $vm | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -like "0"})

       

      {

        

         (Get-VM $vm).ExtensionData.ReconfigVM_task($guestConfig)

         Sleep  5

         # create report    

      $vmobj = get-vm $vm

      $row = "" | select VM, Cluster, GB, Reservation, ChangeDate

      $row.vm = $vmobj.name

      $row.Cluster = $vmobj | Get-Cluster | Select -ExpandProperty Name

      $row.GB = $vmobj.MemoryGB

      $row.Reservation = $vmobj | Get-VMResourceConfiguration | Select -ExpandProperty MemReservationGB

      $row.Changedate =  $CurrentDate

       

      $report += $row 

       

      $report |  Export-csv -force  "$path\changeRAMsetting.csv" -UseCulture -NoTypeInformation

      $report  | Export-csv  "$patcharchive\changeRAMsetting$CurrentDate.csv" -UseCulture -NoTypeInformation

        

         }

       

         else

         {

         write-host "no change needed" -ForegroundColor Yellow

         }

        • 1. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          Not too sure what you are trying to do.
          Is this only to produce a report?
          Or do you also want to reconfigure the VMs that do not fit?

           

          For the report alone, you could do

           

          Get-VM |

          Where-Object { $_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -eq 0 -and

                         $_.ExtensionData.Config.MemoryReservationLockedToMax} |

          Select Name,

              @{N='Cluster';E={(Get-CLuster -VM $_).Name }},

              MemoryGB,

              @{N='Reservation';E={$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation/1GB}} |

          Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

          • 2. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
            vespavbb Novice

            Hi,

             

            I need to find the VM´s where the ram reservation is not set and lockedtomax is not set and reconfigure them, but only where it is missing

             

            After reconfigure i want to export the report with the arry information like @report

            • 3. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              Do you also want to reconfigure if one of those 2 is not set?

              • 4. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
                LucD Guru
                User ModeratorsCommunity WarriorsvExpert

                In any case, the following will make the change when one of the settings is not compliant.

                If you want the change when both are not compliant, change the -or to an -and.

                 

                $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

                $spec.MemoryReservationLockedToMax = $True

                $spec.MemoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo

                $spec.MemoryAllocation.Reservation = 0


                Get-VM |

                Where-Object { $_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -ne 0 -or

                               -not $_.ExtensionData.Config.MemoryReservationLockedToMax} |

                ForEach-Object -Process {

                    $vm.ExtensionData.ReconfigVM($spec)

                    Get-VM -Name $vm.Name |

                    Select Name,

                        @{N='Cluster';E={(Get-Cluster -VM $_).Name }},

                        MemoryGB,

                        @{N='Reservation';E={$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation/1GB}},

                        @{N='ChangeDate';E={Get-Date}}

                } |

                Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

                • 5. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
                  vespavbb Novice

                  Hi

                  yes I did it with "-or" this looks correct for me, .....almost but the "else" is not working. I would expect that the message appears in the shell, but does not

                   

                  #Body

                  #Connect-VIServer -Server $server -Username $username -Password $password

                   

                  $path = "D:\blabla"

                  $patcharchive = "D:\blabla\Archive"

                   

                  $CurrentDate = Get-Date

                  $CurrentDate = $CurrentDate.ToString('dd-MM-yyyy')

                   

                   

                  #get VM´s where no RAM Reservation is set

                  #$cluster = get-cluster

                  $vms = get-vm -name vmtest0* | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -like "0" -or $_.ExtensionData.Config.MemoryReservationLockedToMax -notlike "true"}

                  $guestConfig = New-Object VMware.Vim.VirtualMachineConfigSpec

                  $guestConfig.memoryReservationLockedToMax = $True

                  $report = @()

                   

                   

                  ForEach ($vm in $vms)

                  {

                   

                   

                  if (get-vm $vm | Where-Object {$_.ExtensionData.ResourceConfig.MemoryAllocation.Reservation -like "0" -or $_.ExtensionData.Config.MemoryReservationLockedToMax -notlike "true"})

                   

                   

                  {

                    

                     (Get-VM $vm).ExtensionData.ReconfigVM_task($guestConfig)

                     Sleep  5

                     # create Report   

                  $vmobj = get-vm $vm

                  $row = "" | select VM, Cluster, GB, Reservation, ChangeDate

                  $row.vm = $vmobj.name

                  $row.Cluster = $vmobj | Get-Cluster | Select -ExpandProperty Name

                  $row.GB = $vmobj.MemoryGB

                  $row.Reservation = $vmobj | Get-VMResourceConfiguration | Select -ExpandProperty MemReservationGB

                  $row.Changedate =  $CurrentDate

                   

                  $report += $row 

                   

                   

                  $report |  Export-csv -force  "$path\changeRAMsetting.csv" -UseCulture -NoTypeInformation

                  $report | Export-csv  "$patcharchive\changeRAMsetting$CurrentDate.csv" -UseCulture -NoTypeInformation

                    

                     }

                   

                     else

                     {

                     write-host "nothing to do" -ForegroundColor Yellow

                     }

                      

                     }

                  sleep 5

                   

                   

                  $report | select VM, Cluster,GB,Reservation

                  • 6. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
                    LucD Guru
                    vExpertUser ModeratorsCommunity Warriors

                    That is not the script I posted.

                    So you want a line in the console saying "Nothing to do" when the VM is compliant?

                    • 7. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
                      vespavbb Novice

                      Hi Luc,

                      yes, everything works fine now(in my script), except the else exit

                      If you can check why the "nothing to do" does not appear.... this would help me for learning .

                      • 8. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
                        LucD Guru
                        vExpertCommunity WarriorsUser Moderators

                        When you fill in the variable $vms at the start you already do the test, hence only VMs that will pass the test will get through.
                        That is why the else block will never be reached.

                         

                        Please stop using the -like operator to test [int] and [boolean] values.

                        Use the -eq operator instead.

                        • 9. Re: get-vm where $guestConfig.memoryReservationLockedToMax = $True
                          vespavbb Novice

                          oh you are right, I understand.

                          Thank you Luc