vespavbb
Enthusiast
Enthusiast

get-vm where $guestConfig.memoryReservationLockedToMax = $True

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

   }

VCP4,VCP5,VCP6
Tags (3)
0 Kudos
9 Replies
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

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

VCP4,VCP5,VCP6
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

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

VCP4,VCP5,VCP6
0 Kudos
LucD
Leadership
Leadership

That is not the script I posted.

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

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 .

VCP4,VCP5,VCP6
0 Kudos
LucD
Leadership
Leadership

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
vespavbb
Enthusiast
Enthusiast

oh you are right, I understand.

Thank you Luc

VCP4,VCP5,VCP6
0 Kudos