To power on the VMs after expanding the disk space, you can use the Start-VM cmdlet provided by PowerCLI. You can add the following code snippet after expanding the disk space:
# ...
Import-Csv -Path 'I:\Vmware\VDI_List.csv' | ForEach-Object {
$vm = Get-VM -Name $_.Name
if ($vm.PowerState -eq 'PoweredOff') {
Write-Host "Powering on VM $($vm.Name)" -ForegroundColor Green
Start-VM -VM $vm -Confirm:$false | Out-Null
} else {
Write-Host "VM $($vm.Name) is already powered on" -ForegroundColor Yellow
}
Get-HardDisk -VM $vm -Name 'Hard disk 1' | ForEach-Object {
Set-HardDisk -HardDisk $_ -CapacityGB ($_.CapacityGB + 10) -Confirm:$False
}
}
Write-Host "::: Disk Expansion Complete :::" -ForegroundColor Green
In this code snippet, Start-VM cmdlet is used to power on the VMs that are powered off. The -Confirm:$false parameter is used to suppress the confirmation prompt. The Get-HardDisk cmdlet is updated to use the -VM parameter to specify the VM for which the hard disk is being expanded.
Note: Please make sure to test the code in a non-production environment before using it in a production environment to avoid any unintended consequences.