VMware {code} Community
MR-Z
VMware Employee
VMware Employee

changing host password via perl SDK?

Can anybody provide a quick pointer on how to change the root password using the perl SDK?

It appears that I need to create a hostConfigSpec (with userAccount property) and call applyHostConfig_Task to achieve that. But I am having a hard time figuring out the structure of hostconfigSpec piece...

In addition, if I want to only get the properties needed to speed up the query, what properties should I filter on?

$hosts = Vim::find_entity_views(view_type => 'HostSystem', properties => ['name','config'], filter => { name => qr/$hostName/});

Thanks!

0 Kudos
1 Reply
Dan_Perkins
Contributor
Contributor

A method I figured out was using the host accountManager, this requires a direct connection to the host and not through vCenter.

Also please note this example has zero error checking or handling of any error conditions.

It should be several times longer, if it did. Also note, since it uses the root account to authenticate it validly assumes the account exists. If you are changing other accounts passwords you will want to check the accounts exist.

===============================================

#!/usr/bin/perl

use strict;

use warnings;

use VMware::VIRuntime;

my $hostname = 'host.example.com';

my $username = 'root';

my $oldpass = 'oldpass';

my $newpass = 'newpass';

my $host_vim = Vim->new(service_url => "https://".$hostname."/sdk/webServer");

$host_vim->login(user_name => $username, password => $oldpass);

my $updateUserSpec = HostAccountSpec->new(id => $username, password => $newpass);

my $hostSc = $host_vim->get_service_content();

my $accountManager = $host_vim->get_view( mo_ref => $hostSc->accountManager);

$accountManager->UpdateUser(user => $updateUserSpec);

$host_vim->logout();

exit 0;

0 Kudos