7

I'm trying to use the following drush script, but I keep getting errors. Currently, it is not properly setting the user role to Premium.

#!/user/bin/env drush
<?php  
  $users = [ 
      "name" => "email",
      "name" => "email",
      "name" => "email",
      "name" => "email",
      "name" => "email",
      "name" => "email",
      "name" => "email",
      "name" => "email",
  ];
  foreach ($users as $key => $val) {
      drush_invoke("user-create", $val);
      drush_print("Created account for email " . $val);
      drush_set_option("password", $key);
      drush_invoke('user-password', $val); 
      drush_print("Created password for email " . $val . " with password " . $key);
      drush_set_option("mail", $val);
      drush_invoke('user-add-role', "Premium members");
      drush_print("Added premium role to " . $val);
  }
?>

Is there a better way to achieve this?

2
  • what errors are you getting?
    – Shabir A.
    Dec 18, 2015 at 19:31
  • @YPCrumble Can you double check that you're using the right role name (machine name, not the title)? And the role already exists (role-create)? Probably you've to pass extra argument for user-add-role command to specify the username ($key).
    – kenorb
    Dec 18, 2015 at 19:41

4 Answers 4

3

If you are having trouble with drush_invoke, try drush_invoke_process, which has two advantages over drush_invoke:

  1. It is executed in a separate process, so you can set options explicitly, and do not need to worry about option "pollution" from previous calls to drush_set_option()
  2. The results of the operation, including all log messages, is returned as structured php data, so you have a better idea of what was done.

Example:

$values = drush_invoke_process("@self", "user-add-role", array("Premium members"), array('mail' => $val));

You will find the output of the command in $values['output'], and structured results (if available) in $values['object'].

Note that in general, there is nothing wrong with calling drush_invoke as you are doing above; you may use that technique if it works for your use case. It is likely to be faster, but may sometimes be fragile.

3

Yes, there is a way better way of using drush to achieve this and that is using drush php-script:

$ drush scr /path/to/create-users.php

Create create-users.php and put it somewhere:

<?php
/* 
 * Content of create-users.php (could be located anywhere on the system)
 */

$users = array(
    'name1' => 'name1@domain',
    'name2' => 'name2@domain',
    // ...
);    
$role_name = 'Premium members';

$role = user_role_load_by_name($role_name);
foreach ($users as $name => $email) {
  //set up the user fields
  $fields = array(
    'name' => $name,
    'mail' => $email,
    'pass' => user_password(),
    'status' => 1,
    'roles' => array(
        $role->rid => $role->name,
    ),
  );
  //the first parameter is left blank so a new user is created
  $account = user_save('', $fields);
}

(Code snippet from http://codekarate.com/blog/create-user-account-drupal-7-programmatically)

1

It looks like you've omitted the $val in your user-add-role invoke.

Drush won't know which user to add the role to!

You have

drush_invoke('user-add-role', "Premium members"); 

You should have

drush_invoke('user-add-role', "Premium members", $val); 

If that doesn't solve it, you next might need to check the handling of role name, since it contains a space. A quick test (drush user-add-role 'Some role' [email protected]) locally suggests that this should work OK if the Drush command gets the parameter with the space in it.

As far as "Is there a better way to achieve this?" goes, yes - you could be checking the results of each command and reporting back failures, rather than moving on to the next step. (If the user account already exists when you call drush user-create, your code may want to stop before changing the password for an existing account with drush user-password.)

0

As an alternative option, particularly handy when you many need to rerun with new users/updates, is to use feeds. Then upload/point to a CSV.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.