Tuesday, July 12, 2011

Enable Hot Add Mem & vCPU

<#
# Search for "Hot Add" Capable VMs and Enable
# Requirements: Powershell Version 2.0, PowerCLI 4.01 (not 4.0)
# Version 1.0 - 7/12/2011
#>

######################################
# Prompt to specify vCenter & Cluster
######################################

$vCenterName = Read-Host "Which vCenter to connect to (FQDN)"
$clusterName = Read-Host "Which Cluster"

###############################
# Connect to specified vCenter
###############################

Connect-VIServer -server $vCenterName -Protocol https -User "Username" -Password "Password"

##########################################
# Get collection of VMs & Create Log File
##########################################

$colVMs = get-cluster -name $clusterName | Get-VM
$logFile = New-Item -ItemType File -Path "D:\scripts\ScriptOutput\HotAddCapableVMsList_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"

#################################################################
# Enable / Disable Hot Add Functions
#################################################################

Function Enable-vCpuAndMemHotAdd($vm){
  $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $extra1 = New-Object VMware.Vim.optionvalue
  $extra1.Key="mem.hotadd"
  $extra1.Value="true"
  $vmConfigSpec.extraconfig += $extra1
  $extra2 = New-Object VMware.Vim.optionvalue
  $extra2.Key="vcpu.hotadd"
  $extra2.Value="true"
  $vmConfigSpec.extraconfig += $extra2
  $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Enable-MemHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Disable-MemHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="mem.hotadd"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Enable-vCpuHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="true"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

Function Disable-vCpuHotAdd($vm){
    $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $extra = New-Object VMware.Vim.optionvalue
    $extra.Key="vcpu.hotadd"
    $extra.Value="false"
    $vmConfigSpec.extraconfig += $extra
    $vm.Extensiondata.ReconfigVM($vmConfigSpec)
}

#################################################################
#
#################################################################

foreach ($VM in $colVMs) {
    $VMPower = $VM.PowerState
    $VMName = $VM.name
    $OS = (Get-VMGuest $VM).OSFullName
    $vHW = $vm.ExtensionData.Config.Version

##############################
# See if VM is powered on
##############################

    if ($VMPower -notmatch "PoweredON") {
        Write-Host $VMName `t "Powered OFF" -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tPowered OFF")
    }    

##############################
# See if VM is vHW 7
##############################

    elseif ($vHW -notmatch "vmx-07") {
        Write-Host $VMName `t "Not vHW 7" -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`tNot vHW 7`t`t" + $vHW)
    }
         
###############################
# See if VM has a Windows 7
###############################

    elseif ($OS -like "*Windows 7*"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }
     
###############################
# See if VM has a Windows 2008
###############################

    elseif ($OS -like "*Windows Server 2008*"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }
   
###################################
# See if VM has a Windows 2003 Ent
###################################

    elseif ($OS -like "*Windows Server 2003,Enterprise Edition*"){
        Enable-MemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }

###################################
# See if VM has a SLES 11
###################################

    elseif ($OS -like "Suse Linux Enterprise 11*"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }

###################################
# See if VM has a SLES 10
###################################

    elseif ($OS -like "Suse Linux Enterprise 10*"){
        Enable-MemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }
   
###################################
# See if VM has a RHEL 5 (64-bit)
###################################

    elseif ($OS -like "Red Hat Enterprise Linux 5 (64-bit)"){
        Enable-vCpuAndMemHotAdd $vm
        Write-Host $VMName `t $OS -ForegroundColor Green
        Add-Content -Path $logFile -Value ($VMName + "`t`tUpdate Hot Add`t`t" + $OS + "`t`t" + $vHW)
    }

###################################
# Everything Else
###################################

    else {
        Write-Host $VMName `t $OS -ForegroundColor Red
        Add-Content -Path $logFile -Value ($VMName + "`t`tNo Update`t`t" + $OS + "`t`t" + $vHW)
    }

}

##############################
# Disconnect from vCenter
##############################

Disconnect-VIServer -server $global:DefaultVIServers -Force

##############################
# Email log file
##############################
   
#send-mailmessage -SmtpServer "SMTPServerName" `
#-from "VM-Tools-Upgrade <DoNotReply@noreply.gov>" `
#-to "Somebody <somebody@somewhere.gov>" `
#-subject "VMware Tools Upgrade" `
#-body "VMware Tools upgrade log attached." `
#-Attachment $logFile

##############################

No comments:

Post a Comment