PowerCli To enable/disable the ESXi network Card

Recently I have been approached to answer the query if we can disable the network card using PowerCli for multiple ESXi hosts at once.
Simple way to do is connect to ESXi host over the ssh session and run the below command.
esxcli network nic down -n vmnicname

But what if you want to do this for 100+ ESXi hosts ,this can become tedious task to take ssh session and run the following command . To ease the job I have written the function in PowerCli which will combine the EsxCli into PowerCLI.

Function Set-VMHostNetworkCard 
{
<#
.NOTES
===========================================================================
Created by: Ankush Sethi
Blog:       www.vmwarecode.com
===========================================================================
.SYNOPSIS
To enable/disable the esxi network card
.DESCRIPTION
Function will provide the funtionality to disable or enable the network card etc.
.PARAMETER VMhost
Enter the ESXi name for which you want to perform the action.
.PARAMETER vmnic
Use this parameter provide the network card of esxi ,If you press Tab values will come automatically.
.PARAMETER Action
Use this parameter to provide the enable disable action 

.EXAMPLE
example 1>Get-vmhost esxiname|Set-vmhostnetworkcard -vmnic vmnic1 -Action Disable
example 2>Get-vmhost esxiname|Set-vmhostnetworkcard -vmnic vmnic1 -Action Enable
#>


param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]$VMhost,
[Parameter(Mandatory=$true)]
[ValidateSet("vmnic0","vmnic1","vmnic2","vmnic3","vmnic4","vmnic5","vmnic6","vmnic7")]
$vmnic,
[Parameter(Mandatory=$true)][ValidateSet("Enable","Disable")]$Action
)

process 
{

Try{$esx=Get-VMHost -Name $VMhost -ErrorAction Stop}
catch
{
Write-Error "Please enter the correct name of ESXi"
}

If(($esx.ConnectionState -match "Disconnected") -or ($esx.ConnectionState -match "NotResponding"))
{
Write-Error "ESXi host is not in connected or maintenance mode state to perform the action."
}
$esxcli=Get-EsxCli -VMHost $esx -V2

if($Action -match "Enable")
{
$arg=$esxcli.network.nic.up.CreateArgs()
$arg.nicname=$vmnic
$output=$Esxcli.network.nic.up.invoke($arg)
If($output -eq $true){$esxcli.network.nic.list.invoke()|?{$_.name -match $vmnic}|ft -AutoSize}
}

elseIf($Action -match "Disable")
{
$arg1=$esxcli.network.nic.down.CreateArgs()
$arg1.nicname=$vmnic
$output=$Esxcli.network.nic.down.invoke($arg1)
If($output){$esxcli.network.nic.list.invoke()|?{$_.name -match $vmnic}|ft -AutoSize}

}
}
}

Prerequisite :

1. We should be already connected to vCenter using below command.

Connect-VIServer -Server vCenterName -User UserName -Password Password

2. PowerCli should be greater tha 6.5 Version, To check the current version please use the below command.

Get-Module -ListAvailable -Name vmware.powercli

How to use the Function

  1. Copy the code from above and paste it into Notepad.
  2. Save it with anyname with extension of .ps1
  3. Open the PowerCli and to go the directory where it is saved using cd command
  4. Run the command . .\FileName.ps1 (Note :There is a space between 2 dots.)
  5. Function is loaded in local session of PowerCli and you are good to use this as cmdlet.

Once you have completed above 5 steps , we are good to use this function as cmdlet of Powercli.

Check the below sample usage now.

There are total 2 ways to use this cmdlet. Note you can provide the ESXi name from pipeline as well as parameter in direct command.

Sample Usage with ESXi name as parameter:
Note: No need to type the value of vmnic and Action , Just give SPACE and TAB value will occur

 Set-VMHostNetworkCard -VMhost ESXI1 -vmnic vmnic3 -Action Disable

Sample usage to bring the network card up

Set-VMHostNetworkCard -VMhost ESXI1* -vmnic vmnic3 -Action Enable

Sample usage to enter the ESXi name from Pipeline

Get-VMHost ESXiname|Set-VMHostNetworkCard -vmnic vmnic5 -Action Disable

This can take multiple ESXi as input and process it ,Even you use the Cluster also for provide the ESXi name as input.

Get-VMHost "esxi1","esxi2"|Set-VMHostNetworkCard -vmnic vmnic4 -Action Disable

Sample for Cluster Name:

 Get-Cluster|Get-VMHost|Set-VMHostNetworkCard -vmnic vmnic4 -Action Disable

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

Create a website or blog at WordPress.com

%d bloggers like this: