Configure HotAdd (CPU & RAM)

Voici un script pour vous permettre de configurer vos VMs avec l’option Hot CPU et/ou RAM. Attention, vos VMs devront redémarrer.

# POWERCLI 6.5 Release 1 - by Olivier Gosselin
# http://www.purplescreen.eu

function Set-HotAdd {
<# 
.SYNOPSIS
This cmdlet
.DESCRIPTION
.EXAMPLE
Set-HotAdd -Name MyVM -CPU True -Memory True
Configure Hot add for Memory and CPU to Virtual machine MyVM
.EXAMPLE
Set-HotAdd -Name MyVM,AllVM* -CPU True -Memory True
Configure Hot add for Memory and CPU to Virtual machine MyVM and all vm beginning with AllVM
.INPUTS
None
.PARAMETER Name
Specifie the vm name.
.PARAMETER CPU
Set CPU Hot add to false or true
.PARAMETER Memory
Set Memory Hot add to false or true
.LINK
http://www.purplescreen.eu
#>
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,Position=0)]
    [array]$Name,

    [Parameter(Mandatory=$False,Position=1)]
    [ValidateSet("False", "True")]
    [string[]]$Memory,

    [Parameter(Mandatory=$False,Position=2)]
    [ValidateSet("False", "True")]
    [string[]]$CPU

)#PARAM

$VM = Get-VM -Name $Name | Get-View
$ArrayVMs = $VM.Name
$VMs = @($ArrayVMs)

Write-Host ""
Write-Host "Les VM suivantes seront redÈmarrÈes : " -ForegroundColor red

if (!$VM) {
$ErrorActionPreference = "Stop"
Write-Host "Le script c'est arreter a cause d'une erreur." -ForegroundColor Red
}

foreach ($MyNameis in $VM) {
$VMWaretoolsState = $MyNameis.Summary.Guest.ToolsStatus
$NameOutput = New-Object psobject
$NameOutput | Add-Member -MemberType NoteProperty -Name Name -Value $MyNameis.Name
$NameOutput | Add-Member -MemberType NoteProperty -Name StatusTools -Value $VMWaretoolsState
$NameOutput
}
$WarningBoot = $NameOutput | where {$_.StatusTools -ne "toolsOk"}
if ($WarningBoot) {
Write-Host "Attention : Certaines VMs seront Èteintes par un PowerOff. Utilisez UpdateManager pour mettre ‡ jour les VMWare tools ou vÈrifiez leur Ètat." -ForegroundColor Red

Write-Host ""
Write-Host "Faire [ctrl+c] pour annuler"
Pause
Write-Host ""
}
foreach ($VMName in $VMs) {
$i++
$intSize = $intSize + $objFile.Length
Write-Progress -activity "VM en cours de configuration $VMName" -status "Status : " -percentComplete (($i / $VMName.length)  * 100)
$VMView = Get-VM -Name $VMName | Get-View
$PowerState = $VMView.Runtime.PowerState
$ToolsRun = $VMView.Summary.Guest.ToolsStatus

if ($PowerState -eq "poweredOn") {
if ($ToolsRun -eq "toolsOk") {
Get-VM -Name $VMName | Stop-VMGuest -Confirm:$false
}
else {
Get-VM -Name $VMName | Stop-VM -Confirm:$false
Write-Host "Les VMTools ne fonctionnent pas sur cette Machine virtuelle ($VMName). La machine est eteinte par un PowerOff" -ForegroundColor red
}
Write-Host "Arret de la machine en court ($VMName)..." -ForegroundColor Yellow
}
do {
$DoVMView = Get-VM -Name $VMName | Get-View
$PowerState = $DoVMView.Runtime.PowerState
Start-Sleep -Seconds 1
} while ($PowerState -ne "poweredOff")

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
if ($Memory -eq "True") {
$spec.memoryHotAddEnabled = $true
}
else {
$spec.memoryHotAddEnabled = $false
}

if ($CPU -eq "True") {
$spec.cpuHotAddEnabled = $true
}
else {
$spec.cpuHotAddEnabled = $false
}

Write-Host "On applique les modifications." -ForegroundColor Yellow
$VMView.ReconfigVM_Task($spec)

Start-Sleep -Seconds 10
Get-VM -Name $VMName | Start-VM

Write-Host "La machine Virtuelle ($VMName) est en cour de dÈmarrage." -ForegroundColor red
Write-Host ""
}
$intSize = "{0:N0}" -f $intSize
}