using commvault/snapprotect we have very nice backups but more often than not there remain inaccessible nas datastores behind and I got tired of removing them from the esxi cli.
So I found the vicfg-nas script in the perl sdk installation and all pieces were there to solve my problem. Thanks for that script!
This is what I came up with:
#!/usr/bin/env perl
# Disable SSL hostname verification for vCenter self-signed certificate
BEGIN {
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
}
use strict;
use warnings;
use VMware::VIRuntime;
use VMware::VILib;
use VMware::VIExt;
my %opts = (
vihost => {
alias => "h",
type => "=s",
help => "The host to use when connecting via a vCenter Server",
required => 0,
},
list => {
alias => "l",
type => "",
help => "List the currently mounted NAS file systems",
required => 0,
},
_default_ => {
type => "=s",
argval => "label",
help => "The label for the NAS datastore",
required => 0,
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
my $label = Opts::get_option('_default_');
my $host = Opts::get_option('nasserver');
my $delete = Opts::get_option('delete');
my $list = Opts::get_option('list');
Util::connect();
my $host_view = VIExt::get_host_view(1, ['configManager.datastoreSystem']);
Opts::assert_usage(defined($host_view), "Invalid host.");
my $datastore_system =
Vim::get_view (mo_ref => $host_view->{'configManager.datastoreSystem'});
if (defined $list) {
list_nas($datastore_system);
} else {
Opts::usage();
exit 1;
}
Util::disconnect();
sub list_nas {
my ($dssys) = @_;
my $datastores = $dssys->datastore;
foreach my $dsRef (@$datastores) {
my $ds = Vim::get_view (mo_ref => $dsRef);
if ($ds->info->isa("NasDatastoreInfo")) {
next if $ds->summary->accessible == 1;
print $ds->info->name,"\n";
$dssys->RemoveDatastore(datastore => $dsRef);
}
}
}
So this snippet will remove inaccessible datastores ( accessible == 1 means the datastore is accessible, so we skip those in the for loop) on the esx hosts. We do not prompt for removing, so use at your own risk :-)
Usage:
perl esx-nas-datastores --server esxhost --list --username user_with_privs_to_remove_datastore ---password pwd
I should probable rename the --list switch to remove_ds or something like that :-)