Shutting down VM using PowerCli
Need to shutdown a large number of VM's? This comes in handy. This is my first powercli script using others work as reference so it may not be pretty, but it gets the job done.Prepare a server list
Put the VM's name in a text file. A vm name is the first column when you export a list of VM from vcenter:non-prod-vm.list
machine1
machine2
...
machine99
machine2
...
machine99
Prepare a powercli script
shutdownvm.ps1
#Define VC connection variables
$server = "1.2.3.4"
$user = "administrator"
$pass = "your-password-here"
#Connect to VC
Connect-VIServer -Server $server -Protocol https -User $user -Password $pass | Out-Null
$Machineslist = Gc "c:\pcli\non-prod-vm.list"
#Invoking Scripts on VMs
ForEach($VM In $Machineslist) {
echo "Shutting down $VM"
Shutdown-VMGuest -VM $VM -Confirm:$false
}
#Disconnect from VC
Disconnect-VIServer -Server $server -Force:$true -Confirm:$false
$server = "1.2.3.4"
$user = "administrator"
$pass = "your-password-here"
#Connect to VC
Connect-VIServer -Server $server -Protocol https -User $user -Password $pass | Out-Null
$Machineslist = Gc "c:\pcli\non-prod-vm.list"
#Invoking Scripts on VMs
ForEach($VM In $Machineslist) {
echo "Shutting down $VM"
Shutdown-VMGuest -VM $VM -Confirm:$false
}
#Disconnect from VC
Disconnect-VIServer -Server $server -Force:$true -Confirm:$false
Prepare a batch file to call the powercli script
And redirect output to a log file. Run the batch file, sit back and let it do the job for you.shutdownvm.cmd
cd c:\pcli
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& ".\shutdownvm.ps1 "" | c:\pcli\wtee shutdownvm.log
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& ".\shutdownvm.ps1 "" | c:\pcli\wtee shutdownvm.log
There are no comments on this page. [Add comment]