Tuesday, June 25, 2019

Connecting to vCenter from PowerShell

For a few years now I have been pulling data from the vCenter SQL Server database about my SQL Server virtual machines and storing that data into my own database.  After systems did the last upgrade, vCenter no longer stores the information in a SQL database. I asked them how to connect and they suggested that I use the PowerShell library that VMware provides. This is how I got it to work.
NOTE: I am only going to be using Get commands when I connect. You can change settings or start and stop VMs and even hosts from the tools if you have the proper permissions so know what a command does before you run it.  I don't believe you can use the -whatif parameter with any of the commands so be extra careful.


First I need to install the library, also known as VMware PowerCLI. I tried downloading the install package but it is an older version. After some web searching, I found that you can get it from PowerShell Gallery
You can get the most current pre-release version using this code. NOTE: I ran into some issues using pre-release versions.
Install-Module -Name VMware.PowerCLI
If you want to install a specific version, you can specify the version number. Check this PowerShell Gallery page for version numbers. I am going to install the current version listed on that page
Install-Module -Name VMware.PowerCLI -RequiredVersion
If you find you need to remove the module you can do so with this command. I had to use this to uninstall an older version and install a newer one
get-module -Name VMware.PowerCLI -ListAvailable | uninstall-module -force

Getting Connected

Now that I have it installed I want to see what I have available and then I will connect to a vCenter server. To list the modules that were installed use this command
get-module -ListAvailable |where-object {$_.name -like 'VMWare*'}
On my machine I get this list back.

You could then use Get-Command -Module [Module Name] to list out all the function and CmdLets for each module. Feel free to test that out but we are going to be using the VMware.VimAutomation.Core module today to get some information.

First we are going to connect to a vCenter server. We need to import the module first then we can connect (Remember to change the parameters for your environnment)
Import-Module -Name VMware.VimAutomation.Core
Connect-VIServer "vCenterServer" -User "DFA_Primary\MYUSERNAME" -Password "MYPASSWORD"
In my environment I got the following error message:

Connect-VIServer : 6/25/2019 2:18:48 PM Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Prompt if you'd like 
to connect once or to add a permanent exception for this server.
This is because we use a self-signed certificate. You need to execute this command once to fix this:
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
After I executed this command, I was able to use the Connect-VIServer command.

Basic Commands

Now that we are connected we can use some basic commands to get inventory information. First I am going to list all of the hosts with some of their properties
Get-VMHost |select-object Name, ConnectionState, PowerState, NumCPU, MemoryTotalGB | Format-Table
If you want to see all the properties of just one host you can do it like this
#This gets basic info on a host
Get-VMHost |where-object {$_.name -eq "astdc-esxi02.dimensional.com"} |select-object -Property *
#This will get you even more info but you have to supply a -VMHost paramater
Get-VMHostAdvancedConfiguration -VMHost "astdc-esxi02.dimensional.com"
Now lets do the same thing for virtual machines. First we will list them 
Then we can dig in and see all the available properties of a particular virtual machine
Get-VM |where-object {$_.name -eq "VMNAME"} |select-object -Property *
These are just some basic commands to get started but they give me the data that I need to collect the inventory information I used to be able to collect directly from the old SQL Server database.

No comments:

Post a Comment