Link
#Import-Module VMware.VimAutomation.Core
#如果使用 6.5.0 之前版本,需要先导入模块

$vmktable = Import-CSV .\add_vmkernel.csv 
#导入 VMKernel 配置文件

$selectedlocation="Datacenter"
#此处可以设置 Datacenter 名称或者 Cluster 名称以限制范围

$vswitch = "vSwitch0"
#选择虚拟交换机

$VMHostlist = Get-VMHost -location $selectedlocation

Foreach ($VMhost in $VMHostlist) 
    {
        $VMhostMgmtVmk=Get-vmhost $VMhost | Get-VMHostNetworkAdapter -VMKernel | where {$_.ManagementTrafficEnabled -eq $true } 
        Write-Host  "Current VMhost Mamagement ip is :" $VMhostMgmtVmk.ip
        
        $CurrentVmkRow = $vmktable | where {$_.Esxi_Host -eq $VMhostMgmtVmk.ip} 
      
        Write-Host "Adding vSAN vmk" $CurrentVmkRow.vSAN_IP "to" $CurrentVmkRow.Esxi_Host -foregroundcolor green
        
       Get-VMHost $vmhost| New-VMHostNetworkAdapter -VirtualSwitch $vSwitch -PortGroup $CurrentVmkRow.vSAN_Label  -IP $CurrentVmkRow.vSAN_IP -SubnetMask $CurrentVmkRow.SubnetMask -MTU $CurrentVmkRow.MTU  -VsanTrafficEnabled $true| out-null
        Get-VMHost $vmhost| Get-virtualportgroup -name $CurrentVmkRow.vSAN_Label | Set-VirtualPortGroup -Name $CurrentVmkRow.vSAN_Label -VLANID $CurrentVmkRow.vSAN_VLAN_ID | out-null
        
        Write-Host "Adding vMotion vmk" $CurrentVmkRow.vMotion_IP "to" $CurrentVmkRow.Esxi_Host -foregroundcolor green
        
        Get-VMHost $vmhost| New-VMHostNetworkAdapter -VirtualSwitch $vSwitch -PortGroup $CurrentVmkRow.vMotion_Label  -IP $CurrentVmkRow.vMotion_IP -SubnetMask $CurrentVmkRow.SubnetMask -MTU $CurrentVmkRow.MTU  -VMotionEnabled $true| out-null
        Get-VMHost $vmhost| Get-virtualportgroup -name $CurrentVmkRow.vMotion_Label | Set-VirtualPortGroup -Name $CurrentVmkRow.vMotion_Label -VLANID $CurrentVmkRow.vMotion_VLAN_ID | out-null
           
    }

csv 文件示例:

文件名 add_vmkernel.csv ,需要在运行时放在 powershell 运行目录下。

Esxi_Host,vSAN_IP,vMotion_IP,vSAN_VLAN_ID,vSAN_Label,vMotion_VLAN_ID,vMotion_Label,SubnetMask,MTU
172.16.1.1,172.16.2.1,172.16.3.1,1015,VMKernel-vSAN,1012,VMKernel-vMotion,255.255.255.0,1500
172.16.1.2,172.16.2.2,172.16.3.2,1015,VMKernel-vSAN,1012,VMKernel-vMotion,255.255.255.0,1500
172.16.1.3,172.16.2.3,172.16.3.3,1015,VMKernel-vSAN,1012,VMKernel-vMotion,255.255.255.0,1500