VMware Cloud Community
EXPRESS
Enthusiast
Enthusiast
Jump to solution

ShutDown / PowerOn VMs

Hi All,

I am hoping you guys can help me here. I am sure someone may even have done this already as well. What I am looking to do is to be able to run a script to be able to;

So what I would like to do is from windows:

1. Power Off a list of VM's at 8:00 pm on Saturdays (on a weekly basis)

2. Power On the list of VM's at 8:05 pm on Saturdays (on a weekly basis)

3. Send me an email if possible when completed

lamw sent me a script, but I need some help in setting it up see thread below. If someone has one I can use I would really appreciated. We have about 100 VM's...

http://communities.vmware.com/thread/232914

Thank you,

Express

Thank you, Express
0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok, the scheduled task should be the easy bit, just save your script as a ps1 file and then follow this: http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/

I normally make sure the cmdline works in a dos window first and then copy and paste it into the scheduled task.

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

0 Kudos
48 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Should be easy enough, where are you getting the list of vm's, all vms in one cluster, all vms on certain hosts or are you wanting to get the names from a csv file ?

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

I didn't know I had that many options! Well let me ask you what would be the easiest? I guess getting the names from a cvs file would be good this way if I dont want certain vm's not to shutdown or startup. So for instance I have 100 vms in one datacenter within three hosts running in a HA Cluster...

Virtual Center - esxvc1.express.com

Hosts names - esx1, esx2, esx3

vmnames - vmw-expres1, vmw-expres2, vmw-expres3

Let me know what other information you would need and THANKS so much for your help!

Thank you,

Express

Thank you, Express
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution


Please make sure you test this first ! -

Personaly I would keep it simple with something like this as a scheduled task for shutting down the VMs

Import-Csv "C:\test.csv" | Foreach {Shutdown-VMGuest -VM $_}


$mailer = new-object Net.Mail.SMTPclient("smtp.mydomain,com")
 $msg = new-object Net.Mail.MailMessage("Fromme@mydomain.com","Toyou@yourdomain.com","Shutdown VMs","The VMs have been shutdown")
 $mailer.send($msg)

And for the power on, a seperate script:

Import-Csv "C:\test.csv" | Foreach {Start-VM -VM $_}

$mailer = new-object Net.Mail.SMTPclient("smtp.mydomain,com")
$msg = new-object
Net.Mail.MailMessage("Fromme@mydomain.com","Toyou@yourdomain.com","Startup
VMs","The VMs have been Started")
 $mailer.send($msg)

You could also add some checks to make sure the VMs have shutdown/startup as all this is doing is telling them to shutdown/startup.

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

http://virtu-al.net

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Thanks for the superfast response!

Now here's another dumb question so do I take those two and creat a perl script for each one? So I am thinking something like this:

create a file called vmon.pl

#!/usr/bin/perl

Import-Csv "C:\test.csv" | Foreach {Shutdown-VMGuest -VM $_}

$mailer = new-object Net.Mail.SMTPclient("smtp.mydomain,com")

$msg = new-object Net.Mail.MailMessage("Fromme@mydomain.com","Toyou@yourdomain.com","Shutdown VMs","The VMs have been shutdown")

$mailer.send($msg)

Then to run it I just call the vmon.pl? Then if so I just schedule it with windows task scheduler? Also I am running this on a VM XP. Sorry for all the questions.

Thank you,

Express

Thank you, Express
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok, these are not perl scripts, they are PowerCLI scripts so you need to make sure you have powershell and Powercli installed on your windows box (http://www.virtu-al.net/2008/08/13/getting-started-with-the-vi-toolkit/), add these to a text file with an extension of .ps1 instead of .txt and then call the scheduled task like in the following blog post: http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Okay gotcha, I got that installed I actually thought those two were perl related. Okay I am going to test I will let you know.

Thank you,

Express

Thank you, Express
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Alrighty, before I did the schedule add-in I just wanted to test the .ps1 so what I did was from the c:\progr...\VMwa...\Infras...\vShphe PowerCli\Scripts> (I typed) powershell.exe poffvm.ps1 then I got the following error;

File C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\poffvm.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details. At line:1 char:10 + poffvm.ps1 <<<<

Thank you,

Express

Thank you, Express
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Aha, the wonderfull powershell default security policy, if you read the blog post above that tells you to install PowerCLI it also talks about setting your execution policy with set-executionpolicy cmdlet, i would read about this before choosing which is correct for your environment. Normally remotesigned is enough.

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Never easy huh. Quick question as I try and get that policy to work, I will be running this on a vm xp, VC is on a separate server... will this script still work as is?

Thank you,

Express

Thank you, Express
0 Kudos
harkamal
Expert
Expert
Jump to solution

Launch powershell and execute following command to enable scripts

Set-ExecutionPolicy RemoteSigned

To verify

Get-ExecutionPolicy

0 Kudos
harkamal
Expert
Expert
Jump to solution

Shutdown VM

Get-Content c:\myServerList.csv | % { Get-VM $_ | Shutdown-VMGuest -confirm:$false }

Start VM

Get-Content c:\myServerList.csv | % { Start-VM $_ }

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

yes, it doesnt matter where you launch it from just check the link which tells you how to run them as a scheduled task, for now you can use the connect-viserver cmdlet at the top of each script to connect to your VI server as so:

connect-viserver MyVIServer -User "MyUser" -Pass "MyPass"

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Thanks harkamal for that.

Thank you,

Express

Thank you, Express
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Ok, I inserted that line but now i get the following error:

Unexpected token 'connect-viserver' in expression or statement. At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\poffvm.ps1:1 char:23 + connect-viserver <<<< esxmansi1 -User express -Pass express123{code]

I really appreciate your time and help with this...

Thank you,

Express

Thank you, Express
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Damn forum, get rid of the {code... bit at the end so it is just:

connect-viserver "myviserver" -user "me" -pass "mypass"

or if your account you are logged in as has rights you can just do a connect-viserver "myviserver"

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Okay I got changed the following let me know if this is correct:

{code Connect-VIserver esxmansi1.express.com -User express -Pass Expres123 code}

Below for this line in the debugging it shows on the PowerGUI Script Editor a wavy line under the t in VMGuest

Import-Csv "C:\vmlist.csv" | Foreach {Shutdown-VMGuest -VM $_}

7163_7163.gif

The term 'Shutdown-VMGuest' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again. At :line:6 char:54 + Import-Csv "C:\vmlist.csv" | Foreach {Shutdown-VMGuest <<<< -VM $_}

Thank you,

Express

Thank you, Express
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

No 'code' or brackets, just the words. PowerGUI is great, good move. Looks like you havent activated the PowerCLI snapin though, go to File, Powershell libraries and tick the box for VMware.vim.....

This will enable powergui to understand the PowerCLI cmdlets.

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
EXPRESS
Enthusiast
Enthusiast
Jump to solution

Added the VMware.VimAutomatio snap-in, but now I am getting this:

Cannot bind parameter 'VM'. Cannot convert "" to "VMware.VimAutomation.Types.VirtualMachine".

At :line:5 char:59

+ Import-Csv "C:\vmlist.csv" | Foreach { Shutdown-VMGuest -VM <<<< $_}

This is the whole script...

connect-viserver esxmansi1.express.com -User express -Pass Expres123

Import-Csv "C:\vmlist.csv" | Foreach { Shutdown-VMGuest -VM$_}

$mailer = new-object Net.Mail.SMTPclient("dsasmtp.express.com")

$msg = new-object Net.Mail.MailMessage("express@express.com","express@express.com","Shutdown VMs","The VMs have been shutdown")

$mailer.send($msg)

- When I just ran it again I get

A parameter cannot be found that matches parameter name 'VM$_'.

At :line:5 char:61

+ Import-Csv "C:\vmlist.csv" | Foreach { Shutdown-VMGuest -VM$_ <<<< }

Thank you,

Express

Message was edited by: EXPRESS

Thank you, Express
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Whats in your csv file ? I had a header called VM then the machines listed underrneath that like this:

VM

TESTVM01

TESTVM02

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos