VMware Cloud Community
the1960
Contributor
Contributor

Set-VMResourceConfiguration

Hi Guys

I don't understand why dosen't work. I want for all VM's MemReservations set to 0.

ForEach ($VM in (Get-VM)){

  $MemResGB = 0
  $vmview = Get-VM $VM | Get-View
  $MemReservation = $vmview.Config.MemoryAllocation.Reservation

  If ($MemReservation -eq 512) {
     Write-Host $VM " --- " $MemReservation
     Set-VMResourceConfiguration -MemReservationGB $MemResGB
     Write-Host $VM " *** " $MemReservation
  }
}

Can help anybody

0 Kudos
6 Replies
CNorris_Const
Enthusiast
Enthusiast

You need to use the Get-VMResourceConfiguration command with Set-VMResourceConfiguration, the Configuration parameter is required for the Set-VMResourceConfiguration command:

requiredConfigurationVMResourceConfiguration[]named
  • pipeline
Specifies the configuration object you want to modify.

 

Keep in mind that there are a number of ways you could do this, but here is your code snippet with a small modification that should work for you. If you want the second Write-Host to show the configuration change, you need to use the Get-VMResourceConfiguration command again to check the value and use Write-Host after:

 

ForEach ($VM in (Get-VM)){

$MemResGB = 0
$MemReservation = Get-VMResourceConfiguration $VM

If ($MemReservation.MemReservationGB -eq 512) {
Write-Host $VM " --- " $MemReservation.MemReservationGB
Set-VMResourceConfiguration -Configuration $MemReservation -MemReservationGB $MemResGB
$CheckMemReservation = Get-VMResourceConfiguration $VM | Select-Object -Property MemReservationGB
Write-Host $VM " *** " $CheckMemReservation.MemReservationGB
}
}

 

Let me know if you have any further questions.

0 Kudos
the1960
Contributor
Contributor

Thanks for the fast answer.

$MemReservation.MemReservationGB never returns the value 512!

Br

0 Kudos
CNorris_Const
Enthusiast
Enthusiast

There are a number of properties that you can view from Get-VMResourceConfiguration that may be useful for what you're trying to do. There is also a MemReservationMB property that will show the value in MB. The majority of my VMs in the Lab show 0, but you should be seeing the value that it's set to before hand with the first Write-Host command. If you're not sure, cross check a few VMs and confirm the Memory Reservation is set to what the first Write-Host is showing.

Just an FYI, mine never returns 512 either, I do see other numbers. Is there a reason you should be finding a lot of VMs with 512 set for the Memory Reservation?

0 Kudos
the1960
Contributor
Contributor

With Get-View I get the parameter Config.MemoryAllocation.Reservation, which returns e.g. the value 512 or 128. This value was a default value that we set some time ago. Now all values should be set to 0.

0 Kudos
CNorris_Const
Enthusiast
Enthusiast

Try this code. This should be modifying the same configuration that you were seeing the value on.

 

$MemResGB = New-Object VMware.Vim.VirtualMachineConfigSpec
$MemResGB.MemoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo
$MemResGB.MemoryAllocation.Reservation = 0

ForEach ($VM in (Get-VM)){

$MemReservation = Get-VM $VM | Get-View -Property Config.MemoryAllocation

If ($MemReservation.Config.MemoryAllocation.Reservation -eq 512) {
Write-Host $VM " --- " $MemReservation.Config.MemoryAllocation.Reservation
$MemReservation.ReconfigVM_Task($MemResGB)
$CheckMemReservation = Get-VM $VM | Get-View -Property Config.MemoryAllocation.Reservation
Write-Host $VM " *** " $CheckMemReservation.Config.MemoryAllocation.Reservation
}
}

 

This worked in my Lab environment and changed the VMs accordingly. Let me know how things go.

0 Kudos
the1960
Contributor
Contributor

Many Thanks

excellent. Works very fine.

Br Tobias

0 Kudos