Tripp Lite SV20KM1P1B Users Guide for PowerAlert Network Shutdown Agent PANSA - Page 18

Windows Script: shutdownvms.pl

Page 18 highlights

APPENDIX A - Sample Scripts Windows Script: shutdownvms.pl #!/usr/bin/perl -w # Use this script to shutdown ESXi Hosts in an HA Cluster from a # Windows machine outside of the cluster # Prerequisites for this script to work include the vSphere CLI being installed # and, if using version 6.5 or later of the vSphere CLI, a compatible version of Perl e.g. # ActivePerl or Strawberry Perl version 5.14 # Place this script at C:\Program Files (x86)\VMware\VMware vSphere CLI\bin and # call this script from PANSA or PAL # By default, most hosts will come back in Maintenance mode when powered back on after using # this script # Shut down the VMs

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

18
Windows Script: shutdownvms.pl
#!/usr/bin/perl -w
# Use this script to shutdown ESXi Hosts in an HA Cluster from a
# Windows machine outside of the cluster
# Prerequisites for this script to work include the vSphere CLI being installed
# and, if using version 6.5 or later of the vSphere CLI, a compatible version of Perl e.g.
# ActivePerl or Strawberry Perl version 5.14
# Place this script at C:\Program Files (x86)\VMware\VMware vSphere CLI\bin and
# call this script from PANSA or PAL
# By default, most hosts will come back in Maintenance mode when powered back on after using
# this script
# Shut down the VMs << Uses vmware-cmd.pl
# Place the hosts in Maintenance mode <<Uses vicfg-hostops.pl
# Shut down the hosts <<Uses vicfg-hostops.pl
# Shut down vCenter (if vCenter is a vm you will need to connect to each host separately and shut it down.)
# << This script connects to each host separately
#use strict;
use warnings;
use 5.14.4;
use VMware::VIRuntime;
use VMware::VILib;
use VMware::VIExt;
my $host;
my $state;
my $toolsstate;
my @vmxcmd;
my $vmxcmd;
my @vmxlist;
my $vmxlist;
my $statecmd;
my $stop;
#my $vmwareBIN = "C:\Program Files (x86)\VMware\VMware vSphere CLI\bin";
my $vmwareCMD = "vmware-cmd.pl";
my $vicfgHostops = "vicfg-hostops.pl";
# User input required- Credientals for ESXi Servers
# Be sure to change the username and password parameters before use
# use \ to escape special characters, e.g. \@
my $loginCMD = "-U xxxx -P xxxxxx";
# User input required- Credientals for ESXi Servers
# Be sure to change the username and password parameters before use
# use \ to escape special characters, e.g. \@
my $loginHostops = "--username xxxx --password xxxxxx";
# Designate the ESXi host(s) to shut down.
# User input required- Enter IP addresses for all ESXi hosts in the cluster.
my @hosts = ( "10.0.0.25","10.0.0.26","10.0.0.27","10.0.0.28");
foreach my $host (@hosts) {
#say $host;
@vmxlist=();
$vmxlist="";
$vmxcmd = "$vmwareCMD -H $host $loginCMD -l";
#say $vmxcmd;
$vmxlist = `$vmxcmd`.$vmxlist;
#say $vmxlist;
$vmxlist =~ s/^\n//;
#say $vmxlist;
@vmxlist = split /\n/, $vmxlist;
foreach $vmxlist (@vmxlist) {
#getState (on, off, suspended)
$statecmd = "$vmwareCMD -H $host $loginCMD \"$vmxlist\" getstate";
#say $statecmd;
$state = `$statecmd`;
#say $state;
if($state =~ /(on)$/) {
#getToolsState
$toolsstate = `"$vmwareCMD" -H $host $loginCMD \"$vmxlist\" gettoolslastactive`;
#say $toolsstate;
if($toolsstate =~ /(1)$/) {
#shutdown vm gracefully
$stop = `"$vmwareCMD" -H $host $loginCMD \"$vmxlist\" stop soft`;
}
else {
APPENDIX A – Sample Scripts