VMware Cloud Community
eldadc
Contributor
Contributor

Looping vm machines and doing file operations

Hi,

I'm using with vSphere Guest SDK Documentation in .NET C#. VI-ToolKit.

I'm  trying to loop all my vm machines in my ESXi and to do some file  operations , I'm confused but I didn't find how I can loop all machines  and use file manager object, there are lot of examples using  virtual machine object inherited from entity object. No file operation  with those object.

I know VIX+VI where combind in the last SDK release.

For Example : How do I create a StartProgramInGuest method in my code?

any suggestions ?

Thanks In Advanced.

0 Kudos
3 Replies
LucD
Leadership
Leadership

You can do something along these lines (prototyping in PowerCLI)

$si = Get-View ServiceInstance 
$vmOpMgr
= Get-View $si.Content.guestOperationsManager
$vmProcMgr
= Get-View $vmOpMgr.fileManager
$spec
= New-Object VMware.Vim.GuestProgramSpec
$spec
.programPath = "C:\myprogram.exe"
$spec.workingDirectory = "C:\Temp"
$auth
= New-Object VMware.Vim.NamePasswordAuthentication
$auth
.interActiveSession = $false
$auth
.username = "domain\account"
$auth
.password = "mysecretpassword"
Get-View
-ViewType VirtualMachine | %{   $vmProcMgr.StartProgramInGuest($_.MoRef,$auth,$spec) }

It will execute the program C:\myprogram.exe on all the VM and use the credentials passed through the $auth variable.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

eldadc
Contributor
Contributor

Hi,

Thanks so much on the detailed answer.

Can you please elaborate on : $_.MoRef ?

Can you please help me if you can, how to convert this code to c#, it seems the objects are the same, but still I'm getting errors and have issue in my c# code.

Thanks in Advanced.

0 Kudos
LucD
Leadership
Leadership

I'm a C# noob I'm afraid.

But perhaps try in the Developper Community.

The $_ variable represents the object that came over the pipeline (from the Get-View cmdlet) to the code block ('{...}') in the foreach loop ('%').

On the object we access the MoRef property, which is kind of a pointer that vSphere uses to address objects.

In this case we pass this MoRef to the method as a parameter.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos