Skip to content

Instantly share code, notes, and snippets.

@sudo-battlekafer
Created September 2, 2022 14:15
Show Gist options
  • Save sudo-battlekafer/d7fb722c018a9a7269407be87b2043bc to your computer and use it in GitHub Desktop.
Save sudo-battlekafer/d7fb722c018a9a7269407be87b2043bc to your computer and use it in GitHub Desktop.
Powercli Upgrade Hardware version to 19
#import PoSH modules
Write-Verbose -Message "Loading Modules..." -Verbose
Get-Module -ListAvailable VMware* | Import-Module | Out-Null
#connect to VCenter
$path = "C:\Users\$env:UserName\.ssh\vmca.xml"
if(!(test-Path $path)){
Write-Host "Please create VMWare credentials file with the command"
Write-Host "New-VICredentialStoreItem -Host <vCenter server name/IP> -User <User name of vCenter server administrator> -Password <Password> -File <Location where you want to save the file to along with file name>"
Write-Host "Example: New-VICredentialStoreItem -Host vcenter.example.com -User [email protected] -Password 12345678910$# -File C:\Users\$env:UserName\.ssh\vmca.xml"
break
}
$credentials = Get-VICredentialStoreItem -file $path
Write-Host "Connecting to VCenter"
connect-viserver $credentials.Host -User $credentials.User -Password $credentials.Password
Clear-Host
#Get the folder name from the user. Update all VM's if blank
$folder = Read-Host "Which folder would you like to update? (Leave blank to update all VM's)"
#Check if the user entered a folder. If a folder was provided return only the VM's for that folder
If($folder)
{
Write-Host "Updating VM's in $folder"
#Get all VM's for a specified folder and return only their names
$virtualmachines = Get-Folder $folder | Get-VM | select -expandproperty Name
}
#Else if the user left the folder blank, get all VM's
Else
{
Write-Host "Updating all VM's"
#Get all VM's in the vCenter and return only their names
$virtualmachines = Get-VM | select -expandproperty Name
}
#Perform the following for each VM returned
ForEach ($vm in $virtualmachines)
{
Write-Host "Updating hardware version on $vm"
#shutdown VM
Shutdown-VMGuest -VM $vm -Confirm:$false
$do = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$do.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$do.ScheduledHardwareUpgradeInfo.UpgradePolicy = "always"
$do.ScheduledHardwareUpgradeInfo.VersionKey = "vmx-19"
$vm.ExtensionData.ReconfigVM_Task($do)
Start-VM -VM $vm -Confirm:$false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment