Monday, May 2, 2016

VMWare PowerCLI - Connecting to Esxi Server and get ESXi hosts Part 2

In part 1 Getting command and remote connection, I write about how to import PowerCLI modules to be able to execute commands.
Now I will write about how to connect, and where.
Depend on the requirement, you may want to connect to ESXi host or vCenter in order to perform an operation.
Most of the operation can be executed direct after connecting to vCenter, but incase and for a reason vCenter is not available you might connect direct with ESXi Host insted of connecting to vCenter.
Lets assume that we want connect to vCenter and get all the host in all the clusters.
We can use the following commands

#Import Vmware ModuleAdd-PSSnapin VMware.VimAutomation.Core
#We Will ask the user to write the username and password for vSphere, you can comment this line if your#vCetner is joined to the domain and you are connected to a domain joined computer$viCredential=Get-Credential-Message"Please Write your vCenter username and password"#This command will be used to connect to Esxi Server or vCenter#You can remote the -Credential $viCredential, if your using domain joined computer and your vCenter is#Joined to the domainConnect-VIServer MyvCenterServer -Credential$viCredential#Get all the Servers in the vCenter in all clusteredGet-VMHost|Format-Table -AutoSize name, NumCpu, ConnectionState
#Close the remote connectionDisconnect-VIServer *

As you can see the main steps are
1- Make sure that you have the module or import then as I explained in part 1
2- Connect-VIServer is used to connect with ESXi host or with the vCenter, in my case I will connect with vCenteryou can remove the -Credential$viCredential if your Esxi hosts and vCenter are joined to the domain and the computer you are using is domain joined computer.Get-VMHost can be used to get Esxi hosts piping this command to Format-Table it mean that we want to have custom properties in the returned table.-Autosize will make the columns close to each other.name, NumCpu, ConnectionState, are properties we want show in the table.
The output will be something like this

Disconnect-viserver * will disconnect all the open session to all VI Servers (ESXi host or vCenter).

If you want to show different properties in the table, you can Pipe Get-VMHost to get-member
so the command will be like Get-VMHost | get-member
Just Run the command above after you connect to vCetner, the result will be like the following

Name

MemberType

----

----------

ConvertToVersion

Method

Equals

Method

GetHashCode

Method

GetType

Method

IsConvertableTo

Method

LockUpdates

Method

ToString

Method

UnlockUpdates

Method

ApiVersion

Property

Build

Property

Client

Property

ConnectionState

Property

CpuTotalMhz

Property

CpuUsageMhz

Property

CustomFields

Property

DatastoreIdList

Property

DiagnosticPartition

Property

ExtensionData

Property

FirewallDefaultPolicy

Property

HyperthreadingActive

Property

Id

Property

IsStandalone

Property

LicenseKey

Property

Manufacturer

Property

MaxEVCMode

Property

MemoryTotalGB

Property

MemoryTotalMB

Property

MemoryUsageGB

Property

MemoryUsageMB

Property

Model

Property

Name

Property

NetworkInfo

Property

NumCpu

Property

Parent

Property

ParentId

Property

PowerState

Property

ProcessorType

Property

State

Property

StorageInfo

Property

TimeZone

Property

Uid

Property

Version

Property

VMSwapfileDatastore

Property

VMSwapfileDatastoreId

Property

VMSwapfilePolicy

Property

You can add any Property value to the pipe, lets assume u wana get a list of VMHost in a table and view only PowerState,Name,ProcessorType

#Import Vmware ModuleAdd-PSSnapin VMware.VimAutomation.Core
#We Will ask the user to write the username and password for vSphere, you can comment this line if your#vCetner is joined to the domain and you are connected to a domain joined computer$viCredential=Get-Credential-Message"Please Write your vCenter username and password"#This command will be used to connect to Esxi Server or vCenter#You can remote the -Credential $viCredential, if your using domain joined computer and your vCenter is#Joined to the domainConnect-VIServer MyvCenterServer -Credential$viCredential#Get all the Servers in the vCenter in all clusteredGet-VMHost|Format-Table -AutoSize PowerState, Name, ProcessorType
#Close the remote connectionDisconnect-VIServer *