I am currently having the below code which is taking clone of a vm and mounting OS iso file to the cloned VM cd drive
```
$SourceVMName = "999_SW999OSTest"
$CloneVMName = "999_SW999OSTest_Clone"
$VMFolder = "Shared Infra (New)"
$TargetDatastore = "DISTR_HPERF_NPROD_LUN-01_DEV_3333"
$SourceVM = Get-VM -Name 999_SW999OSTest
$VMHost = Get-VMHost -VM 999_SW999OSTest
# Get the target datastore
$Datastore = Get-Datastore -Name $TargetDatastore
$DatastoreCluster = Get-DatastoreCluster -Datastore DISTR_HPERF_NPROD_LUN-01_DEV_3333
New-VM -Name $CloneVMName -VM $SourceVM -VMHost $VMHost -Location $VMFolder -Datastore $DatastoreCluster -RunAsync
while($true)
{
Start-Sleep -Seconds 300
# Step 3: Mount ISO to the clone VM
$IsoPath = "[DISTR_HPERF_NPROD_LUN-01_DEV_3333] Patches/SW_DVD9_Win_Server_STD_CORE_2019_1809.2_64Bit_English_DC_STD_MLF_X22-18452.ISO"
# Replace with the datastore path to the ISO file
$CloneVM = Get-VM -Name $CloneVMName
if ($CloneVM.PowerState -eq "PoweredOn") {
$CDDrive = Get-CDDrive -VM $CloneVM
$CDDrive | Set-CDDrive -ISOPath $IsoPath -Connected $true -Confirm:$false
}
}
```
Next I need to install the OS from the mounted iso file. I am planning to upgrade the OS from 2016 to 2022
please let me know on how to proceed with this
Found this procedure to do an in-place upgrade of your windows server. https://learn.microsoft.com/en-us/windows-server/get-started/perform-in-place-upgrade
can you please let me know how this link is helpful in terms of powershell
To do an inplace upgrade your code could look something like this (as described in the link that @MerlevedeN posted):
# Import the Windows PowerShell modules that are required for the upgrade
Import-Module ServerManager
Import-Module DISM
# Get the name of the VM that you want to upgrade
$VMName = "myVM"
# Get the path to the Windows Server 2022 installation media
$ISOPath = "C:\Temp\WindowsServer2022.iso"
# Mount the ISO file to the CD drive of the VM
$CDDrive = Get-CDDrive -VM $VMName
$CDDrive | Set-CDDrive -ISOPath $ISOPath -Connected $true -Confirm:$false
# Start upgrade
Start-Process -Wait -FilePath "setup.exe" -ArgumentList "/IAcceptEULA /Upgrade /SourcePath:$ISOPath"
