Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Create a custom TCPIP stack on ESXi server - VMware PowerCLI GUI

While working with my VMware team we had to audit and create or delete TCP/IP network stack, Here on the ESXi server under Networking >> TCP/IP Configuration you cannot create new custom stack using vSphere client. I was able to automate almost all infrastructure using PowerCLI scripts, but few of the ESXi servers we were not able to update/edit TCP/IP configuration, due to no direct connection to the server. So me and my team had to manually configure those server through PowerCLI command to add new TCP/IP custom stack. 

Other GUI script: Powershell GUI VMware ESXi custom patch bundle builder

VMware vSPhere vCenter ESXi tcp ip configuration powercli powershell wpf xaml gui default tcpip stack networking virtual switch esxi list dvs distributed switch standard.png

I have written written this script for my junior colleagues who are bit hesitant to use script but wants to provision/deploy ESXi servers, Instead of command line script they want some interface so they can track the TCP/IP stack configuration while adding or removing it.

VMware vSphere vCenter ESXi server TCP IP configuration network stack netstack VMkernel ipv4 gateway stack dns server advanced primary system stack custom stack.png

This is how the script works, It is built using PowerShell and WPF XAML codes using PowerCLI module.

There is by default 'default' named stack is used for management traffic. By default same stack (gateway) is used by other traffic, for example: vMotion and provisioning (cold migration, VM clones, and snapshots), Using TCP/IP netstack configuration I can assign dedicated gateway, routing table and DNS configuration to such traffic, And isolate network traffic. 

This script is built and based on PowerCLI Get-EsxCLI command line to add or remove TCP/IP netstack on ESXi Server. 

$esxcli.network.ip.netstack.add.invoke(@{netstack = "test"})
$esxcli.network.ip.netstack.remove.invoke(@{netstack = 'test'})

Download this script Esxi_TCP/IP_configuration here, or this script is also available on github.com.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#Load required libraries
Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase, System.Windows.Forms, System.Drawing 

#Website: http://vcloud-lab.com
#Written By: vJanvi
#Date: 14 Aug 2021
#Tested Environment:
    #Microsoft Windows 10
    #PowerShell Version 5.1
    #PowerCLI Version 12.3.0
    #vSphere 7

#Read xaml file
#$xamlFile = 'D:\Projects\PowerShell\WPF\Esxi_TCP-IP_Configuration\Esxi_TCP-IP_Configuration\MainWindow.xaml'

#<#
$xamlContent = @'
<Window x:Class="Esxi_TCP_IP_Configuration.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Esxi_TCP_IP_Configuration"
        mc:Ignorable="d"
        Title="ESXi server TCP/IP configuration      - http://vcloud-lab.com" Height="480" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
            <ColumnDefinition Width="28*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="28*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>
        <GroupBox x:Name="groupBox_Authentication" Header="VC/ESXi Authentication" Grid.ColumnSpan="2"  Grid.RowSpan="6" Margin="5,5,5,0">
            <StackPanel>
                <Label x:Name="label_vCenter" Content="vCenter or ESXi Server:" Height="23" VerticalAlignment="Top" Margin="5,0,5,0"/>
                <TextBox x:Name="textBox_vCenter" Height="23" Text="marvel.vcloud-lab.com" TextWrapping="Wrap" Margin="5,0,5,0"/>
                <Label x:Name="label_UserName" Height="23" Content="UserName:" Margin="5,0,5,0"/>
                <TextBox x:Name="textBox_UserName" Height="23" Text="administrator@vsphere.local" TextWrapping="Wrap" Margin="5,0,5,0"/>
                <Label x:Name="label_Password"  Content="Password:" Height="23" Margin="5,0,5,0"/>
                <PasswordBox x:Name="passwordBox_Password" Height="23" Password="Password" Margin="5,0,5,0"/>
                <Button x:Name="button_Login" Content="Login" Height="21" Margin="5,2,5,2" Width="70" HorizontalAlignment="Right"/>
            </StackPanel>
        </GroupBox>
        <TextBlock x:Name="textBlock_ESXiList" Grid.Column="2" Text="ESXi Server List:" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,10,0,0" Grid.ColumnSpan="2"/>
        <Button x:Name="button_ESXiList" Content="ESXi List" Height="22"  Grid.Column="4" Margin="5,5,5,5" IsEnabled="False"/>
        <ListBox x:Name="listBox_ESXiList" Grid.Column="2" Grid.Row="1" Margin="5,0,5,5" Grid.ColumnSpan="3" Grid.RowSpan="5"/>
        <DataGrid x:Name="dataGrid_TcpIpConf" Grid.Row="7" Margin="5,0,5,5" Grid.ColumnSpan="5" Grid.RowSpan="3"/>
        <Button x:Name="button_TCPIPConf" Content="Get TCP/IP Conf" Height="22" Grid.Column="3" Grid.ColumnSpan="2" Margin="5,5,5,5" IsEnabled="False" Grid.Row="6"/>
        <TextBlock x:Name="textBlock_TcpIpConf" HorizontalAlignment="Left" Margin="5,0,5,5" Grid.Row="6" Text="Current TCP/IP Configuration:" TextWrapping="Wrap" VerticalAlignment="Center" Grid.ColumnSpan="2" />
        <TextBlock x:Name="textBlock_Logs" Grid.Column="5" Text="Configuration Logs:" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,10,0,0" Grid.ColumnSpan="2"/>
        <TextBox x:Name="logTextBox" Grid.Column="5" Margin="5,0,5,5" Grid.Row="1" Text="Logs" TextWrapping="Wrap" Grid.ColumnSpan="3" Grid.RowSpan="5" IsReadOnly="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
        <TextBox x:Name="textBox_Remove" Grid.Column="6" Margin="5,5,5,5" Grid.Row="7" Text="{Binding SelectedItem.Name, ElementName=dataGrid_TcpIpConf}" TextWrapping="Wrap" VerticalAlignment="Center" Grid.ColumnSpan="2" IsReadOnly="True"/>
        <Button x:Name="button_Add" Content="Add" Grid.Column="5" Grid.Row="8" Margin="15,7,15,10"/>
        <Button x:Name="button_Remove" Content="Remove" Grid.Column="5" Grid.Row="7" Margin="15,10,15,7" />
        <TextBox x:Name="textBox_Add" Grid.Column="6" Margin="5,5,5,5" Grid.Row="8" TextWrapping="Wrap" VerticalAlignment="Center" Grid.ColumnSpan="2"/>
        <TextBlock x:Name="textBlock_AddRemoveMessage" Grid.Column="5" HorizontalAlignment="Left" Grid.ColumnSpan="3" Margin="15,0,5,5" Grid.Row="9" Text="Restriction: Don't Add/Remove stacks names with Default, Provisioning, vMotion, defaultTcpipStack" TextWrapping="Wrap" VerticalAlignment="Center" Foreground="Maroon"/>
    </Grid>
</Window>
'@
#>

#$xamlContent = Get-Content -Path $xamlFile -ErrorAction Stop
#[xml]$xaml = $xamlContent -replace 'x:Class=".*?"', '' -replace 'xmlns:d="http://schemas.microsoft.com/expression/blend/2008"', '' -replace 'mc:Ignorable="d"', ''
[xml]$xaml = $xamlContent -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace 'x:Class=".*?"', '' -replace 'd:DesignHeight="\d*?"', '' -replace 'd:DesignWidth="\d*?"', ''

#Read the forms in xaml
$reader = (New-Object System.Xml.XmlNodeReader $xaml) 
$form = [Windows.Markup.XamlReader]::Load($reader) 

#AutoFind all controls
$xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach-Object { 
    New-Variable  -Name $_.Name -Value $form.FindName($_.Name) -Force 
}

Function Confirm-Powercli
{
	$AllModules = Get-Module -ListAvailable VMware.PowerCLI
	if (!$AllModules)
	{
		Show-MessageBox -Message "Install VMware Powercli 12.0 or Latest. `n`nUse either 'Install-Module VMware.VimAutomation.Core' `nor download Powercli from 'http://my.vmware.com'" -Title 'VMware Powercli Missing error' | Out-Null
	}
	else
	{
		Import-Module VMware.PowerCLI
		$PowercliVer = Get-Module -ListAvailable VMware.VimAutomation.Core | Select-Object -First 1
		$ReqVersion = New-Object System.Version('12.0.0.0')
		if ($PowercliVer.Version -gt $ReqVersion)
		{
			$logTextBox.Text = ''
			$logTextBox.AppendText("VMware PowerCLI Version: $($PowercliVer.Version).`r`n")
            $logTextBox.AppendText("$('-' * 50)`r`n")
			#$textboxLogs.Text = "VMware PowerCLI Version: $($PowercliVer.Version)"
		}
		else
		{
			Show-MessageBox -Message "Install VMware Powercli 6.0 or Latest. `n`nUse either 'Install-Module VMware.VimAutomation.Core' `nor download Powercli from 'http://my.vmware.com'" -Title 'Lower version Powercli' | Out-Null
		}
	}
}

function Show-MessageBox
{
	param (
		[string]$Message = "Show user friendly Text Message",
		[string]$Title = 'Title here',
		[ValidateRange(0, 5)]
		[Int]$Button = 0,
		[ValidateSet('None', 'Hand', 'Error', 'Stop', 'Question', 'Exclamation', 'Warning', 'Asterisk', 'Information')]
		[string]$Icon = 'Error'
	)
	#Note: $Button is equl to [System.Enum]::GetNames([System.Windows.Forms.MessageBoxButtons])   
	#Note: $Icon is equl to [System.Enum]::GetNames([System.Windows.Forms.MessageBoxIcon])   
	$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::$Icon
	[System.Windows.Forms.MessageBox]::Show($Message, $Title, $Button, $MessageIcon)
}

Confirm-Powercli
function Update-DataGrid {
	param (
		[string]$ESXiName,
		[System.Windows.Controls.DataGrid]$DataGrid
	)

    $Global:Esxcli = Get-EsxCli -VMHost $listBox_ESXiList.SelectedValue
    $tcpIpCompleteInfo = $esxcli.network.ip.netstack.list.invoke()
    #$tcpIpInfo = $tcpIpCompleteInfo| Select-Object Name, NetstackInstance, Enabled, Portgroup, PortSet, MTU

    $tcpIpInfoList = New-Object System.Collections.ArrayList
    foreach ($tcpIpInfo in $tcpIpCompleteInfo)
    {
        #$Global:NetStackName = @{netstack = $tcpIpInfo.Name}
        #$tcpIpDetails = $Esxcli.network.ip.netstack.get.invoke($netStackName)
        $tcpIpDetails = $esxcli.network.ip.netstack.get.Invoke($tcpIpInfo.Name)
        
        $tcpIpInfoList += [PSCustomObject]@{
            Name = $tcpIpDetails.Name;
            Enabled = $tcpIpDetails.Enabled;
            State = $tcpIpDetails.State;
            IPv6Enabled = $tcpIpDetails.IPv6Enabled
        }
    }
    $dataGrid_TcpIpConf.Clear()
    $dataGrid_TcpIpConf.ItemsSource = $tcpIpInfoList
    $dataGrid_TcpIpConf.IsReadOnly = $true
}

$button_Login.Add_click({
    try
    {
        $Global:VCConnection = Connect-Viserver -Server $textBox_vCenter.Text -User $textBox_UserName.Text -Password $passwordBox_Password.Password -ErrorAction Stop
        $logTextBox.AppendText("Connected Server: $($VCConnection.Name) `nVersion: $($VCConnection.Version) `nUser: $($VCConnection.User)`r`n")
        $logTextBox.AppendText("$('-' * 50)`r`n")
        $button_Login.IsEnabled = $false
        $button_ESXiList.isEnabled = $true
        
        $listBox_ESXiList.Clear()
        
        $Global:ESXiList = Get-VMHost
        $listBox_ESXiList.Items.Clear()
        foreach ($esxiServer in $ESXiList) 
        {
            $listBox_ESXiList.Items.Add($esxiServer.Name)
        }
        #$listBox_ESXiList.ItemsSource = $ESXiList.Name
        $listBox_ESXiList.SelectedItem = $listBox_ESXiList.Items[0]
        $listBox_ESXiList.SelectedValue = $listBox_ESXiList.Items[0]
        
        $button_TCPIPConf.IsEnabled = $true
    }
    catch
    {
        $logTextBox.AppendText("Connection to vCenter/ESXi failed, Try again `nError: $($error[0].Exception.Message)`r`n")
        $logTextBox.AppendText("$('-' * 50)`r`n")
        $button_Login.IsEnabled = $true
    }

})

$button_ESXiList.Add_click({
    $Global:ESXiList = Get-VMHost
    $listBox_ESXiList.Items.Clear()
    foreach ($esxiServer in $ESXiList) 
    {
        $listBox_ESXiList.Items.Add($esxiServer.Name)
    }
    #$listBox_ESXiList.ItemsSource = $ESXiList.Name
    $listBox_ESXiList.SelectedItem = $listBox_ESXiList.Items[0]
    $listBox_ESXiList.SelectedValue = $listBox_ESXiList.Items[0]
    $button_TCPIPConf.IsEnabled = $true

    
})

$button_TCPIPConf.Add_click({
    <#
    $Global:Esxcli = Get-EsxCli -VMHost $listBox_ESXiList.SelectedValue
    $Global:TcpIpCompleteInfo = $esxcli.network.ip.interface.list.invoke()
    $tcpIpInfo = $tcpIpCompleteInfo| Select-Object Name, NetstackInstance, Enabled, Portgroup, PortSet, MTU

    $tcpIpInfoList = New-Object System.Collections.ArrayList
    foreach ($tcpIpInfo in $tcpIpCompleteInfo)
    {
        $tcpIpInfoList += [PSCustomObject]@{
            NetstackInstance = $tcpIpInfo.NetstackInstance;
            Name = $tcpIpInfo.Name;
            Enabled = $tcpIpInfo.Enabled;
            Portgroup = $tcpIpInfo.Portgroup;
            PortSet = $tcpIpInfo.PortSet;
            MTU = $tcpIpInfo.MTU
        }
    }
    $dataGrid_TcpIpConf.Clear()
    $dataGrid_TcpIpConf.ItemsSource = $tcpIpInfoList
    $dataGrid_TcpIpConf.IsReadOnly = $true
    #>

    Update-DataGrid -ESXiName $listBox_ESXiList.SelectedValue -DataGrid $dataGrid_TcpIpConf

    $logTextBox.AppendText("Selected ESXi host: $($listBox_ESXiList.SelectedValue)`r`n")
    $logTextBox.AppendText("$('-' * 50)`r`n")

    #foreach ($tcpip in $tcpIpInfo) 
    #{
    #    $dataGrid_TcpIpConf.Items.Add($tcpip)
    #}
    #$esxcli.network.ip.interface.list.invoke()
})

$button_Remove.Add_click({ 
    #if (($textBox_Remove.Text -match "defaultTcpipStack|Default|Provisioning|vMotion"))
    if ($textBox_Remove.Text -in 'defaultTcpipStack', 'Default', 'Provisioning', 'vMotion')
    {
        Show-MessageBox -Message "Don't try to Add or Remove TCP/IP netstack name equal to 'defaultTcpipStack', 'Default', 'Provisioning', 'vMotion' `n`n`nChoose another TCP/IP netstack" -Title 'Choose different TCP/IP stack' -Button 0 -Icon Stop
    }
    elseif (($textBox_Remove.Text -eq $null) -or ($textBox_Remove.Text.trim() -eq '')) 
    {
        Show-MessageBox -Message "Don't keep TCP/IP netstack name empty or null" -Title 'Choose correct TCP/IP stack' -Button 0 -Icon Stop
    }
    else
    {
        #$esxcli.network.ip.netstack.remove.invoke(@{netstack = 'test'})
        $interaface = $null
        $removedStack = $textBox_Remove.Text
        $vmKernals = $Esxcli.network.ip.interface.list()
        $interaface = $vmKernals | Where-Object {$_.NetstackInstance -eq $textBox_Remove.Text.Trim()}
        if (($interaface -eq $null) -or ($interaface -eq ''))
        {
            $Esxcli.network.ip.netstack.remove.invoke($textBox_Remove.Text.Trim())
        }
        else 
        {
            Show-MessageBox -Message "Can't delete TCP/IP stack'$($textBox_Remove.Text.Trim())', It is used by VMKernel '$($vmKernals.Name)'." -Title 'TCP/IP stack is in use' -Button 0 -Icon Stop
        }
        Update-DataGrid -ESXiName $listBox_ESXiList.SelectedValue -DataGrid $dataGrid_TcpIpConf
        $logTextBox.AppendText("Removed TCP/IP Stack Name: $($removedStack)`r`n")
        $logTextBox.AppendText("$('-' * 50)`r`n")
    }
})

$button_Add.Add_click({ 
    #if (($textBox_Add.Text -eq 'defaultTcpipStack' -or $textBox_Add.Text -eq 'Default' -or $textBox_Add.Text -eq 'Provisioning' -or $textBox_Add.Text - 'vMotion'))
    if ($textBox_Add.Text -in 'defaultTcpipStack', 'Default', 'Provisioning', 'vMotion')
    {
        Show-MessageBox -Message "Don't try to Add or Remove TCP/IP netstack name equal to 'defaultTcpipStack', 'Default', 'Provisioning', 'vMotion' `n`n`nChoose another TCP/IP netstack" -Title 'Choose different TCP/IP stack' -Button 0 -Icon Stop
    }
    elseif (($textBox_Add.Text -eq $null) -or ($textBox_Add.Text.trim() -eq ''))
    {
        Show-MessageBox -Message "TCP/IP netstack name shouldn't be empty or null" -Title 'Choose correct TCP/IP stack' -Button 0 -Icon Stop
    }
    elseif ($textBox_Add.Text -in $dataGrid_TcpIpConf.Items.Name)
    {
        Show-MessageBox -Message "TCP/IP netstack name with $($textBox_Add.Text) already exist" -Title 'TCP/IP stack already exist' -Button 0 -Icon Stop
    }
    else
    {
        $addedStack = $textBox_Add.Text
        $Esxcli.network.ip.netstack.add.invoke($false, $textBox_Add.Text.Trim())        
        ###---
        #$esxcliOld = Get-EsxCli -VMHost ironman.vcloud-lab.com
        #$esxcliOld.network.ip.netstack.add($false, $textBox_Add.Text.Trim())
        ###---
        #$esxcliOld.network.ip.netstack.add.invoke(@{disabled = $false; netstack = $textBox_Add.Text})
        #$Esxcli.network.ip.netstack.add.invoke($textBox_Add.Text.Trim())
        Update-DataGrid -ESXiName $listBox_ESXiList.SelectedValue -DataGrid $dataGrid_TcpIpConf
        $textBox_Add.Text = ''
        $logTextBox.AppendText("Added TCP/IP Stack Name: $addedStack`r`n")
        $logTextBox.AppendText("$('-' * 50)`r`n")
    }
    
})

$form.Add_Closing({Disconnect-VIServer * -Confirm:$false}) 
[void]$form.ShowDialog()

Useful Articles
VMware LifeCycle Manager There are conflicts from the patches selected for staging or remediation
VMware LifeCycle Manager import updates bundle and patch ESXi server
Deploy install VCSA (vCenter server appliance 6.5) on VMWare Workstation
Enable or disable SSH on VMWare vCenter Server Appliance (VCSA)
The top 10 reasons to upgrade to VMware vSphere 6.5

Go Back

Comment

Blog Search

Page Views

11271795

Follow me on Blogarama