0

I have a Powershell Script which detects if a virtual Server has dynamic Memory enabled. Everytime i execute my Script the error ReadOnly property appers. Here is the error from PowerShell:

'DynamicMemoryEnabled' is a ReadOnly property.
At C:\Path\to\Script.ps1:131 char:16
+             if($VM.DynamicMemoryEnabled = $false){
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

This is my Script:

# Get virtual machines
$VMselection = Get-SCVirtualMachine | Where-Object {$_.Name -like "$searchVM"}
$VMs = $VMselection

#Start foreach-Schleife
foreach($VM in $VMs){
            if($VM.DynamicMemoryEnabled = $false){
                $ProcessorCount = $VM.CPUCount
                $StaticMemory = $VM.Memory
                $MemoryConfiguration = $VM.DynamicMemoryEnabled
                $MemorySize = $StaticMemory / "1024"

                # Format for Output
                $OutputFormat = "{0}c / {1}G{2}"
                if($MemoryConfiguration -eq $true){
                    $MemoryUnit = "d"
                }
                else{
                    $MemoryUnit = "s"
                }

                # Result Output
                $VMName = $VM.Name
                $Output = $OutputFormat -f $ProcessorCount, $MemorySize, $MemoryUnit

                [PSCustomObject]@{
                    Servername = $VMName
                    "CPU / RAM" = $Output
                } | Out-Null

            }
            else{
                $ProcessorCount = $VM.CPUCount
                $DynamicMemory = $VM.DynamicMemoryMaximumMB
                $MemoryConfiguration = $VM.DynamicMemoryEnabled
                $MemorySize = $DynamicMemory / "1024"

                # Format for Output
                $OutputFormat = "{0}c / {1}G{2}"
                if($MemoryConfiguration -eq $true){
                    $MemoryUnit = "d"
                }
                else{
                    $MemoryUnit = "s"
                }

                # Result Output
                $VMName = $VM.Name
                $Output = $OutputFormat -f $ProcessorCount, $MemorySize, $MemoryUnit

                [PSCustomObject]@{
                    Servername = $VMName
                    "CPU / RAM" = $Output
                } | Out-Null
            }

PS: Its just a snippet of my Script. These values are later on displayed into a DataGridView.

cosmo_
  • 11
  • 5
  • 1
    Can you clearly explain what you are trying to accomplish? The error is pretty clear: readonly properties are read-only and can't be changed. If you want to check the value use -eq instead of = (equality comparer instead of setting the value) – bluuf Aug 28 '23 at 07:00
  • This is due to a classic [gotcha](https://stackoverflow.com/a/69644807/1701026) where you *assign* (rather than *compare*) a value in a condition. – iRon Aug 28 '23 at 07:42
  • Thanks a lot guys. I really fell for that. ^^ - I guess that wont happen again :p – cosmo_ Aug 28 '23 at 08:05

1 Answers1

0

This code will try to set the property to false. First, this is not what you intend to do and second it fails because the property is read only.

if ($VM.DynamicMemoryEnabled = $false) {...}

To check for equality in Powershell, use this instead

if ($VM.DynamicMemoryEnabled -eq $false) {...}
David Trevor
  • 794
  • 1
  • 7
  • 22