Contributor
Contributor

VIC power on and off VMs from the command line ...

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
Hot Shot
Hot Shot

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

#

  1. Copyright 2006 VMware, Inc. All rights reserved.

#

  1. This script allows users to perform power operations on a VM.

  2. Supported operations are poweron, poweroff, suspend, reset, reboot, shutdown and

  3. standby. For more information, execute the script with --help option.

#

  1. Examples:

  2. powerops.pl --operation poweron --vmname MyVM

  3. 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();

  1. 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);

  1. 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) {

  1. power on

if( $op eq "poweron" ){

  1. if($_->runtime->powerState->val ne 'poweredOff' &&

  2. $_->runtime->powerState->val ne 'suspended' ){

  3. print "The current state of the VM " . $_->name . " is ".

  4. $_->runtime->powerState->val." ".

  5. "The poweron operation is not supported in this state\n";

  6. next ;

  7. }

  8. print "Powering on " . $_->name . "\n";

$_->PowerOnVM();

print "Poweron successfully completed\n";

}

  1. 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";

}

  1. 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";

}

  1. 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";

}

  1. 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";

}

  1. 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";

}

  1. 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();

View solution in original post

0 Kudos
12 Replies
Immortal
Immortal

This should help

Also, there is no command line from the VIC, you will have to be at the console to run these commands.

Contributor
Contributor

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

0 Kudos
Hot Shot
Hot Shot

You could install the VI Perl Toolkit on the VC server and create start and stop scripts and run them from the command line.

0 Kudos
Immortal
Immortal

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

0 Kudos
Contributor
Contributor

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

0 Kudos
Champion
Champion

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

http://blog.laspina.ca/ vExpert 2009
Hot Shot
Hot Shot

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

#

  1. Copyright 2006 VMware, Inc. All rights reserved.

#

  1. This script allows users to perform power operations on a VM.

  2. Supported operations are poweron, poweroff, suspend, reset, reboot, shutdown and

  3. standby. For more information, execute the script with --help option.

#

  1. Examples:

  2. powerops.pl --operation poweron --vmname MyVM

  3. 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();

  1. 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);

  1. 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) {

  1. power on

if( $op eq "poweron" ){

  1. if($_->runtime->powerState->val ne 'poweredOff' &&

  2. $_->runtime->powerState->val ne 'suspended' ){

  3. print "The current state of the VM " . $_->name . " is ".

  4. $_->runtime->powerState->val." ".

  5. "The poweron operation is not supported in this state\n";

  6. next ;

  7. }

  8. print "Powering on " . $_->name . "\n";

$_->PowerOnVM();

print "Poweron successfully completed\n";

}

  1. 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";

}

  1. 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";

}

  1. 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";

}

  1. 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";

}

  1. 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";

}

  1. 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();

View solution in original post

0 Kudos
Immortal
Immortal

Is this what you are looking for? If so, you should be able to right click on any VM and power off/on. Or look under the commands option and you will see these

0 Kudos
Contributor
Contributor

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

0 Kudos
Contributor
Contributor

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.

0 Kudos
Hot Shot
Hot Shot

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.

0 Kudos
Contributor
Contributor

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

0 Kudos