Hello,
I want to be able to power off and power back on a vm from the VIC using the command line.
We are a ESX3.0.1 environment with the VIC being a Windows 2003 Server and the VMs being Windows 2003 Servers as well.
Thanks for your help in advance.
Andy
Here is a sample script that comes with the toolkit. It will perform power ops on a vm. You can run it from any machine that supports the toolkit.
#!/usr/bin/perl -w
#
Copyright 2006 VMware, Inc. All rights reserved.
#
This script allows users to perform power operations on a VM.
Supported operations are poweron, poweroff, suspend, reset, reboot, shutdown and
standby. For more information, execute the script with --help option.
#
Examples:
powerops.pl --operation poweron --vmname MyVM
powerops.pl --operation poweroff --filter "config.guestFullName=Windows.*"
use strict;
use warnings;
use VMware::VIM2Runtime;
use VMware::VILib;
my %opts = (
'operation' => {
type => "=s",
help => "The power operation to perform on the virtual machine(s) specified",
required => 1,
},
'vmname' => {
type => "=s",
help => "The name of the virtual machine",
required => 0,
},
'filter' => {
type => "=s",
help => "The filter used to select matching virtual machines",
required => 0,
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Additional validation
if (!defined (Opts::get_option('filter') || Opts::get_option('vmname'))) {
print "ERROR: --filter or --vmname must be specified\n\n";
&help();
exit (1);
}
my ($property_name, $property_value);
Filter can be user provided. For example, --filter "config.guestFullName=Windows*"
if( !defined (Opts::get_option('filter')) ){
$property_name = "config.name";
$property_value = Opts::get_option('vmname');
}
elsif (defined (Opts::get_option('filter'))){
($property_name, $property_value) = split ("=", Opts::get_option('filter'));
}
Util::connect();
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine',
filter => {$property_name => $property_value});
my $op = Opts::get_option('operation');
foreach (@$vm_views) {
power on
if( $op eq "poweron" ){
if($_->runtime->powerState->val ne 'poweredOff' &&
$_->runtime->powerState->val ne 'suspended' ){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The poweron operation is not supported in this state\n";
next ;
}
print "Powering on " . $_->name . "\n";
$_->PowerOnVM();
print "Poweron successfully completed\n";
}
reset
elsif( $op eq "reset" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The reset operation is not supported in this state\n";
next ;
}
print "Resetting the VM " . $_->name . "\n";
$_->ResetVM();
print "Reset successfully completed\n";
}
standby
elsif( $op eq "standby" ){
if($_->runtime->powerState->val ne 'poweredOn' ){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The standby operation is not supported in this state\n";
next ;
}
print "Standby VM " . $_->name . "\n";
$_->StandbyGuest();
print "Standby successfully completed\n";
}
power off
elsif( $op eq "poweroff" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The poweroff operation is not supported in this state\n";
next ;
}
print "Powering off " . $_->name . "\n";
$_->PowerOffVM();
print "Poweroff successfully completed\n";
}
suspend
elsif( $op eq "suspend" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The suspend operation is not supported in this state\n";
next ;
}
print "Suspending VM " . $_->name . "\n";
$_->SuspendVM();
print "Suspend successfully completed\n";
}
soft shutdown
elsif( $op eq "shutdown" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The shutdown operation is not supported in this state\n";
next ;
}
print "Shutting down VM " . $_->name . "\n";
$_->ShuddownGuest();
print "Shutdown successfully completed\n";
}
reboot
elsif( $op eq "reboot" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The reboot operation is not supported in this state\n";
next ;
}
print "Rebooting VM " . $_->name . "\n";
$_->RebootGuest();
print "Reboot successfully completed\n";
}
else{
die "\nInvalid argument --operation: '$op'";
}
}
Util::disconnect();
Troy,
Thank you for your help on this. The manual is really nice to have, however I am looking for some way where I can run commands from our VIC (Windows 2003 Server) to shutdown and start the vms.
From the VIC GUI interface I can click on the stop, start and pause buttons. However I am looking for some way to do this from the command line instead.
Thanks for your help in advance,
Andrew
You could install the VI Perl Toolkit on the VC server and create start and stop scripts and run them from the command line.
I'm not sure I understand your question. The VIC is a GUI interface, there are no command line requests you can run from the VIC.
All command line requests have to be run at the console. you can use Putty to access the console and run them from your desk, if that is what you are asking. If you don't have Putty you can download it here
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Ross,
I am not familiar with the VI Perl Toolkit. Will this give you the ability to stop and start vms from the VIC?
thx
Hello,
Yes it will and much much more. It's not all that hard, there are lot's of samples.
Check it out at.
http://www.vmware.com/support/developer/viperltoolkit/doc/perl_toolkit_guide.html
Here is a sample script that comes with the toolkit. It will perform power ops on a vm. You can run it from any machine that supports the toolkit.
#!/usr/bin/perl -w
#
Copyright 2006 VMware, Inc. All rights reserved.
#
This script allows users to perform power operations on a VM.
Supported operations are poweron, poweroff, suspend, reset, reboot, shutdown and
standby. For more information, execute the script with --help option.
#
Examples:
powerops.pl --operation poweron --vmname MyVM
powerops.pl --operation poweroff --filter "config.guestFullName=Windows.*"
use strict;
use warnings;
use VMware::VIM2Runtime;
use VMware::VILib;
my %opts = (
'operation' => {
type => "=s",
help => "The power operation to perform on the virtual machine(s) specified",
required => 1,
},
'vmname' => {
type => "=s",
help => "The name of the virtual machine",
required => 0,
},
'filter' => {
type => "=s",
help => "The filter used to select matching virtual machines",
required => 0,
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Additional validation
if (!defined (Opts::get_option('filter') || Opts::get_option('vmname'))) {
print "ERROR: --filter or --vmname must be specified\n\n";
&help();
exit (1);
}
my ($property_name, $property_value);
Filter can be user provided. For example, --filter "config.guestFullName=Windows*"
if( !defined (Opts::get_option('filter')) ){
$property_name = "config.name";
$property_value = Opts::get_option('vmname');
}
elsif (defined (Opts::get_option('filter'))){
($property_name, $property_value) = split ("=", Opts::get_option('filter'));
}
Util::connect();
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine',
filter => {$property_name => $property_value});
my $op = Opts::get_option('operation');
foreach (@$vm_views) {
power on
if( $op eq "poweron" ){
if($_->runtime->powerState->val ne 'poweredOff' &&
$_->runtime->powerState->val ne 'suspended' ){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The poweron operation is not supported in this state\n";
next ;
}
print "Powering on " . $_->name . "\n";
$_->PowerOnVM();
print "Poweron successfully completed\n";
}
reset
elsif( $op eq "reset" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The reset operation is not supported in this state\n";
next ;
}
print "Resetting the VM " . $_->name . "\n";
$_->ResetVM();
print "Reset successfully completed\n";
}
standby
elsif( $op eq "standby" ){
if($_->runtime->powerState->val ne 'poweredOn' ){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The standby operation is not supported in this state\n";
next ;
}
print "Standby VM " . $_->name . "\n";
$_->StandbyGuest();
print "Standby successfully completed\n";
}
power off
elsif( $op eq "poweroff" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The poweroff operation is not supported in this state\n";
next ;
}
print "Powering off " . $_->name . "\n";
$_->PowerOffVM();
print "Poweroff successfully completed\n";
}
suspend
elsif( $op eq "suspend" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The suspend operation is not supported in this state\n";
next ;
}
print "Suspending VM " . $_->name . "\n";
$_->SuspendVM();
print "Suspend successfully completed\n";
}
soft shutdown
elsif( $op eq "shutdown" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The shutdown operation is not supported in this state\n";
next ;
}
print "Shutting down VM " . $_->name . "\n";
$_->ShuddownGuest();
print "Shutdown successfully completed\n";
}
reboot
elsif( $op eq "reboot" ){
if($_->runtime->powerState->val ne 'poweredOn'){
print "The current state of the VM " . $_->name . " is ".
$_->runtime->powerState->val." ".
"The reboot operation is not supported in this state\n";
next ;
}
print "Rebooting VM " . $_->name . "\n";
$_->RebootGuest();
print "Reboot successfully completed\n";
}
else{
die "\nInvalid argument --operation: '$op'";
}
}
Util::disconnect();
Looks like I need to get very familiar with Perl. I am quite familiar with regular DOS batch and KSH, but will now have to get familiar with Perl.
Thanks
Ross,
Would there be any way to use this within a DOS batch script for example;
powerops.pl --operation poweron --vmname MY_VM
.
.
powerops.pl --operation poweroff --vmname MY_VM
Thanks for your help,
I wish that I could assign more points to everyone that has helped with this. Doesn't seem fair to assign points to only 3 people.
I think your batch file will need to look something like this:
perl "C:\{path to perl script}\powerops.pl" --operation poweron --vmname --username --password --server
note:
The server variable can be left out if you are using it on the VC server itself.
Ross & everyone else on this posting. It is very much appreciated. I wish that there was more that I could do to submit points.
Andrew