PowerCLI: Add Hot Add / Hot Plug Status to a Specific VM Guest

#****************************************************************
# Script Name : Add-HotAddHotPlug-SpecificVM.ps1
# Purpose    : Add Hot Add / Hot Plug Status to a specific VM Guest
# Date       : 08-29-2023, 1300 CST
# - Initial version
# Date       : 08-29-2023 1444 CST
# - Updated with $PSCredential_VMWARE secured credentials
# Author     : bing.com/www.Burwell.tech
#****************************************************************
#Set VM Vcenter login and password c reentials securly
$localvmcredFile = "$env:USERPROFILE\localvmcredential.xml"  
  
if(Test-Path $localvmcredFile){  
    $PSCredential_VMWARE = Import-Clixml $localvmcredFile  
}  
else{  
    $localvmusername = "administrator@vsphere.local"
    $PSCredential_VMWARE = Get-Credential -Credential "$localvmusername"  
    $PSCredential_VMWARE | Export-Clixml $localvmcredFile  
}  
$VMServerName = Read-Host -prompt "What VM server do you want to Update to Hot Pluggable and Hot add?"
Get-VM -Name $VMServerName | Stop-VMGuest -Confirm:$false
#Identify Vcenter
$vCenterServer = Get-VMGuest -VM $VMServerName | Select-Object -ExpandProperty VMHost | Select-Object -ExpandProperty Parent

# Connect to vCenter
Connect-VIServer -Server $vCenterServer -Credential $PSCredential_VMWARE # -User <Username> -Password <Password> #<-- old way

# Find the VM
$vm = Get-VM -Name  $VMServerName

# Shut down the VM and wait for it to complete with verification
Stop-VMGuest -VM $VMServerName -Confirm:$false
do {
    Start-Sleep -Seconds 5
} until ((Get-VM -Name  $VMServerName).PowerState -eq 'PoweredOff')

# Add hot plug and hot swap capability to the VM guest
Set-VM -VM $VMServerName -MemoryHotAddEnabled $true -CpuHotAddEnabled $true

# Turn on the VM and wait for it to complete with verification
Start-VM -VM $VMServerName
do {
    Start-Sleep -Seconds 5
} until ((Get-VMGuest -VM $VMServerName).GuestState -eq 'Running')

# Disconnect from vCenter
Disconnect-VIServer -Server $vCenterServer -Confirm:$false
<#
Replace <vCenterServer> with the name or IP address of your vCenter server, <Username> with your vCenter username, <Password> with your vCenter password, and  $VMServerName with the name of your VM.
This script connects to your vCenter server using Connect-VIServer, finds your VM using Get-VM, shuts down your VM using Stop-VMGuest, waits for the shutdown to complete with verification using a do...until loop and Start-Sleep cmdlet, adds hot plug and hot swap capability to your VM guest using Set-VM, turns on your VM using Start-VM, waits for it to complete with verification using a do...until loop and Start-Sleep cmdlet, and disconnects from your vCenter server using Disconnect-VIServer.
Please note that this script assumes that you have already installed VMware PowerCLI on your system. If you haven’t installed it yet, you can download it from VMware’s website.
#>


Posted

in

,

by