The first script is used to start the homelab.
So this script starts the ESXi host using IPMI and loads the CSV file that contains the running virtual machines from last time and starts them as soon as the ESXi host is connected to vCenter.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #variables $ipmiCred = Get-Credential ADMIN $Ip = "192.168.2.229" $vcenterCred = get-credential root $vcenterserver = '192.168.2.200' $vmhost = '192.168.2.230' $PoweredOnVMspath = 'c:\temp\PoweredOnVMs.csv' #boot host using IPMI try { Get-PcsvDevice -TargetAddress $Ip -ManagementProtocol IPMI -Credential $ipmiCred | Start-PcsvDevice } catch { $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName write-host "error connecting to IPMI: $faileditem $errormessage" } #load PowerCLI . 'C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1' #connect to vCenter server try{ Connect-VIServer -Server $vcenterserver -Credential $vcenterCred } catch{ write-host 'Connection to vCenter server failed' -ForegroundColor Red } #wait for the host to start up do { sleep 10 $ServerState = (get-vmhost $vmhost).ConnectionState } while ($ServerState -eq 'NotResponding') Write-Host "$vmhost is still booting" #load the list of VMs that were powered on last time try{ $PoweredOnVMs = Import-Csv -Path $PoweredOnVMspath } catch{ Write-Host 'Import CSV of powered on VMs failed' -ForegroundColor Red } #start VMs that were powered on last time try{ start-vm $PoweredOnVMs.name} catch{Write-Host 'VMs power on failed' -ForegroundColor Red} |
The second script is used to store the running virtual machines into the CSV file and shut down the ESXi host.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #variables $vcenterCred = get-credential root $vcenterserver = '192.168.2.200' $vmhost = '192.168.2.230' $PoweredOnVMspath = 'c:\temp\PoweredOnVMs.csv' #load PowerCLI . 'C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1' #connect to vCenter server try{ Connect-VIServer -Server $vcenterserver -Credential $vcenterCred } catch{ write-host 'Connection to vCenter server failed' -ForegroundColor Red } #find powered on VMs try{ $PoweredOnVMs = get-vmhost $vmhost | get-vm | Where-Object{$_.PowerState -eq 'PoweredOn'} } catch{ write-host 'Failed to find Powered On VMs' -ForegroundColor Red} #export powered on VMs to CSV try{ $PoweredOnVMs | Export-Csv -Path $PoweredOnVMspath } catch { Write-Host 'failed to export CSV' -ForegroundColor Red} # shut it all down, start with VMs that have VMtools installed foreach ($PoweredOnVM in $PoweredOnVMs) { try{ Shutdown-VMGuest $PoweredOnVM.Name -Confirm:$false} catch{Suspend-VM $PoweredOnVM.Name -Confirm:$false}} write-host 'Shutting down VMs and waiting some minutes' -foregroundcolor Green #wait for the VMs to shut down or suspend do { start-sleep 10 $VMState = (get-vmhost $vmhost|get-vm) } while ($VMState.PowerState -eq 'PoweredOn') #wait a few more seconds start-sleep 15 #shut down the ESXi host write-host 'Shutting down ESXi host' try{Stop-VMHost $vmhost -Force -Confirm:$false | Out-Null} catch{Write-Host 'ESXi host shutdown failed' -ForegroundColor Red} |
As with all things in a homelab, these scripts are subject to change as soon as I think of something new. Suggestions are welcome!
No comments:
Post a Comment