Saturday, April 24, 2021

Powershell ESXCLI

 Recently at work I was tasked with gathering information for all of our VM Hosts at work. In the process of doing so, I realized that documentation was a bit hard to find and unclear how to get from point A to point B without hopping from blog to blog, doc to doc. So here is my explanation of it all from start to finish of how to interact with EsxCli through PowerShell. I will include a simple script at the end.


The first thing is we need to make sure we have the module we need to do it. To just simply install it via command, it is as follows:

Install-Module -Name VMware.PowerCLI -AllowClobber


This will pull the necessary module to work with EsxCli stuff. After that we need to import it with this command:


Import-Module VMware.PowerCLI


Simple enough. Now we can actually start using the VMware PowerCLI stuff. There are a ton of commands for it which can be looked at via this command:


Get-Command -Module VMware.*


The next goal for me was to connect to a server because you cannot do much of anything until you connect. To do so, you need to consider whether or not the certificate for your server will throw an error or not. If it will, it may be best to tell it to be ignored to speed things up. You do all of this with the following commands (This connects to either a regular server or VCenter):


Set-PowerCLIConfiguration  -InvalidCertificateAction Ignore

$server = Connect-VIServer -Server YourServerNameHere


Hopefully all goes well up to this point. It should ask you for credentials to log in. If you're still with me, all is well. We are mere moments away from being able to access all of the EsxCli stuff. We need to interact with the VM host or hosts. If you connect to VCenter, you will have multiple hosts. We can access the one or more hosts with this command:


Get-VMHost


The results of that will give us the objects necessary to then access the EsxCli. We do that like so:


$esxcli = Get-EsxCli -VMHost HostReturnedFromGetVMHost


Everything can then be accessed through the $esxcli object through properties. There is also a V2 option that changes things slightly. Adding that changes things slightly when calling. I will use that in the script example at the end. As it stands now, if we want something like the server platform information, we do so like this:


$esxcli.hardware.platform.get()


There you have it, the basics of how to use PowerShell to interface the EsxCli interface. Now here is the script I made, then modified a bit while writing this up for improvement. It gets a CSV file that will contain the IP, Make, Model, and Serial Number for an inventory of the VM hosts.


# Install necessary powershell module for EsxCli

If (-Not $(Get-Module -ListAvailable -Name VMWare.PowerCLI)) {

    Install-Module -Name VMware.PowerCLI -AllowClobber

}


Import-Module VMware.PowerCLI


# Collection object for exporting later

$vmhostinfo = @()


# Need to ignore because no CA

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

# Should ask for credentials the first time probably

$server = Connect-VIServer -Server boe-vcenter5.ecsd.effingham.k12.ga.us


# Iteration, loops, and that stuff

Get-VMHost | ForEach-Object {

    $x = Get-EsxCli -VMHost $_ -V2

    # This is to get around the special interpretation of $_

    $ip = $_.Name

    # Adds an IP property to the collection

    $vmhostinfo += $($x.hardware.platform.get.Invoke() | Select-Object -Property @{Name = "IP"; Expression = {$IP}},VendorName,ProductName,SerialNumber)

}


#Given in CSV format because it's easier to manipulate and exports to a spreadsheet

$vmhostinfo | Export-Csv -Force vmhostinfo.csv -NoTypeInformation

# Good luck!


There you have it, a simple script for gathering some information. There's obviously a lot more that you can do, but at work we do not have a need for any of that just yet. I will probably create something later on down the pipeline to script out other tasks I normally do manually just to see if I can. Hope this helps people.

No comments:

Post a Comment

Tag Cloud

.NET (2) A+ (5) ad ds (1) addon (4) Android (4) anonymous functions (1) application (9) arduino (1) artificial intelligence (1) backup (1) bash (6) camera (2) certifications (3) comptia (5) css (2) customize (11) encryption (3) error (13) exploit (5) ftp (1) funny (4) gadget (4) games (3) GUI (5) hardware (16) haskell (6) help (14) HTML (3) imaging (2) irc (1) it (1) java (2) javascript (13) jobs (1) Linux (19) lua (1) Mac (4) malware (1) math (6) msp (1) network (13) perl (2) php (3) plugin (2) powershell (8) privacy (2) programming (24) python (10) radio (2) regex (3) repair (2) security (16) sound (2) speakers (2) ssh (1) story (5) Techs from the Crypt (5) telnet (1) tools (13) troubleshooting (11) tutorial (9) Ubuntu (4) Unix (2) virtualization (2) web design (6) Windows (16) world of warcraft (1) wow (1) wx (1)