VMware

This Question is Answered

2 "helpful" answers available (6 pts)
5 Replies Last post: Oct 16, 2009 12:46 AM by LarsOeschey  

errorhandling? (Vim::login) posted: Oct 15, 2009 5:32 AM

Click to view LarsOeschey's profile Novice 9 posts since
Oct 9, 2009

Hi,

I'm trying to establish some errorhandling in my script to output to a logfile. Somehow I can't get this to work:

print LOG "login failed\n" if !(Vim::login(
service_url => $service_url,
user_name => $username,
password => $password));

in an error case I get the output on the console, but not in the logfile... How could that be done?

Lars

Re: errorhandling? (Vim::login)

1. Oct 15, 2009 6:57 AM in response to: LarsOeschey
Click to view lamw's profile Champion 2,803 posts since
Nov 27, 2007
Without seeing more of your code, it's hard to say what is wrong ..

Take a look on how to output to a file using Perl: http://www.troubleshooters.com/codecorn/littperl/perlfile.htm

=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at: http://engineering.ucsb.edu/~duonglt/vmware/
vGhetto Script Repository
VMware Code Central - Scripts/Sample code for Developers and Administrators
VMware Developer Comuunity
Twitter: @lamw

http://engineering.ucsb.edu/~duonglt/vmware/vexpert_silver_icon.jpg

If you find this information useful, please award points for "correct" or "helpful".

Re: errorhandling? (Vim::login)

3. Oct 15, 2009 9:09 AM in response to: LarsOeschey
Click to view lamw's profile Champion 2,803 posts since
Nov 27, 2007
Actually there are code and quote tags but you wrap them in "{" "}"

code tags


quote tags

Any reason you're not using the default Util::connect() and Util::disconnect() code that VMware already wrote in their perl modules?

You may want to take a look at this page http://www.vmware.com/support/developer/viperltoolkit/doc/perl_toolkit_guide.html to help you get started, much of the comand line parms and auth/connection code has been simply to help you get a script up and running much faster.


=========================================================================
William Lam
VMware vExpert 2009
VMware ESX/ESXi scripts and resources at: http://engineering.ucsb.edu/~duonglt/vmware/
vGhetto Script Repository
VMware Code Central - Scripts/Sample code for Developers and Administrators
VMware Developer Comuunity
Twitter: @lamw

http://engineering.ucsb.edu/~duonglt/vmware/vexpert_silver_icon.jpg

If you find this information useful, please award points for "correct" or "helpful".

Re: errorhandling? (Vim::login)

4. Oct 15, 2009 2:04 PM in response to: LarsOeschey
Click to view stumpr's profile Expert 451 posts since
Sep 26, 2007
You could use eval blocks:


eval {
     Vim::login( service_url=> $service_url, user_name => $username, password => $password);
};

if ($@) {
     # A fault was thrown
     print LOG $@ . "\n";

     # ...any other error handling...
}



You can also do string comparisons against the $@ variable to see if it matches a fault. For example, login could throw "InvalidLogin - Thrown if the user and password combination is invalid." You could use code like:


if ( $@ =~ m/InvalidLogin/gi ) {
      print LOG "Invalid Login ($@)\n";
}
else {
      print LOG "Login Error: ($@)\n";
}



This would let you handle specific faults if you wanted to recover or trigger another code path as a result.

Developer Social Media

Communities