Reply to Message

View discussion in a popup

Replying to:
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