Connects PowerCLI to an ESXi host or vCenter with credentials resolved from the ServerEngine credential store, finds the VM whose name matches the host being worked on, and powers it on unless it already runs. The connection is closed again at the end of every run, which is what keeps a scheduled job from leaking sessions.
powershell
# Set target VM name
$server = "esxi.example.local"
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true -Confirm:$false
$username = "SE-CredentialsStore.Username.(esxi.example.local)"
$password = "SE-CredentialsStore.Password.(esxi.example.local)"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $securePassword)
Connect-VIServer -Server $server -Credential $credential
$vm = $targetVM = Get-VM | Where-Object { $_.Name -like "*$hostname*" }
if (-not $vm) {
Write-Host "<WRITE-LOG = ""*ERROR: VM not found: $vmName*"">"
} elseif ($vm.PowerState -eq "PoweredOn") {
Write-Host "<WRITE-LOG = ""*INFO: VM $vmName is already powered on.*"">"
} else {
try {
Start-VM -VM $vm -Confirm:$false | Out-Null
Write-Host "<WRITE-LOG = ""*SUCCESS: VM $vmName powered on.*"">"
} catch {
Write-Host "<WRITE-LOG = ""*ERROR: Failed to power on VM $vmName - $($_.Exception.Message)*"">"
}
}
Disconnect-VIServer -Server $server -Confirm:$falseRun it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in VMware & vSphere
Ready when you are.
Try ServerEngine free for 7 days.