Jeremie_R
Contributor
Contributor

Snapshot vm list via txt or csv file

Jump to solution

Hello ,

I want to create a workflow to create a snapshot of vm list, this list of vm must be a txt or csv file.

the file will be ask when I run the workfow (browse file) or a UNC path already configured in the workflow.

someone have an idea ?

0 Kudos
1 Solution

Accepted Solutions
Burke-
VMware Employee
VMware Employee

Here's some sample code from a workflow I wrote for vCloud DIrector 1 that imported users from a csv file that was uploaded to the vCO server. The variable "csvfile" is my MimeAttachment input, the server was configured to allow read/write in the c:\orchestrator folder....

var csvContent = csvFile.content;

var csvFileName = "c:\\Orchestrator\\csvFile.csv";

csvFile.write("c:\\Orchestrator\\","csvFile.csv");

System.log("csvFileName: "+csvFileName);

var host = organization.parent;

// Now use file to create FileReader object:

var fileReader = new FileReader(csvFileName);

if (fileReader.exists){

    fileReader.open();

    var line=fileReader.readLine();

    System.log("Headers: \n"+line);

    line = fileReader.readLine();

    while (line != null){

        var lineValues = line.split(",");

        var username = lineValues[0];

var username = lineValues[0];

        var password = lineValues[1];

        var enabled = lineValues[2];

        var role = lineValues[3];

        var fullname = lineValues[4];

        var email = lineValues[5];

        var phone = lineValues[6];

        var im = lineValues[7];

        var storedvmquota = lineValues[8];

        var runningvmquota = lineValues[9];

        var vcdRole = System.getModule("com.vmware.pso.vcd.roles").getRoleByName(host,role);

        System.log("Role name found: "+vcdRole.name);

        System.log("Attempting to create User Params");

        var userParams = System.getModule("com.vmware.library.vCloud.Admin.User").createUserParams(

                    username, fullname, enabled, false,

                    email, phone, null, password, vcdRole,

                    false, runningvmquota,

                    false, storedvmquota

                 );

        var userOut = System.getModule("com.vmware.library.vCloud.Admin.User").createUser(organization, userParams);

        userOut.im = im;

        userOut.update();

        System.log(userOut.name + " imported...");

        line = fileReader.readLine();

    }

    fileReader.close();

    // now cleanup by deleting the uploaded file from the server

    var file = new File(csvFileName);

    System.log("Filename: "+file.name);

    deleteFile(csvFileName);

}else{

    System.log("Error! File not found! "+csvFileName);

}

function deleteFile(fileName) {

    var file = new File(fileName);

       if (file.exists) {

               file.deleteFile();

          }

}

And here's sample code that retrieves a VC:VirtualMachine object by vm name where "vmName" is the String input containing the Name to search for and "vCenterVM" is the resulting VC:VirtualMachine object:

var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name='"+vmName+"'");

var vCenterVM = null;

if (vms != null){

    if (vms.length == 1){

        System.log("Match found for vm named: "+vmName);

        vCenterVM = vms[0];

    }else{

        System.log("More than one VM found with that name! "+vmName);

        for each (vm in vms){

            System.log("VM ID: "+vm.id);

        }

    }

}

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter

View solution in original post

0 Kudos
4 Replies
Burke-
VMware Employee
VMware Employee

Create a workflow that takes an input of MimeAttachment - this will let you upload the file as an input.

Once vCO has the file, get the content, and work your way through each item in the list...

For each item, use an action that retrieve a VC:VirtualMachine object by name...

Once you have the VC:VirtualMachine object, create your snapshot...

Rinse and Repeat Smiley Wink

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos
markdjones82
Expert
Expert

EDIT: Just noticed this is in orchestrator.  So, probably won't help you, but if you want to do in powercli here is how you can do it.

In powercli you can do something like this for a .txt file with the names of the servers:

$vmlist = Read-Host "Enter UNC path to file location"

$vmlist = Get-Content $vmlist

foreach ($vm in $vmlist) {

     New-Snapshot -VM $vm -Name NameofSnap

}

You need to be connected to the Vcenter server, but you can also add that into your script by doing:

Connect-Viserver -Server Vcentername

Message was edited by: Mark Jones

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
Burke-
VMware Employee
VMware Employee

Here's some sample code from a workflow I wrote for vCloud DIrector 1 that imported users from a csv file that was uploaded to the vCO server. The variable "csvfile" is my MimeAttachment input, the server was configured to allow read/write in the c:\orchestrator folder....

var csvContent = csvFile.content;

var csvFileName = "c:\\Orchestrator\\csvFile.csv";

csvFile.write("c:\\Orchestrator\\","csvFile.csv");

System.log("csvFileName: "+csvFileName);

var host = organization.parent;

// Now use file to create FileReader object:

var fileReader = new FileReader(csvFileName);

if (fileReader.exists){

    fileReader.open();

    var line=fileReader.readLine();

    System.log("Headers: \n"+line);

    line = fileReader.readLine();

    while (line != null){

        var lineValues = line.split(",");

        var username = lineValues[0];

var username = lineValues[0];

        var password = lineValues[1];

        var enabled = lineValues[2];

        var role = lineValues[3];

        var fullname = lineValues[4];

        var email = lineValues[5];

        var phone = lineValues[6];

        var im = lineValues[7];

        var storedvmquota = lineValues[8];

        var runningvmquota = lineValues[9];

        var vcdRole = System.getModule("com.vmware.pso.vcd.roles").getRoleByName(host,role);

        System.log("Role name found: "+vcdRole.name);

        System.log("Attempting to create User Params");

        var userParams = System.getModule("com.vmware.library.vCloud.Admin.User").createUserParams(

                    username, fullname, enabled, false,

                    email, phone, null, password, vcdRole,

                    false, runningvmquota,

                    false, storedvmquota

                 );

        var userOut = System.getModule("com.vmware.library.vCloud.Admin.User").createUser(organization, userParams);

        userOut.im = im;

        userOut.update();

        System.log(userOut.name + " imported...");

        line = fileReader.readLine();

    }

    fileReader.close();

    // now cleanup by deleting the uploaded file from the server

    var file = new File(csvFileName);

    System.log("Filename: "+file.name);

    deleteFile(csvFileName);

}else{

    System.log("Error! File not found! "+csvFileName);

}

function deleteFile(fileName) {

    var file = new File(fileName);

       if (file.exists) {

               file.deleteFile();

          }

}

And here's sample code that retrieves a VC:VirtualMachine object by vm name where "vmName" is the String input containing the Name to search for and "vCenterVM" is the resulting VC:VirtualMachine object:

var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name='"+vmName+"'");

var vCenterVM = null;

if (vms != null){

    if (vms.length == 1){

        System.log("Match found for vm named: "+vmName);

        vCenterVM = vms[0];

    }else{

        System.log("More than one VM found with that name! "+vmName);

        for each (vm in vms){

            System.log("VM ID: "+vm.id);

        }

    }

}

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos
Jeremie_R
Contributor
Contributor

I use vco appliance, I think that the path in your script doesn't work with the appliance.

Could you show me an exemple when use MimeAttachement ?

what si better to use, vco on appliance or on windows server ?

0 Kudos