VMware Cloud Community
thanos24
Enthusiast
Enthusiast

how to use get-log but for .net api instead

I want to figure out how to perform the get-log powercli cmd against an esxi host, but using .net api call instead of powercli cmd.

Get-VMHost HostNameOrIP | Get-Log -Bundle -DestinationPath c:\Storage\Location\

I am not sure if this is the right forum, honestly vmware community is difficult to find any forum specificly for the powercli .net api if i am in wrong place please direct me to the right place.

i already figured out in the .net api how to get the host info

IList <EntityViewBase> esxiList = client.FindEntityViews(typeof(HostSystem), null, null, null);

and then for licenses as well

VMware.Vim.

LicenseManagerlic_manager = (VMware.Vim.LicenseManager)client.GetView(client.ServiceContent.LicenseManager, null);

LicenseManagerLicenseInfo [] lic_found = lic_manager.Licenses;

but i am having a difficult time finding the proper calls in the .net api to get logs from esxi hosts and vcenter host in the SDK reference

http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/index.html

So if someone could please show me what i need to use for api calls? as i assume the get-log powercli cmd has to have an equivalent api call that is using.

Tags (2)
Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership

You assume right, the Get-Log cmdlet uses a method from the SDK API.

That is the GenerateLogBundles_Task method that is present on the DiagnosticManager object.

You will get, in the Task.Info.Result property a number of DiagnosticManagerBundleInfo objects.

Through the URL property in these objects you can fetch the log bundle(s).

As an example, this is a script I normally use

$esxName = "MyEsx" 
$folder = "C:\Temp\" $si = Get-View ServiceInstance
$diagMgr
= Get-View $si.Content.DiagnosticManager
$esx
= Get-VMHost -Name $esxName

$wc = New-Object System.Net.Webclient
$diagMgr
.GenerateLogBundles($true,$esx.ExtensionData.MoRef) | %{   $wc.downloadfile($_.Url.Replace('*',$defaultVIServers[0].Name),
                  (
$folder + $_.Url.Split('/')[-1])) }


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

Reply
0 Kudos
thanos24
Enthusiast
Enthusiast

Ok, i have tried but i am stuck. i am novice programmer and on .net programming little over a year... i normally don't have problems but this vmware api it hard for me to follow for some reason , so my apologies.

based on your example i thought i had it figured out in the api, but i am stuck on the GenerateLogBundles_Task on the host portion... if i am not doing it correctly let me know...

but the auto populate options i am getting in visual studio, i am not able to find anything close to ExtensionData.MoRef, and i tried several other selections that did not work...

I assume i am VERY close and that once i find the right host portion I would then be able to look at logs.info.Result like you mentioned and get the URL then just open the URL and pull down my file ??? I feel i am very close i hope so Smiley Happy  any help would be much appreacited sir. thanks

VimClient client = new VimClient();

 

client.Login(@"https://10.10.10.1/sdk", "administrator", "password");

IList<EntityViewBase> esxiList = client.FindEntityViews(typeof(HostSystem), null, null, null);

VMware.Vim.DiagnosticManager diag_manager = (VMware.Vim.DiagnosticManager)client.GetView(client.ServiceContent.DiagnosticManager, null);

foreach (HostSystem host in esxiList)

{

DiagnosticManagerBundleInfo logs = diag_manager.GenerateLogBundles_Task(true, host.???);

}

Ok i tried the following update see below, based on the vmware api if i am using an esxi host it should be set to true and null for host, if it's vcenter host then set to false and then specify a list/array of hosts.. when i do visual studio says:

Cannot implicitly convert type 'VMware.Vim.ManagedObjectReference' to 'VMware.Vim.DiagnosticManagerBundleInfo'

or am i still not going about this in the right way? thanks

{

DiagnosticManagerBundleInfo task = diag_manager.GenerateLogBundles_Task(true, null);

}

Another update: I now see that a Task is returned and i have to monitor this task, but again i am having difficulty figuring out how to cast the "task" object to return as type Task?  once i do i will have the info.result along with many other options. i now know i must wait for this task to complete before being able to pull down the file using http... any help appreciated i am almost there i can feel it Smiley Happy. my problem now is "task" is a ManagedObjectReference and not type Task ... If i make "task" of type "Task" it says it can't convert MORef to Task.

foreach (HostSystem host in esxiList)

{

ManagedObjectReference task = diag_manager.GenerateLogBundles_Task(true, null);

}

and i tried also  the following but won't convert either, so i am stumped on how to get the task object so i can check it's properties?

foreach (HostSystem host in esxiList)

{

ManagedObjectReference task = (Task)diag_manager.GenerateLogBundles_Task(true, null);

 

}

Reply
0 Kudos
thanos24
Enthusiast
Enthusiast

ok i think i am closer now. see below. i can see that logs gets assigned type "Task" and value is "task-task17001" or some task number.. but the "task" variable is NULL??? so my loop will not work because task.info is NULL???

how else am i suppose to get the returned task type assigned to be able to check it status??

foreach (HostSystem host in esxiList)

{

DiagnosticManager diag_manager = (DiagnosticManager)client.GetView(host.Client.ServiceContent.DiagnosticManager, null);

ManagedObjectReference logs = diag_manager.GenerateLogBundles_Task(true, null);

Task task = new Task(client, logs);

while (task.Info.State != TaskInfoState.success)

{

}

DiagnosticManagerBundleInfo results = (DiagnosticManagerBundleInfo)task.Info.Result;

}

}

Reply
0 Kudos
LucD
Leadership
Leadership

I assume you already consulted the vSphere Web Services SDK Programming Guide ?

In there are several sections that document how to work with MoRefs and Task objects.


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

Reply
0 Kudos
thanos24
Enthusiast
Enthusiast

i didn't use it i thought it was not for .net my mistake Smiley Sad. i thought only doc available was

http://www.vmware.com/support/developer/windowstoolkit/wintk40/doc/viwin_devg.pdf

for each version of the api of course.. but this doc never changed much.. thanks i'll spend some time reading through this i'm sure i should be able to figure it out.

Reply
0 Kudos