Quantcast
Channel: VMware Communities : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 180259

Help! Why does my script crash my vSphere server?

$
0
0

I have to be doins something wrong. I'm not sure what. I'm very new to the API and I'm piecing a script to create VMs together. I can't use the standard one because I have different requirements (SAN clusters, and distributed vswitches). Besides, writing scripts is fun

 

So, this is the first try at creating a new guest. Whenever I run it vSphere server crashes completely. My vSphere Server is version 5.0.0.29542.

 

Any thoughts on why it crashed? Attached is the script.

 

Thanks,

Scott

 

 

 

#!/usr/bin/perl
use strict;
use VMware::VIRuntime;
use VMware::VILib;
use Data::Dumper;
# Connect to vSphere and collect information
Opts::parse();
Opts::validate();
Util::connect();
my $dscluster = Vim::find_entity_view(view_type => 'StoragePod', filter => {'name' => "MySAN" }, properties => ['name']);
if ( ! defined( $dscluster ) ) {
        print "Unable to locate datastore cluster: $dscluster!\n";
        Util::disconnect();
        exit 1;
}
my $folder = Vim::find_entity_view(view_type => 'Folder', filter => {'name' => "RandomFolder"}, properties => ['name']);
if ( ! defined( $folder ) ) {
        print "Unable to find folder to use!\n";
        Util::disconnect();
        exit 1;
}
# Define the new VM
my @vm_devices;
my $configSpec = VirtualMachineConfigSpec->new(
                                             name => "test-vm",
                                             memoryMB => 2048,
                                             numCPUs => 2,
                                             deviceChange => \@vm_devices);
# We only have one resource pool. Use that one for the storageplacementspec
my $resourcePools = Vim::find_entity_views( view_type => "ResourcePool" );
# Create a storage placement for the host to pass to recommenddatastores
my $storageMgr = Vim::get_view(mo_ref => Vim::get_service_content()->storageResourceManager);
my $podSpec = StorageDrsPodSelectionSpec->new(storagePod => $dscluster);
my $storageSpec = StoragePlacementSpec->new( type => "create",
                                             podSelectionSpec => $podSpec,
                                             folder => $folder,
                                             configSpec => $configSpec,
                                             resourcePool => $resourcePools->[0]
   );
# Figure out which datastore to put the guest on. Pick the VM that will have the
# most space free at the end
my $result = $storageMgr->RecommendDatastores(storageSpec => $storageSpec);
my $curRec;
my $curRecFree = 100;
foreach my $rec ( @{$result->recommendations} ) {
    if ( $rec->action->[0]->spaceUtilAfter < $curRecFree ) {
        $curRecFree = $rec->action->[0]->spaceUtilAfter;
        $curRec = $rec->key;
    }
}
# Create the VM
$storageMgr->ApplyStorageDrsRecommendation_Task( key => [ $curRec ] );

 


Viewing all articles
Browse latest Browse all 180259

Trending Articles