VMware Cloud Community
ccavanna
Contributor
Contributor
Jump to solution

Network Change Automation

I am trying to write a script to help with some major network changes that I am in charge of. The script I am writing should do the following things...

Connect through Mgmt Network2

Remove vmnic2 from vSwitch0

Create vSwitch3

Add several portgroups to vSwitch3

Down the nic's on the Mgmt Network

Restart the Mgmt Network

Sleep 60 seconds

Connect back to Mgmt Network2

Set the vlanid on Mgmt Network

Change the vmk ip of the Mgmt Network

So here is what I have so far... I do believe that I am on the right track but I feel like I may not be going about this the right way. I am also having issue's creating the portgroups on vSwitch3. Any help is greatly appreciated!

Connect-VIServer -server xx.xx.xx.xx

$VMhost = Get-VMHost -Name xx.xx.xx.xx

$vmnic0 = "vmnic0"

$vmnic1 = "vmnic1"

$vmnic2 = "vmnic2"

$vmnic3 = "vmnic3"

$VMhostMgmt2 = Get-VMHost -name xx.xx.xx.xx

$vSwitch3 = "vSwitch3"

$VMhost | Get-VirtualSwitch | Where-Object {$_.Name -eq "vSwitch0" -and $_.Nic -eq "vmnic2"} | Foreach {

  If ($VMHost | Get-VirtualSwitch -Name $_.Name) {

  Get-VirtualSwitch -Name $_.Name | Set-VirtualSwitch -Nic $vmnic0, $vmnic1 -confirm:$false

  }

}

$VMhost | Get-VirtualSwitch | Where-Object {$_.Name -ne "vSwitch3" } | Foreach {

  If ($VMHost | Get-VirtualSwitch -Name $_.Name) {

  New-VirtualSwitch -VMHost $VMhost -Name vSwitch3 -NumPorts 128 -Mtu 1500 -Nic $vmnic2, $vmnic3 -confirm:$false

  New-VirtualPortGroup -Name xx.xx.xx.xx -VirtualSwitch $vSwitch3 -VLanId 10 -confirm:$false

  New-VirtualPortGroup -Name xx.xx.xx.xx -VirtualSwitch $vSwitch3 -VLanId 12 -confirm:$false

  New-VirtualPortGroup -Name xx.xx.xx.xx -VirtualSwitch $vSwitch3 -VLanId 40 -confirm:$false

  }

}

Get-EsxCli -vmhost $VMhost

$esxcli.network.nic.down($vmnic0)

$esxcli.network.nic.down($vmnic1)

Get-VMHostService -VMHost $VMhost |where {$_.Key -eq "vpxa"} | Restart-VMHostService -Confirm:$false

start-sleep -s 60

connect-VIServer -server xx.xx.xx.xx

start-sleep -s 60

$VMhostMgmt2 | Get-VirtualSwitch | Where-Object {$_.Name -eq "vSwitch0"} | foreach {

  If (Get-VirtualSwitch -name $_.Name) {

  Set-VirtualPortGroup -Name "Management Network" -VLanId 19 -confirm:$false

  }

}

Get-VMHostNetworkAdapter | where {$_.Name -eq "vmk0"} | foreach {

  If (Get-VMHostNetworkAdapter -VMHost $VMhostMgmt2 -Name $_.Name){

  Get-VMHost $VMhostMgmt2 | Get-VMHostNetworkAdapter -Name $_.Name | Set-VMHostNetworkAdapter -IP xx.xx.xx.xx -SubnetMask 255.255.255.0 -Confirm:$false

  }

}

Get-VMHostService -VMHost $VMhostMgmt2 |where {$_.Key -eq "vpxa"} | Restart-VMHostService -Confirm:$false

Get-EsxCli -vmhost $VMhost

$esxcli.network.nic.up($vmnic0)

$esxcli.network.nic.up($vmnic1)

Get-VMHostService -VMHost $VMhost |where {$_.Key -eq "vpxa"} | Restart-VMHostService -Confirm:$false

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It looks like you might have more than one connection open.

Display the contents from

$global:DefaultVIServers

If there is more than one value returned, and if you are working in "multiple" mode (see Set-PowerCLiConfiguration), that is most probably the cause of the error


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

What issue are you having ? Are there any error messages ?


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

0 Kudos
ccavanna
Contributor
Contributor
Jump to solution

New-VirtualPortGroup : 9/20/2013 5:17:05 PM    New-VirtualPortGroup        The specified parameter 'VirtualSwitch' expects a single value, but your name criteria

'vSwitch3' corresponds to multiple values.

At C:\Users\ccavanna\Desktop\VMware Scripts\vlan-reconfig-script.ps1:35 char:1

+ New-VirtualPortGroup -Name xx.xx.xx.xx -VirtualSwitch $vSwitch3 -VLanId 10 -confi ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidResult: (System.Collecti...dObjectInterop]:List`1) [New-VirtualPortGroup], VimException

    + FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_MoreResultsThanExpected,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewVirtualPortGroup

Also having some issues with the script reconnecting to the host after downing the nic's on the Mgmt Network. So that I can finish running the commands through Mgmt Network2 and have it complete successfully. I am running PowerCLI 5.1 R2 against a host that is 5.0 Update 2 Build 914586.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks like you might have more than one connection open.

Display the contents from

$global:DefaultVIServers

If there is more than one value returned, and if you are working in "multiple" mode (see Set-PowerCLiConfiguration), that is most probably the cause of the error


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

0 Kudos