- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When you take a brace away there, you have to add one at the end.
$vmlist = Import-Csv .\vmlist.csv -UseCulture
$vmlist1 = $vmlist | select -ExpandProperty VM
$taskTab = @{ }
#Shutdown VMs
foreach ($vm in $vmlist1) {
#if($vm.Powerstate -eq "PoweredOn") { //FYI If this part is not commented out, the script will run without doing anything.
Write-Host $vm is now being powered off. The script will confirm all VMs are powered off before proceeding to the next task... -foregroundcolor green
$taskTab[(Stop-VM -VM $vm -Confirm:$false -RunAsync).Id] = $vm
}
#}
#Take snapshot of VMs
$runningTasks = $taskTab.Count
while ($runningTasks -gt 0) {
Get-Task | % {
if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {
Get-VM $taskTab[$_.Id]
Write-Host Snapshot of $taskTab[$_.Id] is being taken before configuration change. The script will confirm all VM snapshots are complete before proceeding to the next task... -foregroundcolor green
$tasktab[(New-Snapshot -VM $taskTab[$_.Id] -Name BeforeVideoRamChange -RunAsync).Id] = $vm
$taskTab.Remove($_.Id)
$runningTasks--
}
elseif ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error") {
$taskTab.Remove($_.Id)
$runningTasks--
}
}
Start-Sleep -Seconds 15
#Update Video Ram
$runningTasks = $taskTab.Count
while ($runningTasks -gt 0) {
Get-Task | % {
if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {
Get-VM $taskTab[$_.Id]
if ($taskTab[$_.Id].Powerstate -eq "PoweredOn") {
return "One or more VMs is still powered on. Manually power off VMs before re-attempting."
}
$vid = $taskTab[$_.Id].ExtensionData.Config.Hardware.Device | ? { $_.GetType().Name -eq "VirtualMachineVideoCard" }
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange.Operation = 'edit'
$vid.videoRamSizeInKB = $newVideoRamSize
if ((!$devChange.Operation) -or (!$vid.videoRamSizeInKB)){
return "ERROR: Unable to set video memory on $taskTab[$_.Id]}. Ensure it is powered off."
}
Write-Host Video Memory on VM: $taskTab[$_.Id] has been successfully set to $vid.videoRamSizeInKB -foregroundcolor green
$devChange.Device += $vid
$spec.DeviceChange += $devChange
$taskTab[$_.Id].ExtensionData.ReconfigVM($spec)
$taskTab.Remove($_.Id)
$runningTasks--
}
elseif ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error") {
$taskTab.Remove($_.Id)
$runningTasks--
}
Start-Sleep -Seconds 15
Write-Host The script will wait for the configuration task to be complete before the VMs are powered on -foregroundcolor green
}
}
#Power on VMs
$runningTasks = $taskTab.Count
while ($runningTasks -gt 0) {
Get-Task | % {
if ($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success") {
Get-VM $taskTab[$_.Id]
if ($taskTab[$_.Id].Powerstate -eq "PoweredOff") {
Write-Host $taskTab[$_.Id] is now being powered on. Please wait five to ten minutes before verifying new configuration -foregroundcolor green
$taskTab[(Start-VM -VM $taskTab[$_.Id] -Confirm:$false -RunAsync).Id] = $vm
}
}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference