# Powershell script to boot VMs that are off # used for Solarwinds Alerts # Derrick Rose # Get VM Node Name from commandline Argument param ( [string]$n )
# Check to make sure VMWare PowerCli is loaded $PowerCLIModulePath = "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules" $OldModulePath = [Environment]::GetEnvironmentVariable('PSModulePath','Machine') if ($OldModulePath -notmatch "PowerCLI") { Write-Host "[Adding PowerCLI Module directory to Machine PSModulePath]" -ForegroundColor Green $OldModulePath += ";$PowerCLIModulePath" [Environment]::SetEnvironmentVariable('PSModulePath',"$OldModulePath",'Machine') } else { Write-Host "[PowerCLI Module directory already in PSModulePath. No action taken]" -ForegroundCol or Cyan 19 } 20 21 # Adds the base cmdlets 22 Add-PSSnapin VMware.VimAutomation.Core 23 # This script adds some helper functions and sets the appearance. You can pick and choose parts of this file for a fully custom appearance. 24 . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironm ent.ps1" 25 26 #Configure Script 27 $TimeStart = "23" # 11 PM / 24 hour 28 $TimeEnd = "3" # 3 AM / 24 hour 29 $cServer = "VCENTER SERVER" 30 $cUser = "VMWARE ACCOUNT" 31 $cPassword = "VMWARE PASSWORD" 32 $LogFile = "C:\Scripts\PowerOn_VM_Logfile_"+$((get-date).tostring("MMddyyyyHHmmss"))+".txt" 33 34 ################################################################################### 35 # Set Scheduler, only run script if its in the schedule time 36 ################################################################################### 37 #[int]$hour = (Get-Date).Hour 38 #if ($hour -ge $TimeStart -and $hour -le $TimeEnd) { 39 # Continue running script 40 #} else { 41 # write-host "Cannot run script. Script can be run from $TimeStart to $TimeEnd AM" -ForegroundColo r Cyan 42 # break 43 #} 44 ################################################################################### 45 46 # Connect to server 47 Connect-VIServer -Server $cServer -User $cUser -Password $cPassword 48 $Check_VM_State = Get-vm $n 49 50 if ($n.Length -eq 0) { 51 write-host "Usage: PowerOn_VM.ps1 " 52 } else { 53 54 $Date = Get-Date 55 "======================" | Out-File $LogFile -Append 56 "$Date" | Out-File $LogFile -Append 57 "======================" | Out-File $LogFile -Append 58 #Ping Host 59 if (test-connection -computername $n -quiet) { 60 # Pingable 61 "VM: "+$n+" is pingable" | Out-File $LogFile -Append 62 if ($Check_VM_State.Powerstate -eq "PoweredOff") { 63 "Starting VM: "+$n | Out-File $LogFile -Append 64 Start-VM -VM $n 65 } else { 66 "Checking VMWare for vm state..." | Out-File $LogFile -Append 67 "Powerstate for "+$n+" is "+$Check_VM_State.Powerstate | Out-File $LogFile -Append 68 } 69 #Remove-Item $LogFile 70 } else { 71 # Not Pingable, check to see if VM is on, if not start it 72 "Cannot Ping: "+$n | Out-File $LogFile -Append 73 "Powerstate for "+$n+" is "+$Check_VM_State.Powerstate | Out-File $LogFile -Append 74 if ($Check_VM_State.Powerstate -eq "PoweredOff") { 75 # if VM is powered off: Start VM 76 "Starting VM: "+$n | Out-File $LogFile -Append 77 Start-VM -VM $n 78 } else { 79 # if VM is powered on but un-pingable: Restart VM 80 "VM "+$n+" already powered on but un-pingable" | Out-File $LogFile -Append 81 "Checking VMWare for vm state..." | Out-File $LogFile -Append 82 "Powerstate for "+$n+" is "+$Check_VM_State.Powerstate | Out-File $LogFile -Append 83 "Restarting "+$n+" " | Out-File $LogFile -Append 84 Restart-VM -VM $n -RunAsync 85 } 86 } 87 } 88 Disconnect-VIServer -Server $cServer -confirm:$false 89 #sleep 30 90 #Remove-Item $LogFile 91 stop-process -Id $PID 92 #break
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Powershell script to boot VMs that are off # used for Solarwinds Alerts # Derrick Rose # Get VM Node N...