2009-03-11

Powershell: Toggle network connection status on virtual machines in VMware

I have a network that is separated from the Internet, and in this network, I have a virtual machine, running Microsoft Windows Server Update Services (WSUS)

The virtual machine has two network card, and when I want to synchronize, I toggle the connection state on both adapters, disabling the internal one, and enabling the one connected to the Internet.

As we want to use Microsoft Forefront Client Security (FCS) at this site, the WSUS server should connect to the Internet couple of times a day to be able to download new virus definitions. This is not feasible to do manually, so I had to automate this.

February 2008, I attended VMWorld Europe in Cannes, and was introduced to the VMware Infrasructure Toolkit (for Windows). Until today, I have found no use for this at work, but I was going to give it a try. In fact, I had almost no experience in Powershell at all, and had to download and install Powershell first.

I used the interactive Powershell and quickly found Get-NetworkAdapter. Some trial and error due to unfamiliar syntax of the script language, but I found a way to loop through the adapters and printing out the status. What a disappointment. All I got, disregarding the connection state of the adapter, was this: VMware.VimAutomation.Client20.ConnectInfoImpl

Some minutes later, by the help of Mr Google, I found that I could use $adapter.ConnectionState.Connected as a boolean variable.

Some more minutes, fidling around in the new world of this toolkit and Powershell, I had a working script (no syntax highlighting yet, I have not found a highlighter for Powershell...) :

# Toggle connection state on NICs on a virtual machine
#
# Usage:
# powershell.exe -psc "C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" -noprofile -c ". \"C:\path to\script\toggleConnectedNIC.ps1\""
#
# Copyright (c) 2009 Rune Nordbøe Skillingstad <rune.skillingstad@ntnu.no>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.


# Change this to your VI server
$VIServer = "localhost"
# Change this to the virtual machine you want to toggle connection on
$ComputerName = "domain-wsus.domain.com"

# Add -User <username> -Password <password> if needed
$server = Connect-VIServer -Server $VIServer -Protocol https

$adapters = Get-NetworkAdapter $ComputerName

foreach($adapter in $adapters) {
If(!$adapter.ConnectionState.Connected) {
Write-Host "Enabling " $adapter.NetworkName
$ignore = Set-NetworkAdapter $adapter -Connected:$true -Confirm:$false
} else {
Write-Host "Disabling " $adapter.NetworkName
$ignore = Set-NetworkAdapter $adapter -Connected:$false -Confirm:$false
}
}

1 comment:

  1. I would suggest using the latest version of this:
    http://runesk.blogspot.com/2009/05/powershell-toggle-network-connection.html

    ReplyDelete