Friday, January 6, 2012

Powershell -- Enable/disable LAN card

$network = get-wmiobject win32_networkadapter | where-object {$_.netconnectionID -like "*IBM USB*"}
$network.disable() ##use enable() to enable card.

How did I find above script.

Above script is not much great, anyone can find it, but here I am posting it for beginners, who have some foundation knowledge but don't know where to begin, so here are some of the pointers. I even posted how I went wrong and how I corrected myself. 

Here i go.
Gets instances of Windows Management Instrumentation (WMI) classes or information about the available classes

I usually use Get-WmiObject to query hardware information, after putting series of below wrong command,
c:\get-wmiobject | where-object {$ -like "*network*"}
c:\get-wmiobject list | where-object {$ -like "*network*"}

Got my mistake where I am wrong and ran below command. which gave me huge list of classes (objects)
c:\>get-wmiobject –list
c:\>get-wmiobject -list | where-object {$ -like "*network*"} #To find only network related classes/objects

Tried first object, win32_networkadapter (I thought this will be the same object as I want, where I can find network adapter information) and ran   
c:\>get-wmiobject win32_networkadapter

Ran series of command to find network adapter name “IBM USB”  but no success.
c:\>get-wmiobject win32_networkadapter | ft –auto
c:\>get-wmiobject win32_networkadapter | where-object {$ -like "*IBM USB*"} # * used as a wild card

Ran get-member to list properties and method for win32_networkadapter class.
c:\>get-wmiobject win32_networkadapter | get-member

But still I was not sure where can I find property containing “IBM USB” and tried some of the properties
c:\>get-wmiobject win32_networkadapter | ft name
c:\>get-wmiobject win32_networkadapter | ft systemname

I got an idea, and exported all the properties in csv file, after lots of spelling mistakes and wrong commands I successfully captured data.
c:\>get-wmiobject win32_networkadapter | ft * | exprot-csv -path test.csv # Spelling mistakes (exprot-csv)
c:\>get-wmiobject win32_networkadapter | ft * | export-csv -path test.csv # Ft command not needed here
c:\>get-wmiobject win32_networkadapter | export-csv -path test.csv #success

I opened test.csv file and found NetconnectionID is the property where I can find desired network adapter (This is the only column contains "IBM USB"), again did some more mistakes, and I was getting either null data or errors.
c:\>get-wmiobject win32_networkadapter | where-object {$_.netconnectID -like "*IBM USB*"}
c:\>get-wmiobject win32_networkadapter | where-object {$_.netconnectID -match "*IBM USB*"}
c:\>get-wmiobject win32_networkadapter | where-object {$_.netconnectID -match "*IBM USB*"} # spelling mistake ($_.netconnectID)

Ran below corrected command and compared Macaddress from powershell console and network adapter GUI, both were same.
c:\>get-wmiobject win32_networkadapter | where-object {$_.netconnectionID -like "*IBM USB*"} 

To find out how to enable or disable this network card ran get-member.
c:\>get-wmiobject win32_networkadapter | where-object {$_.netconnectionID -like "*IBM USB*"} | get-member

 Final Script as below, saved networkadapter details in variable called "nic" then tried it with Disable() to check and it worked, to enable card use enable() method. once you enable or disable verify it from network connections in control panel.

c:\> $nic = get-wmiobject win32_networkadapter | where-object {$_.netconnectionID -like "*IBM USB*"}

When running this script open network connections and you can check results.

***Have a nice scripting***


No comments: