VMware Cloud Community
AviR
Contributor
Contributor

Disabling VMGuest Network Using PowerCLI

Hi Guys,

So I have a bit of a scenario on my hands here, I have a DR trial coming up and require some assistance with something.

I need to be able to disable one of 2 network cards across all VMs, That will be provided to me in CSV format.

I know that you may use this below like before power on...

$nadpter = Get-VM test01 | Get-NetworkAdapter -name "Network adapter 2"

$nadpter | Set-NetworkAdapter -startconnected:$false -Confirm:$false

So that would literally pull the proverbial network cable out of the NIC before startup.

What I really really want to know is... Can you use the command -VmGuestNetworkInterface  to select a named NIC within the OS....

Example if i wanted to select and disable DRNIC as shown in the below image.

Capture.PNG

Thanks in advance for the time and effort. Smiley Happy

0 Kudos
7 Replies
LucD
Leadership
Leadership

I'm afraid not Smiley Sad

First, the VmGuestNetworkInterface cmdlets are only supported on a limited set of guest OS.

Second, the Set-VmGuestNetworkInterface has no option to disable a vNIC inside the guest OS.

But, do you have the VMware Tools installed in your VMs, and do you run a Windows OS in the VM ?

If yes, you could use the Invoke-VmScript cmdlet to fire off a netsh command inside the guest OS to disable the NIC.

The netsh command would be something like this

netsh interface set interface name="Local Area Connection" admin=disabled

Alternatives are to use the WMI interface or remote PowerShell, but that requires that you are allowed to do a WSMAN connection to the guest.


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

0 Kudos
AviR
Contributor
Contributor

Not to look excessively lazy... Just time constrained. Would you mind giving me a simple example that I can pop into a loop?

0 Kudos
LucD
Leadership
Leadership

You would first need to determine how you can link the vNIC name ("Network adapter 2") to the name of the NIC inside the guest ("Local Area Connection").

If you can't devise a solid rule for that, there is no way to use this method.


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

0 Kudos
AviR
Contributor
Contributor

We currently have a standard in place where the we name the first and second vNIC internally with their relevant names.

Basically we name the first vNIC within the OS DCNIC1 and the secondary vNIC DRNIC1. If that is what you are asking.

0 Kudos
LucD
Leadership
Leadership

Yes, that is what I mean. And I guess you want to disable the 2nd NIC ?

You could do

Get-VM | where {Get-NetworkAdapter -name "Network adapter 2"} | %{

   Invoke-VMScript -VM $_ -GuestCredential $cred -ScriptText "netsh interface set interface name="DRNIC1" admin=disabled"

}

The script check if there is a vNIC called "Network adapter 2", and if there is, it tries to disable the NIC inside the guest OS


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

AviR
Contributor
Contributor

Thanks LucD,

I shall give a swing ASAP and post back. Thank you for being so quick with your responses. I am relatively new to this and what I know is what google told me Smiley Happy

Thanks again!

0 Kudos
AviR
Contributor
Contributor

Hi,

Thank you for pointing me in the correct direction with the Invoke-VMScript  Smiley Happy

Since our environment NICS are named per standard it wasn't difficult.

So I wrote a function - Or modified one that I found online and it looks a like this.....

$names = Import-Csv -Path .\VMS.csv                                                                                                                                    #Get List from CSV File (attached) in working directory, or specify path!

$GuestCred = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for $VM", "", "")  #Get Admin Creds also Standard Across environment

$names | foreach {Disable-NIC2 (Get-VM $_.Name) $GuestCred}                                                                                       # Loop for each VM name in VMS.csv

Function Disable-NIC ($VM, $GC){                                                                                                                                         #Passing Creds and VM Names to Function

$netsh = 'c:\windows\system32\netsh interface set interface name="NIC2" admin=enabled'                                       #Set Commands for Invoke-VMScript - Disable Adapter

$nicCheck = 'c:\windows\system32\netsh interface show interface name="NIC2"'                                                        #Set Commands for Invoke-VMScript - Check Adapter State

Write-Host "DISABLING DRNIC1!"

Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType bat -ScriptText $netsh                                                    #Disable Guest NIC1

Invoke-VMScript -VM $VM -GuestCredential $GC -ScriptType bat -ScriptText $nicCheck                                             #Will display  Guest NIC1 Administrative State

}

So i still need to work out an out file to display the VM Name and Administrative state (enabled or Disabled) for housekeeping afterwards.

Thanks again for the assist!!!

0 Kudos