ScottDriver42
Enthusiast
Enthusiast

Discussion: Automation- Api's vs. PowerCLI

This is a topic I've been thinking a lot about lately as I've been reading blogs, listening to podcasts, talking to peers, etc. If we are talking about automation, strictly automation, does it matter whether you use an API or PowerCLI? I was listening to a Datanaut's podcast today where the guest was the creator of Vagrant and Terraform and to paraphrase he said "if a product doesn't have a full API it doesn't exist. If the goal is to automate, to me this sounds like an ideological statement. If you can set variables, run declarative code, get results,does it matter if you use an API or PowerShell?

So what I'd like to know is, what am I missing? Is it just a comfort thing where Dev's like Api's and Ops may be more comfortable with PowerShell? Is this API movement just the latest thing?

I have opinions on these questions, but I'd like to hear from others. I hope that I don't break any rules by doing this, but I'd like to get a variety of opinions so I will likely cross post this in the Code community (if a moderator sees this and can help me with, I'd appreciate the assistance).

Please share your thoughts, I'd really like to get some different thoughts on the matter.

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
10 Replies
sajal1
VMware Employee
VMware Employee

It does matter. In case of automation also, main aim is to automate with as much efficiency as possible. When it comes to API, it provides way more information that available through normal PowerCLI. Also it will run quicker. I suggest for these two portions with proof you visit the session I created on advance powercli. I covered why and where you should use API (Learn Advance PowerCLI in under 100 minutes – Anything Virtual and Cloudy ).

Also Brian Graf did an excellent post on the performance side of this. I suggest to read that post on why you should use Get-View than normal cmdlets ()Get-View Part 3: Peformance Impact - Is it really THAT much different? - VMware PowerCLI Blog - VMw... .

In my opinion if you are seriously talking about automation then you will have to use API. The other day I was seeing a post in PowerCLI community (I forgot the exact details) where someone was looking to reduce the runtime of the of a script which was running on 1000 hosts and taking half a day to run. Lucd provided a solution with API reducing run time drastically.

ScottDriver42
Enthusiast
Enthusiast

Thanks for the response! I'll be sure to check out the links and your blog (and I'll probably be back with more questions).

Just to start then, I'd like to understand what in your opinion, the case for PowerCLI is? I see (but haven't had a chance to review) that you have a lot of PowerCLI content on your blog.

SD

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
0 Kudos
LucD
Leadership
Leadership

Just my 2¢, in my opinion it's not a question of either/or between PowerCLI and the API.

The PowerCLI cmdlets do use the same API under the covers!

Since the PowerCLI cmdlets have to be general, they have to make trade offs.As a consequence they are slower than calling the API methods directly.

The PowerCLI cmdlets also make a selection of the properties they return.
This is what is commonly called the 80-20 percent rule. The cmdlets will be sufficient for 80% percent of your tasks, for the other 20%, you will have to dig deeper.

In PowerCLI you can speed up a script by calling the API directly, in other words bypassing a lot of the general stuff that is in the PowerCLI cmdlets.

That's when you enter the realm of Get-View and ExtensionData.

The coding becomes (a bit) more complex, but the code will execute faster.

But remember, you are still calling the same API!

If you want to use the API you will always have to use a language that calls these API.
PowerCLI is one such language, but you could also go for Java, Python, Go, C#... or even a tool like Postman which allows you to call the REST API.

My conclusion, a product needs (public) API to allow automation.
Whatever method you chose to call these API methods, doesn't really matter.
There is a speed of execution factor to take into account.

On the other hand , easy and fast prototyping of your code isn't negligible either.

I could well imagine someone doing his prototyping with PowerCLI, and once tested and working, go to for example C# to create the production code.


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

sajal1
VMware Employee
VMware Employee

Completely agree with Lucd. My assumption was discussion is about whether to use direct API's or PowerCLI cmdlets. So my answer was based on this assumption and biased towards this answer.

As Lucd mentioned, PowerCLI is also using the same API's so either way you are using API. So the question should/can be, whether/when to use direct API's and when use the PowerCLI cmdlets (abstraction of API).

I would still strongly suggest  to read the content from blog link and watch the video. It has all these explanations and covers what you are asking in this discussion. 

ScottDriver42
Enthusiast
Enthusiast

I was about to ask for more detail on the PowerCLI/API relationship.  🙂

I won't get a chance to view the links until this weekend but I will definitely check them out.

Thank you both for the responses, I appreciate it.

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
0 Kudos
ScottDriver42
Enthusiast
Enthusiast

Thanks for the responses and info guys, really appreciate it.

Warning: Dumb question time! So after reading through Brian's blog, and going through Sajal's intro/advanced posts, as well as RTM on get-view, I'm still a little confused. Are you saying that get-view is the api? As it is a part of VMware.VimAutomation.Core and is in the format of a cmdlet, I'd just presumed it was considered part of the core PowerCLI suite.

Is this a failure a lack of understanding on my part? Or did the marketing/product teams need to do a better job of putting out the neon sign that says  "get-view is the vsphere api!"?

Sub-topic, maybe I should break it into it's own question, but... I've used get-view on many occasions. In general I've tended to pass in vi-objects, but even when using it directly I've always found it to be slow. Going through your responses and the linked materials, it sounds to me like I'm maybe doing something wrong. For example, below is the results of my comparison of the two. Am I off the mark, or am I doing something wrong? Does it work faster when acting directly against an object?

Again, many thanks for engaging with me this topic. It's already been educational.

SD

get-viewget-vm

H:\> Measure-Command{ get-view -ViewType VirtualMachine}

Days              : 0

Hours             : 0

Minutes           : 0

Seconds           : 6

Milliseconds      : 780

Ticks             : 67809436

TotalDays         : 7.84831435185185E-05

TotalHours        : 0.00188359544444444

TotalMinutes      : 0.113015726666667

TotalSeconds      : 6.7809436

TotalMilliseconds : 6780.9436

H:\> Measure-Command{ get-vm}

Days              : 0

Hours             : 0

Minutes           : 0

Seconds           : 0

Milliseconds      : 413

Ticks             : 4138398

TotalDays         : 4.7898125E-06

TotalHours        : 0.0001149555

TotalMinutes      : 0.00689733

TotalSeconds      : 0.4138398

TotalMilliseconds : 413.8398

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
0 Kudos
LucD
Leadership
Leadership

There are no dumb questions!
No, the Get-View cmdlet is not the API, but it is one way to interact more directly with the API.

Let's take an example, placing an ESXi node in maintenance mode.

The API method that accomplishes that is described in the API Reference as EnterMaintenceMode_Task under the HostSystem managed object.

Calling that method in PowerCLI, is quite simple

Get-VMHost -Name $esxName | Set-VMHost -State Maintenance -Confirm:$false

Note that behind these two cmdlets, there are a number of API methods that are called.

In fact, you can "see" the API methods that are being called, when you use the Onyx tool.

If we want to avoid the cmdlets, and interact directly with the API methods, we have two options from within PowerCLI.

1) With Get-View

$esx = Get-View -ViewType HostSystem -Filter @{'Name'=$esxName}

$timeout = 0

$evacuate = $true

$spec = New-Object VMware.Vim.HostMaintenanceSpec

$esx.EnterMaintenanceMode($timeout,$evacuate,$spec)

2) With the ExtensionData property

$esx = Get-VMHost -Name $esxName

$timeout = 0

$evacuate = $true

$spec = New-Object VMware.Vim.HostMaintenanceSpec

$esx.ExtensionData.EnterMaintenanceMode($timeout,$evacuate,$spec)

The fact that we can use the method directly from within PowerCLI, is due to the fact the PowerCLI Dev Team has created a "framework".

Thanks to that framework, we can "see" and "use" the objects and methods that are described in the API.

The API on itself is no coding language. It is an interface that allows the coder to interact with the vSphere environment.

This interaction requires a "vehicle", and this vehicle can be any language that has this "framework" defined.

That's why you can use PowerCLI, Java, Python, Go, C#...

The speed of the Get-View cmdlet only becomes visible in bigger environments.
We're talking of thousands of entities here.
And the Get-VM cmdlet was optimised by the PowerCLI Dev Team in recent releases.
The difference between Get-VM and Get-View has become less in the latest PowerCLI releases.


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

mvelezwhiteFFI
Contributor
Contributor

Hey guys:

Sorry if I was late to the party. I heard mention of automation (APIs vs PowerCLI). I don’t mean to make the choices harder for you, but there is a book that was just released last month which may be of help to you. It’s entitled PowerShell: Automating Administrative Tasks. Here is the link: https://urldefense.proofpoint.com/v2/url?u=https-3A__www.amazon.com_PowerShell-2DAutomating-2DAdmini...

Since PowerShell and PowerCLI share so many options/features/etc, I thought that perhaps this would give you some perspective on the whole automation thing. I’m in the middle of performing upgrades, establishing best practices, automating as much as I can in an environment with over 30 vCenters. Books like this and community members like LucD have kept me sane on this project so many times over that there aren’t enough steak, lobster and beer dinners in the world that I can pay people like him with.

The Learning PowerCLI just released its 2nd edition as well. It may sound crazy, but acquire (buy, beg, borrow and/or steal) as many resources as you can and get a good network of people (like this community) who can help you with just about anything. I only suggest the books because it gave me enough info that I would know what questions to ask. I didn’t want to be a total noob LOL.

Good luck and let me know if there’s anything that I can help with. I’m not on the other guys level, but perhaps there’s something less complex that you may not be able to get your head around. I’d be honored to help you get comfortable with some of this stuff; that also helps me to be more familiar with it as well. That’s something you’ll learn about from being in this community. You may already be aware of it. How many places can you go to ask for help and then actually get it?!!

No more rambling, sorry. I get that way about this stuff.

Take care,

Migs

Miguel T Velez-White

Systems Engineer

Flowers Foods, Inc.

Miguel.Velez-White@flocorp.com<mailto:Miguel.Velez-White@flocorp.com>

Office – 229.551.3151

Cell – 318.294.4989

ScottDriver42
Enthusiast
Enthusiast

So much clearer now, although I obviously still have some work to do to get up to snuff.

Not that it matters to the current conversation, but I can't for the life of me get Onyx to work. But that's a matter for another day!

Thanks Luc!

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
0 Kudos
ScottDriver42
Enthusiast
Enthusiast

I'll have to go check that book out Miguel.

For my money I've always told people to read PowerShell in a month of lunches. It's a very accessible first take on PowerShell, in manageable chunks.

Blog: https://virtualvt.wordpress.com/ | Twitter: VTsnowboarder42
0 Kudos