My if statement is always coming back as false

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
        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.

Anyone see what I am doing wrong?

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){

Worked perfectly! Thank you very much for the assistance!

… and because for IF() statement you need boolean value and DynamicMemoryEnabled IS boolean value you can write

If ((Get-VMMemory -VMName $vm.Name).DynamicMemoryEnabled){
…and because you already have VM object you can write

If ((Get-VMMemory -VM $vm).DynamicMemoryEnabled){

Tot use Select try -expandproperty

if ($(GET-VMMemory -vmname $vm_name | select -expandproperty DynamicMemoryEnabled) -eq $TRUE){

And like max told you, for boolean type you wont need to specify ‘-eq $true’