PowerCLI Access to vSphere APIs
This chapter shows how to run example scripts by using the PowerCLI SDK cmdlets that bind directly to the vSphere APIs.
You can use the following mechanisms to access the vSphere APIs with PowerCLI:
- For thevSphere Management API: use theGet-Viewcmdlet that allows you to manipulate VIM objects.
- For thevSphere Automation API: use thevSphere Automation SDK for PowerShellwith itsInitialize-andInvoke-cmdlets.
Using Get-View
Use the Get-View cmdlet to view objects and call methods on the
vSphere Management API
. The Get-View cmdlet offers high performance by binding directly to the vSphere Management API.Using the vSphere Automation SDK for PowerShell
Starting from VMware PowerCLI 12.4.0, you can use the
vSphere Automation SDK for PowerShell
to communicate with the vSphere Automation API
.You can construct
vSphere Automation SDK
cmdlets by using the Initialize-
and Invoke-
cmdlet verbs in combination with the names of the vSphere Automation API
data structures or operations.Constructing vSphere Automation SDK Cmdlets

- UseInitialize-cmdlets to prepare the data structures, if any, for your API calls. These cmdlets function on the client side and do not communicate with thevSphere Automation APIserver.
- UseInvoke-cmdlets to execute the operations on the server side.
Installing Microsoft PowerShell and VMware PowerCLI
To access vSphere APIs through cmdlets, you must have
Microsoft PowerShell
and VMware PowerCLI
installed on your machine.- Verify that PowerShell is available on your system. Windows users have PowerShell pre-installed, while Linux and macOS users must install it manually. For more information, see the PowerCLI Installation Guide.
- To install PowerCLI, open PowerShell and run the command:Install-Module-NameVMware.PowerCLI
Basic Authentication to the VIM API and the vSphere Automation API
With PowerCLI, you connect with a single command to the
vSphere Management API
and the vSphere Automation API
.- Run theConnect-VIServercmdlet with your credentials and you create authentication sessions to both vSphere APIs automatically.Connect-VIServer-Servervc1.example.com -User'MyUser' -Password'MyPassword'
Creating a Hello Folder with the VIM API Using PowerCLI
Because the
CreateFolder
method is a part of the vSphere Management API
, you must use the Get-View
cmdlet.- Create a variable with theServiceInstanceobject view.$serviceInstance = Get-View ServiceInstance
- Create a variable with theRootFolderobject view.$rootFolderView = Get-View -Id $serviceInstance.Content.RootFolder
- To create your folder, invoke theCreateFoldermethod on theRootFolderview.$FolderMOID = $rootFolderView.CreateFolder('Hello Folder')
Tagging the Hello World Folder with the Automation API Through PowerCLI
The Tagging APIs are included in the
vSphere Automation API
as a part of the CIS REST APIs
package (CIS stands for common infrastructure services). Therefore, you can use the vSphere Automation SDK for PowerShell
to access the Tagging APIs with PowerCLI.To tag a folder, start by creating a tag category, then create a tag in that category, and finally associate the tag with the folder. Each step in the tag creation and application process requires two PowerCLI verbs, both the
Initialize
and the Invoke
.- Create a tag category.
The system returns the ID of the newly created tag category.$TaggingCategoryCreateSpec = Initialize-TaggingCategoryCreateSpec -Name "helloCategory" -Description "A tag category for custom folders" -Cardinality "SINGLE" -AssociableTypes "Folder" $tagCategoryID = Invoke-CreateCategory -TaggingCategoryCreateSpec $TaggingCategoryCreateSpec - Create a tag in that category. You use the tag category ID from the previous step.
The system returns the ID of the newly created tag.$TaggingTagCreateSpec = Initialize-TaggingTagCreateSpec -Name "helloTag" -Description "example of a tag from PowerCLI" -CategoryId $tagCategoryID $tagID = Invoke-CreateTag -TaggingTagCreateSpec $TaggingTagCreateSpec - Associate the tag with theHello Worldfolder. You use theHello Worldfolder ID and the tag ID that you created in the previous step.$FolderID = Initialize-StdDynamicID -Type "Folder" -Id $FolderMOID.value $TaggingTagAssociationAttachRequestBody = Initialize-TaggingTagAssociationAttachRequestBody -ObjectId $FolderID Invoke-AttachTagIdTagAssociation -TagId $tagID -TaggingTagAssociationAttachRequestBody $TaggingTagAssociationAttachRequestBody
You can now see the
Hello World
folder with
the tag and category in the vSphere Client.