I am rather new, as in this my first script, to powershell so forgive me if this is a simple fix but I’ve written this:
`Write-Host "$(Get-Date) Changing buffer on virtual machines" -ForegroundColor Green
$GetVM = Get-VM -ComputerName localhost
Foreach ($vm in $GetVM)
{
$vm_name = $vm.Name
Write-Host "vm =" $vm_name
if ($(GET-VMMemory -vmname $vm_name | select DynamicMemoryEnabled) -eq $TRUE){
Write-Host "`n$vm_name is running with dynamic memory`n"
Write-Host "Changing buffer to 35% on $vm" -ForegroundColor White
Set-VMMemory $vm -Buffer 35
Write-Host "Changed buffer to 35% on $vm" -ForegroundColor White
}
else
{
Write-Host "`n$vm_name is NOT running with dynamic memory`n"
}
}
Write-Host "$(Get-Date) Buffer Change Completed" -ForegroundColor Green `
It should be pretty simple. It worked until I added the check to make sure the memory was dynamic but now it is always coming back false. I know that most of my machine are set up with dynamic memory so this shouldn’t be the case.
With your if statement before, you were selecting a single property, but not -Expanding it. When you do this, PowerShell doesn’t merely give you the text value of a property, but also all of the rich-object metadata. And when you do PowerShell comparisons, it’s pretty dump and -eq will only be true when both sides of the comparison exactly match.
When I changed it to the following, your code works:
If ((get-vmmemory -VMName $Vm.Name).DynamicMemoryEnabled -eq $true){
#…
}