VMware Cloud Community
tigerdeccan
Enthusiast
Enthusiast

Adding a vMotion Switch and network adapter config from text files

Hi ,

I am trying to add a vmkernel switch and network adapter , I am reading host names and ip addresses from two diferent text files .

In my code I've nested the foreach loop.

The loop is not seem  to work . Can anyone help me , I have idea of using delimiter and use CSV file rather than text file

my code is as follows

Connect-VIServer

$esxs

= Get-Content c:\scripts\test.txt

$ips

= Get-Content c:\scripts\ip.txt

foreach

($esxs in $esxs)

{

foreach

($ips in $ips)

{

$vswitch

= New-VirtualSwitch -VMHost $esxs -Name "vMotion" -Nic vmnic2

New-VMHostNetworkAdapter

-VMHost $esxs -PortGroup "vMotion" -VirtualSwitch $vswitch -IP $ips -SubnetMask 255.255.255.0 -VMotionEnabled $true VLanID 4000

}

}

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

I think you better use a CSV file, then you place the related data in 1 row in the CSV file.

Your CSV could have a layout like this

HostName,ip

esx1,192.168.1.1

esx2,192.168.1.2

esx3,192.168.1.3

And your script could then be

Connect-VIServer Import-Csv c:\scripts\test.csv -UseCulture | %{
    $vswitch = New-VirtualSwitch -VMHost $_.HostName -Name "vMotion" -Nic vmnic2
    New-VMHostNetworkAdapter -VMHost $_.HostName -PortGroup "vMotion" -VirtualSwitch $vswitch -IP $.ip -SubnetMask 255.255.255.0 -VMotionEnabled $true VLanID 4000
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos