Use basic and advanced Cmdlets to manage VMs and ESXi Hosts – Part 2 {Managing ESXi Host}


Managing ESXi Host:

Displaying hosts related commands
Get running Services on vCenter Hosts
Stop , Start , Restart services on all ESXi hosts
Display specific services on all ESXi hosts
Stop specific services on all ESXi hosts
list related firewall cmdlets
Get default firewall policy and modify it
Get firewall policy exceptions
List disabled firewall rules
Modify firewall rule on All ESXi Hosts
Modify advanced settings on Host level
get list of sessions connected to VMware Hosts or vCenter and connect to them

PowerCLI C:> Get-Command *vmhost* | select name

List running services:

PowerCLI C:> Get-VMHostService -VMHost

Stop a service:

PowerCLI C:> Get-VMHostService -VMHost | Where-Object {$_.Key -eq ‘ntpd’} | Stop-VMHostService

We can use same above syntax to start it , let us try different way:

PowerCLI C:> Get-VMHostService -VMHost | Where-Object {$_.key -eq "ntpd"}

PowerCLI C:> Start-VMHostService -HostService $vmHostService

Restarting service:

PowerCLI C:> Restart-VMHostService -HostService $vmHostService -Confirm:$false

Stop / Start on all hosts , change the policy to off / automatic or On ALL AT ONCE, I believe this example will be useful in exam where you will check everything before you try and enable required services before even you begin:

First we get all hosts in a variable:

PS C:> $vmhosts= get-vmhost

PS C:> $vmhosts

Quickly check all hosts for enabled/disabled and policy status, much better than GUI and all together, saving slow VI client or even web one, I don’t need to know hosts name here , I need a just quick view:

PS C:> Get-VMHost $vmhosts | Get-VMHostService | Where-Object {$_.Key -eq ‘TSM’ -or $_.Key -eq ‘TSM-SSH’}

PS C:> Get-VMHost $vmhosts | Get-VMHostService | Where-Object {$_.Key -eq ‘TSM’ -or $_.Key -eq ‘TSM-SSH’} | Stop-VMHostService -Confirm:$false

Now all hosts have no ssh or esxi shell enabled

Let’s unify all policies to one configuration, On

PS C:> Get-VMHost $vmhosts | Get-VMHostService | Where-Object {$_.Key -eq ‘TSM’ -or $_.Key -eq ‘TSM-SSH’} | set-vmhostservice -policy on

BTW, you can put them together, no issues , I tried it:

PS C:> Get-VMHost $vmhosts | Get-VMHostService | Where-Object {$_.Key -eq ‘TSM’ -or $_.Key -eq ‘TSM-SSH’} | set-vmhostservice -policy on | Stop-VMHostService -Confirm:$false

Now let us bring the policy all to automatic and start up our ssh & ESXi shell access, but I will do both together:

Get-VMHost $vmhosts | Get-VMHostService | Where-Object {$_.Key -eq ‘TSM’ -or $_.Key -eq ‘TSM-SSH’} | set-vmhostservice -policy automatic | Start-VMHostService -Confirm:$false

We have 4 Firewall related cmdlets:

PowerCLI C:> Get-Command *firewall*

Get firewall and Exception status

PowerCLI C:> Get-VMHostFirewallDefaultPolicy -VMHost

Listing only disabled "false" firewall rules:

PowerCLI C:> Get-VMHostFirewallException -VMHost | Where-Object {$_.Enabled -like ‘False’}

Change default firewall policy for a single host same like all hosts , but I preferred here to make it for all :

PowerCLI C:> $vmhost= Get-VMHost

PowerCLI C:> $vmhost

Check policy, then return them back:

PowerCLI C:> $vmhost | Get-VMHostFirewallDefaultPolicy

Remember I don’t need here to see the hosts, I know I have 3 hosts in that vCenter, so 3 lines are enough for me.

Let’s change disabled NFS client on all ESXi hosts , again , I like to do it for all at once:

PowerCLI C:> $vmhost | Get-VMHostFirewallException -Name "NFS Client" | Set-VMHostFirewallException -Enabled $True

You can set advanced settings on host level:

PowerCLI C:> Get-VMHost | Get-AdvancedSetting | more

Or all hosts at once 🙂

PowerCLI C:> $hosts

This is the ssh warning when it is enabled , popping up on each Host summary , we will hide it on all hosts:

PowerCLI C:> Get-VMHost $hosts | Get-AdvancedSetting -name UserVars.SuppressShellWarning | Set-AdvancedSetting -Value "1" -Confirm:$false

Get Viserver session IDs on all hosts:

$hosts= get-vmhost

PowerCLI C:> Get-VIServer $hosts -User root -Password cervantes | fl

Now I get all session IDs for all hosts in my vCenter, of course you can do it for a single server by removing $hsots and place your required host IP

PowerCLI C:> Get-VIServer $hosts -User root -Password P@ssw0rd | select name, sessionid

Now I can open a new PowerCli shell and connect to that specific session:

The above were for hosts , here I can same way get the session IDs for vCenter itself:

Get HBA devices on all hosts:

PS C:> Get-VMHostHba $hosts -Device *hb* |more

Be more specific to display the FC ones only "Autosize is better to see all closer other than big spaces in between"

PS C:> Get-VMHostHba $hosts -Device *hb* | select vmhost, name, type | Where-Object {$_.Type -eq "FibreChannel"} |ft -autosize


About Ahmad Sabry ElGendi
This entry was posted in Uncategorized, VCAP5-DCA, Vmware. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s