VMware Cloud Community
Jocker-H
Contributor
Contributor

Script to set disk ,CPU and Memory for an existing VMs

Hello VMWare Communities.

VMWare version : 7.x

I'm locking for a script to add or remove disk,memory and CPU on Multiples existing VMs at ones.

Note : Some of them the hot add feature is disabled and other is enabled.

Could you please help me 

Thanks in advance.

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership

That is a very broad question.
The New-Harddisk (for harddisks) and Set-VM (for CPU and memory) cmdlets should do the trick.
Note that if you want to enable the hot-add feature, the VM will need to power off/power on.

You will need to give more details.
Are the VMs that need changes in a list, a CSV perhaps?
Should the hot-add feature be enabled?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Jocker-H
Contributor
Contributor

Hello LucD,

Yes the VM are in a list CSV that contain the VM name and the new value to be changed.

Some of them the Hot add Feature is disabled.

So i'm looking for a script to change the VM parameters disk, memory,CPU  if the Hot Add is enabled and if not return the list of the VM with disabled hot add option.

Every one how just started Learning Scripting could ask bad questions !

Thanks in advance

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this.

The new Harddisk is always applied.

When the VM is powered off the CPU and Memory settings are always applied.
When the VM is powered on the hot-add and hot-remove settings are checked.
When a powered on VM doesn't have at least one of the required hot- settings it is exported to a CSV.

# CSV layout: Name, CPU, Memory, Disk
#             VM1, 2, 4096, 40
#             VM2, 2, 4096, 40

$notEnabled = @()

Import-Csv -Path .\vm.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
    $vm = Get-VM -Name $row.Name
    New-HardDisk -VM $vm -CapacityGB $row.Disk -Confirm:$false | Out-Null

    if($vm.PowerState -eq 'PoweredOn') {
        $cpuAdd = $cpuRemove = $memAdd = $true
        if($vm.CpuHotAddEnabled -and $row.CPU -gt $vm.NumCpu){
            Set-VM -VM $vm -NumCpu $row.CPU -Confirm:$false | Out-Null
        }
        else{
            $cpuAdd = $false
        }
        if($vm.CpuHotRemoveEnabled -and $row.CPU -lt $vm.NumCpu){
            Set-VM -VM $vm -NumCpu $row.CPU -Confirm:$false | Out-Null
        }
        else{
            $cpuRemove = $false
        }
        if($vm.MemoryHotAddEnabled -and $row.Memory -gt $vm.Memory){
            Set-VM -VM $vm -MemoryGB $row.Memory -Confirm:$false | Out-Null
        }
        else{
            $memAdd = $false
        }
    }
    else{
        Set-VM -VM $vm -NumCpu $row.CPU -MemoryGB $row.Memory -Confirm:$false | Out-Null
    }
    if($cpuAdd,$cpuRemove,$memAdd -notcontains $false){
        $notEnabled += New-Object -TypeName PSObject -Property ([ordered]@{
            VM = $vm.Name
            PowerState = $vm.PowerState
            CpuAdd = $cpuAdd
            CpuRemove = $cpuRemove
            MemAdd = $memAdd
        })
    }
}

$notEnabled | Export-Csv -Path .\notEnabled.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference