{"id":180,"date":"2015-12-02T11:58:00","date_gmt":"2015-12-02T11:58:00","guid":{"rendered":"https:\/\/www.geekmungus.co.uk\/?p=180"},"modified":"2022-11-05T10:53:19","modified_gmt":"2022-11-05T10:53:19","slug":"automating-vmware-memory-changes","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=180","title":{"rendered":"Automating VMware Memory Changes"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I had to make some memory changes on a batch of VMs. I didn&#8217;t want to have to manually shutdown each VM, alter the memory setting and then restart as this would be very time consuming instead I prepared this script.\u00a0You first need to create a CSV file, e.g. vmalterlist.csv and place it somewhere on your machine. The format of the file should be as follows, where <strong>VMName <\/strong>is the name of the VM (as shown in vCenter) and <strong>TargetMemoryGB <\/strong>is the number of GB of RAM you want the VM to be altered to. The script when run will stop each VM, ideally using VMTools, change the memory and start back up the VMs again. Note: If you don&#8217;t specify to start the VMs explicitly they wont start.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>VMName,TargetMemoryGB\nwintest1,2\nubuntutest1,1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The script takes three arguments at the command line when run from PowerCLI. It requires the vCenter FQDN, the location of the CSV file and if you want the VMs to be powered back up again at the end. It assumes about 5 minutes to allow the VMs to shutdown.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;#\n.SYNOPSIS\nThis script will connect to vCenter, using the list specified shutdown each VM, alter the memory settings as per the CSV file and then start the VM again.\n.DESCRIPTION\nThe script makes use of a connection to the vCenter server to shut down each VM in turn, alter its memory and restart it.\nIf a VM does not have VMware Tools installed or running, the VM will be force powered off.\n.EXAMPLE\n.\/shutVMaltermem.ps1 -VIserver &lt;serverFQDN>  -FileNamePath &lt;filenameandpath> -PowerOnAtEnd &#91;Yes|No]\n.\/shutVMaltermem.ps1 -VIserver vcenter.domain.com -FileNamePath c:\\vmlist.csv -PowerOnAtEnd Yes\n.NOTES\nIf you run the script without any arguments it will connect with the default parameters. VMs will not be started unless specified.\nThe CSV file should have the first line as: VMName,TargetMemoryGB, then each VM should be specified by its name followed \nby the amount of memory in GB the memory should be reduced to.\n#>\nparam (\n    &#91;string]$VIserver = \"vcenter.domain.com\",\n    &#91;string]$filenamepath = \"c:\\vmalterlist.csv\",\n    &#91;string]$poweronatend = \"No\"\n)\nAdd-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue\n# Connect to vcenter server\nWrite-Host \"Connecting to vCenter......\"  \nconnect-viserver $VIserver\nWrite-Host\nWrite-Host \"Script is processing these VMs:\"\nWrite-Host\n# Import vm name from csv file  \nImport-Csv $filenamepath |  \n    foreach {\n        $strVMName = $_.VMName\n        $strTargetMemoryGB = $_.TargetMemoryGB\n        \n        # Get a view for the current VM\n        $vm = Get-View -ViewType VirtualMachine -Filter @{\"Name\" = $strVMName}\n        \n        # If VM is powered on check VMTools status, otherwise move on.\n        if ($vm.Runtime.PowerState -eq \"PoweredOff\") {\n                # Write to the screen the current power state\n                Write-Host $strVMName \"=\" $vm.Runtime.PowerState \" - VM powered off, nothing to do!\"\n        }\n        \n        if ($vm.Runtime.PowerState -ne \"PoweredOff\") {  \n            if ($vm.config.Tools.ToolsVersion -ne 0) {  \n                # Write to the screen the current power state\n                Write-Host $strVMName \"=\" $vm.Runtime.PowerState \" - VMware tools installed, graceful shutdown attempted.\"\n                \n                # Graceful shutdown the VM\n                Shutdown-VMGuest $strVMName -Confirm:$false  \n                          \n            } else {  \n                # Write to the screen the current power state\n                Write-Host $strVMName \"=\" $vm.Runtime.PowerState \" - VMware tools not installed, forced shutdown attempted.\"\n            \n                # Force shutdown the VM \n                Stop-VM $strVMName -Confirm:$false\n                \n            }  \n        }   \n    }\n    \nWrite-Host\nWrite-Host\nWrite-Host -NoNewLine \"Waiting 5 minutes for all VMs to shutdown, before altering memory: \"\nfor ($a=0; $a -le 4; $a++) {\n  Write-Host -NoNewLine \"`r#\"\n  Sleep 60\n}\nWrite-Host -NoNewLine \" Done!\"\nWrite-Host\nWrite-Host \"Altering memory on the following VMs:\"\nWrite-Host\n# Import vm name and new memory size from CSV file to perform the changes to the memory configuration on each VM.\nImport-Csv $filenamepath | \n    foreach {\n        $strVMName = $_.VMName\n        $strTargetMemoryGB = $_.TargetMemoryGB   \n              \n        Write-Host $strVMName \" - Memory set to:\" $strTargetMemoryGB\n        \n        # Set the memory to the new value, and force to confirm its change.\n        Set-VM -VM $strVMName -MemoryGB $strTargetMemoryGB -Confirm:$false\n    }\n    \n# Import vm name from CSV file, to restart all the VMs again, it will give you the option to confirm if you want to start a particular VM.\nImport-Csv $filenamepath | \n    foreach {\n        $strVMName = $_.VMName             \n               \n        # Attempt to start the VMs, if the user has requested that.\n        if ($poweronatend -eq \"Yes\") {\n            Write-Host \"Starting VM: \"$strVMName\n            Start-VM -VM $strVMName -Confirm:$false -RunAsync\n            \n        }\n        \n    }\n    \nif ($poweronatend -ne \"Yes\") {\n    # Write to the screen that no VMs will be started.\n    Write-Host \"Not starting any VMs at request of the user.\"\n}\n# Disconnect from vCenter.\ndisconnect-viserver $VIserver -Confirm:$false \nWrite-Host \"Script complete, disconnected from vCenter.\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I had to make some memory changes on a batch of VMs. I didn&#8217;t want to have to manually shutdown each VM, alter the memory setting and then restart as this would be very time consuming instead I prepared this script.\u00a0You first need to create a CSV file, e.g. vmalterlist.csv and place it somewhere on &#8230; <a title=\"Automating VMware Memory Changes\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=180\" aria-label=\"Read more about Automating VMware Memory Changes\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":155,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vmware"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":1413,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/180\/revisions\/1413"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}