Shutdown VM

#
# This script reads VM server list from a TXT file and power them off
# The power off commands returns immediately without waiting for the task to complete (RunAsync)

# TXT File Location: C:\Temp\vm_shutdown.txt (one server per row)
#
# Log Location: C:\Temp\ShutdownVM_log.txt
#


$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\Temp\ShutdownVM_log.txt -append


$vCenterIP = Read-Host "Enter the VC IP Address/Name "
$vCenterUser = Read-Host "Enter User name"
$vCenterUserPassword = Read-Host -assecurestring "Enter Password"
$vCenterUserPassword =  [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($vCenterUserPassword))

Connect-viserver ${vCenterIP} -user ${vCenterUser} -password $vCenterUserPassword -WarningAction 0 | out-null

$VMList = @()
$VMList = Get-Content C:\Temp\vm_shutdown.txt

Write-host ""
Write-host "***********************************************"
Write-host "Shut-down task started at $(Get-Date)"
Write-host "***********************************************"
Write-host ""
    
foreach($vmName in $VMList){
    $vm = Get-DataCenter "BeiJing" | Get-VM -Name $vmName
    Shutdown-VMGuest -VM $vm -confirm:$false
    Write-host "Shutting down VM " $vmName
}

Write-host ""
Write-host "***********************************************"
Write-host "Shut-down task completed at $(Get-Date)"
Write-host "***********************************************"
Write-host ""

Disconnect-viserver ${vCenterIP} -Confirm:$false #| out-null

Stop-Transcript

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Power on VM

#
# This script reads VM server list from a TXT file and power them on

# TXT File Location: C:\Temp\vm_startup.txt (one server per row)
#
# Log Location: C:\Temp\PowerOnVM_startup_log.txt
#


$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\Temp\PowerOnVM_startup_log.txt -append


$vCenterIP = Read-Host "Enter the VC IP Address/Name"
$vCenterUser = Read-Host "Enter User name"
$vCenterUserPassword = Read-Host -assecurestring "Enter Password"
$vCenterUserPassword =  [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($vCenterUserPassword))

Connect-viserver ${vCenterIP} -user ${vCenterUser} -password $vCenterUserPassword -WarningAction 0 | out-null

$VMList = @()
$VMList = Get-Content C:\Temp\vm_startup.txt

Write-host ""
Write-host "***********************************************" -ForegroundColor Green
Write-host "Power-on task started at $(Get-Date)" -ForegroundColor Green
Write-host "***********************************************" -ForegroundColor Green
    
foreach($vmName in $VMList){
    $vm = Get-DataCenter "Beijing" | Get-VM -Name $vmName
    Start-VM -VM $vm -confirm:$false
}

Write-host ""
Write-host "***********************************************" -ForegroundColor Green
Write-host "Power-on task completed at $(Get-Date)" -ForegroundColor Green
Write-host "***********************************************" -ForegroundColor Green
Write-host ""

Disconnect-viserver ${vCenterIP} -Confirm:$false #| out-null

Stop-Transcript