I am trying to create a script that, in the end, should run unattended (not 100% sure how to make this happen yet). I created a configuration file that holds information for the server, username, and password using common options. I tried to use VI_CONFIG in my script to point to the configuration file so the user would not be prompted while the program was running. I cannot seem to make it work. When I try to add VI_CONFIG an error always comes up: "Cant modify constant item in scalar assignment". Any help would be greatly appreciated. I attached what I have so far below.
So first off, you'll probably want to reference both the programmers guide and toolkit guide: which provides information on how to start writing your own scripts and some methods to implement to make it easier to writing your own custom scripts.
The VI_CONFIG file you're referring to is generally stored as a hidden file which can be named anything, in my example I've named it .viconfig which will hold a few variables:
[vi-admin@scofield ~]$ cat .viconfig VI_SERVER=reflex.primp-industries.com VI_USERNAME=primp VI_PASSWORD=mysecurepassword!
You have the option to store other variables such as the VI_URL, VI_PROTOCOL, etc. though at a minimal for an unattended execution you'll need at least the server, which in this case can be either vCenter Server or ESX(i) server, the proper username/password to login to the systems. VMware has already pre-built some subroutines to automatically parse and validate the various credentials that can provided by default, so no need to reinvent the wheel. Just use what's there, which can be specified on the command line with --config and the path to the configuration file.
Okay, on to the script, again there are lots of scripts that have been pre-written which you can follow from /usr/lib/vmware-vcli/apps/ or /usr/lib/vmware-viperl/apps/ and you'll probably find the vm directory to be quite useful. In your script, you're looking to shutdown and suspend a list of VMs, based on the function of choice, you're doing a hard power off versus a soft guest shutdown. The latter requires that you have VMware Tools installed, else you'll get a soap fault. Nonetheless, it can be handled and caught by doing a force power off, but based on your initial script I've slightly re-written and hopefully it gives you an idea on how to go about writing this script.
Here is a sample run using a configuration file w/o having to provide any credentials:
VM list
[vi-admin@scofield ~]$ cat vmList Quentin VCAP
Suspend
[vi-admin@scofield ~]$ ./vmPowerOps.pl --config .viconfig --operation suspend --vmfile vmList
Trying to suspend Quentin
Successfully suspended Quentin
Trying to suspend VCAP
Successfully suspended VCAP
Shutdown
[vi-admin@scofield ~]$ ./vmPowerOps.pl --config .viconfig --operation poweroff--vmfile vmList
Trying to poweroff Quentin
Successfully poweredoff Quentin
Trying to poweroff VCAP
Successfully poweredoff VCAP
If you're looking to do a proper guest shutdown, take a look at ShutdownGuest
Happy scripting! ![]()
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
![]()
If you find this information useful, please award points for "correct" or "helpful".
So first off, you'll probably want to reference both the programmers guide and toolkit guide: which provides information on how to start writing your own scripts and some methods to implement to make it easier to writing your own custom scripts.
The VI_CONFIG file you're referring to is generally stored as a hidden file which can be named anything, in my example I've named it .viconfig which will hold a few variables:
[vi-admin@scofield ~]$ cat .viconfig VI_SERVER=reflex.primp-industries.com VI_USERNAME=primp VI_PASSWORD=mysecurepassword!
You have the option to store other variables such as the VI_URL, VI_PROTOCOL, etc. though at a minimal for an unattended execution you'll need at least the server, which in this case can be either vCenter Server or ESX(i) server, the proper username/password to login to the systems. VMware has already pre-built some subroutines to automatically parse and validate the various credentials that can provided by default, so no need to reinvent the wheel. Just use what's there, which can be specified on the command line with --config and the path to the configuration file.
Okay, on to the script, again there are lots of scripts that have been pre-written which you can follow from /usr/lib/vmware-vcli/apps/ or /usr/lib/vmware-viperl/apps/ and you'll probably find the vm directory to be quite useful. In your script, you're looking to shutdown and suspend a list of VMs, based on the function of choice, you're doing a hard power off versus a soft guest shutdown. The latter requires that you have VMware Tools installed, else you'll get a soap fault. Nonetheless, it can be handled and caught by doing a force power off, but based on your initial script I've slightly re-written and hopefully it gives you an idea on how to go about writing this script.
Here is a sample run using a configuration file w/o having to provide any credentials:
VM list
[vi-admin@scofield ~]$ cat vmList Quentin VCAP
Suspend
[vi-admin@scofield ~]$ ./vmPowerOps.pl --config .viconfig --operation suspend --vmfile vmList
Trying to suspend Quentin
Successfully suspended Quentin
Trying to suspend VCAP
Successfully suspended VCAP
Shutdown
[vi-admin@scofield ~]$ ./vmPowerOps.pl --config .viconfig --operation poweroff--vmfile vmList
Trying to poweroff Quentin
Successfully poweredoff Quentin
Trying to poweroff VCAP
Successfully poweredoff VCAP
If you're looking to do a proper guest shutdown, take a look at ShutdownGuest
Happy scripting! ![]()
=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at:
VMware Code Central - Scripts/Sample code for Developers and Administrators
![]()
If you find this information useful, please award points for "correct" or "helpful".
