Powershell

I was helping out in the forums when a Question popped.

“Can I programatically disable a Network Card?”

The gentleman had a system that would have Full Domain access (for regular users) and occasionally somebody would need to work on the machine as a Guest and be guaranteed to have no Network access.

Although there are many things you can do to REALLY restrict network access; Hands down NOTHING beats pulling the LAN cable!

So doing it via software?

Turns out with WMI it’s VERY easy.  You just need to run as Administrator and execute the following command.

GET-WMIOBJECT WIN32-NETWORKADAPTER | foreach { $-.Disable() }

which will shutdown *ALL* network adapters (Physical, Logical, ALL! or

GET-WMIOBJECT WIN32-NETWORKADAPTER | foreach { $-.Enable() }

to Enable them all again

You CAN get more specific and isolate that to only physical adapters by adding in a WHERE-OBJECT filter like this since the WIN32-NETWORKADAPTER has a property called “PhysicalAdapter” which is Boolean “True” or “False” on whether it’s a “Physical Adapter”.  

GET-WMIOBJECT WIN32-NETWORKADAPTER | where { $-.PhysicalAdapter –eq $TRUE} | foreach { $-.Disable() }

to only shut off Physical Adapters.

It’s not a typical scenario and this CAN be done with VbScript too if you prefer by using this script for the Blanket approach

———– Disable All Network Adapters ————–
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" –
    & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32-NetworkAdapter")
For Each objItem in colItems
    objitem.Disable()
Next
———– Disable All Network Adapters ———–

But honestly for just DEVELOPING the vbScript portion of it Powershell was STILL the King since it allows me interactively test and work with WMI with “open notepad, edit, save, run, curse, do it again…”

 

Powershell, making life easier one Cmdlet at a time

Sean
The Energized Tech