Friday, 16 December 2011

Disable NIC in ESX VM using PowerCLI

I was trying to understand how CRLs work in IIS 6.0, see my previous two posts here and here and I was trying to prevent communication between the CA server and the application server. I was having to go into vSphere and modify the settings of the NICs (disconnecting them), which got tiring pretty soon. So, I thought I would try to do it from the ESX console and after a bit of digging I found that it should be doable with the vmware-cmd command, like this:
vmware-cmd myCAserver..vmx disconnectdevice ethernet0
Alas, this was the result:
Traceback (most recent call last):
  File "/usr/bin/vmware-cmd", line 88, in ?
  File "/usr/bin/vmware-cmd", line 63, in main
    operationName, result = CommandProcessor.Process(host, args)
  File "/usr/lib/vmware/vmware-cmd/", line 11, in Process
    result = operation.DoIt(*processedArgs)
  File "/usr/lib/vmware/vmware-cmd/operations/", line 152, in DoIt
AttributeError: 'NoneType' object has no attribute 'SetConnected'
After a extra bit of googleing which failed to come up with a decent answer, I gave up and decided to give PowerCLI a try.

I launched PowerCLI from the start menu and connected to the VI server with:
Connect-VIServer viservername
Once Connected to the vcentre server I got both VMs and stored them in a variable with the following commands:
$CA = Get-VM –Name “myCAServer”
$app = Get-VM –Name “myAppServer”
I checked that this has grabbed the VM that I wanted by simply typing:
which returns:
Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
myCAServer           PoweredOn  1        1024           
Since both Guests have only one NIC, I got the nic details and stored in a variable for use later:
$CAnic = Get-NetworkAdapter $CA
$appnic = Get-NetworkAdapter $app
Now I can use Set-NetworkAdapter to switch them on or off at will. To turn off the CA Server’s NIC:
$CAnic = Set-NetworkAdapter –NetworkAdapter $CAnic –confirm:$false –connected:$false
And then back on:
$CAnic = Set-NetworkAdapter –NetworkAdapter $CAnic –confirm:$false –connected:$true
I can now check the status with:
which returns:
         AllowGuestControl                  Connected            StartConnected
         -----------------                  ---------            --------------
                      True                      False                      True
Remember that command completion works pretty well, so you could just type $CAnic. And then keep pressing TAB. Similarly, if you are unsure about the switches for a cmdlet, just type - and then TAB through the options.

It is also possible to pipe commands so that you don't need to use several variables, e.g.
$CAnic = Get-VM -Name "myCAServer" | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$false -confirm:$false
I still use a variable, so that I can easily check the status of the NIC, but it's not necessary, I could have simply used:
 Get-VM -Name "myCAServer" | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$false -confirm:$false
I find that using variables is easier for a beginner such as myself. At any rate, I'm glad I've given PowerCLI a go as it is really good.

No comments:

Post a Comment