The following script will upgrade all guest virtual machines from a lower version to the desired version:
# upgradevHardwareVersion.ps1
# User inputs
$desiredVersion='9.0'
$excludeKeywords=@(
'lax',
'aws'
)
$vmmServer=$env:computername
function updateVmVersion($vmNames,$desiredVersion='9.0',$vmmServer=$env:computername){
$results=@()
foreach($vmName in $vmNames){
$version=.{
try{
$vm=get-scvirtualmachine $vmName -vmmServer $vmmServer
$previousVersion=$vm.Version
if($vm.VirtualMachineState -eq 'Running' -and [version]$previousVersion -lt [version]$desiredVersion){
$null=stop-scvirtualmachine -vm $vm
$null=Update-SCVMVersion -vm $vm
$vm=start-scvirtualmachine -vm $vm
if($vm.status -eq 'Running'){
write-host "$vmName has been updated from version $previousVersion to $($vm.Version) and is now back online"
}else{
write-host "$vmName has been updated from version $previousVersion to $($vm.Version) but it's now offline"
return $previousVersion
}
}elseif($vm.VirtualMachineState -eq 'PowerOff' -and [version]$previousVersion -lt [version]$desiredVersion){
$null=Update-SCVMVersion -vm $vm
}
return $vm.Version
}catch{
write-warning $_
return $previousVersion
}
}
$results+=@{$vmName=$version}
}
return $results
}
# Get the report
$vmsWithLowerVersions=get-scvirtualmachine|?{[version]$_.version -lt [version]$desiredVersion}
write-host "Processing list:`r`n$($vmsWithLowerVersions|select name,version|out-string)"
$filteredList=$vmsWithLowerVersions|?{$name=$_.Name;$null -eq $($excludeKeywords|?{$name -like "*$_*"})}
write-host "Filtered list:`r`n$($filteredList|select name,version|out-string)"
$inversedList=$vmsWithLowerVersions|?{$name=$_.Name;$null -ne $($excludeKeywords|?{$name -like "*$_*"})}
write-host "Inverted list:`r`n$($inversedList|select name,version|out-string)"
# Upgrade all VMs with lower versions
updateVmVersion $vmsWithLowerVersions.Name $desiredVersion $vmmServer
Upgrade a selected list of VMs
$vmsToUpgrade=@"
eqwebmdadfs01
test
test1
"@
$vmmServer='lax-vmm01-kimconnect.com'
$desiredVersion='9.0'
function updateVmVersion($vmNames,$desiredVersion='9.0',$vmmServer=$env:computername){
$results=@()
foreach($vmName in $vmNames){
$version=.{
try{
$vm=get-scvirtualmachine $vmName -vmmServer $vmmServer
$previousVersion=$vm.Version
if($vm.VirtualMachineState -eq 'Running' -and [version]$previousVersion -lt [version]$desiredVersion){
$null=stop-scvirtualmachine -vm $vm
$null=Update-SCVMVersion -vm $vm
$vm=start-scvirtualmachine -vm $vm
if($vm.status -eq 'Running'){
write-host "$vmName has been updated from version $previousVersion to $($vm.Version) and is now back online"
}else{
write-host "$vmName has been updated from version $previousVersion to $($vm.Version) but it's now offline"
return $previousVersion
}
}elseif($vm.VirtualMachineState -eq 'PowerOff' -and [version]$previousVersion -lt [version]$desiredVersion){
$null=Update-SCVMVersion -vm $vm
}
return $vm.Version
}catch{
write-warning $_
return $previousVersion
}
}
$results+=@{$vmName=$version}
}
return $results
}
$vmnames=@($vmsToUpgrade -split "`n")|%{$_.Trim()}
write-warning "Please confirm that we're ready to have these virtual machines rebooted:`r`n$vmnames"
pause
updateVmVersion $vmnames $desiredVersion $vmmServer
Simple version – upgrading only VMs that are offline
$vmsToUpgrade=@"
TEST
TEST1
"@
$vmmServer='lax-vmm01-kimconnect.com'
$vmnames=@($vmsToUpgrade -split "`n")|%{$_.Trim()}
foreach($vmName in $vmNames){
$vm=get-scvirtualmachine $vmName -vmmServer $vmmServer
if($vm.VirtualMachineState -eq 'PowerOff'){
try{
$oldVersion=$vm.Version
$null=Update-SCVMVersion -vm $vm
$newVersion=$vm.Version
write-host "$vmName virtual hardward has been upgraded from version $oldVersion to $newVersion"
pause
}catch{
write-warning $_
}
}
}
Categories: