Raceeend
(Starting to like KiXtart)
2003-07-23 10:01 AM
Finding NIC status

Hello,

I'm busy making a scripts which has to check the NIC status in a laptop. This is because sometimes when a user pulls out the NIC cable when the laptop is still shutting down the nic gets disabled. Which result in a call to the helpdesk and a lot of tests to see what is wrong.

This part of code i tried to check the status of the NIC but this is not returned. The description is working but not the status.
code:
? @KIX
$i = 0
For Each $Device In WMIQuery("DeviceID","Win32_NetworkAdapter",$Laptop)
$Status = WMIQuery("Status","Win32_NetworkAdapter",$Laptop)[$i]
$Description = WMIQuery("Description","Win32_NetworkAdapter",$Laptop)[$i]
? $Description+Chr(9)+$Status
$i = 1+$i
Next



[ 23. July 2003, 10:46: Message edited by: Raceeend ]


NTDOCAdministrator
(KiX Master)
2003-07-23 10:12 AM
Re: Finding NIC status

Where is the var $Laptop defined? I hope/assume it is somewhere else in the script!! If not, don't see how the code does anything.

May also want to change $i = 1+$i to $i=$i+1


Raceeend
(Starting to like KiXtart)
2003-07-23 10:14 AM
Re: Finding NIC status

$Laptop = @wksta

It shows all the NIC's just not the status.


NTDOCAdministrator
(KiX Master)
2003-07-23 10:27 AM
Re: Finding NIC status

Maybe try this code. It works for me on my system.

Give it a try and let me know.





break on
$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_NetworkConnection",,48)
For Each $objItem in $colItems
? "AccessMask: " + $objItem.AccessMask
? "Caption: " + $objItem.Caption
? "Comment: " + $objItem.Comment
? "ConnectionState: " + $objItem.ConnectionState
? "ConnectionType: " + $objItem.ConnectionType
? "Description: " + $objItem.Description
? "DisplayType: " + $objItem.DisplayType
? "InstallDate: " + $objItem.InstallDate
? "LocalName: " + $objItem.LocalName
? "Name: " + $objItem.Name
? "Persistent: " + $objItem.Persistent
? "ProviderName: " + $objItem.ProviderName
? "RemoteName: " + $objItem.RemoteName
? "RemotePath: " + $objItem.RemotePath
? "ResourceType: " + $objItem.ResourceType
? "Status: " + $objItem.Status
? "UserName: " + $objItem.UserName
Next



Raceeend
(Starting to like KiXtart)
2003-07-23 10:49 AM
Re: Finding NIC status

Edited $Status = WMIQuery("Status","Win32_NetworkConnection",$Laptop)[$i] to $Status = WMIQuery("Status","Win32_NetworkAdapter",$Laptop)[$i]

But this is still not working.

Thanx for the code NTDOC but i need NetworkAdapter and if i use that in your code then it gets the same result: Not working.


NTDOCAdministrator
(KiX Master)
2003-07-23 11:35 AM
Re: Finding NIC status

This works for me at work, but not here at home.


; From this information
; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_networkadapter.asp
break on
$Index=0
$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
For Each $objItem in $colItems
$Index=$index+1
IF $objItem.AdapterType='Ethernet 802.3'
? 'Description: ' + $objItem.Description
? 'Adapter Type: ' + $objItem.AdapterType
? 'Name: ' + $objItem.Name
? 'Status: ' + $objItem.NetConnectionStatus
ENDIF
Next


Raceeend
(Starting to like KiXtart)
2003-07-23 01:19 PM
Re: Finding NIC status

If i changes:
? 'Status: ' + $objItem.NetConnectionStatus

to:
? 'Status: ' + $objItem.Status

it works....on Winnt but not on w2k :-(

Could it be that the winnt comp has a static ip and the w2k dynamic? Or are there any other differences?


Chris S.
(MM club member)
2003-07-23 02:26 PM
Re: Finding NIC status

If you want to check if a network cable has been removed, you may want to check NetworkAdapterConfiguration instead.



Break On
$strComputer = "."
$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")
$colItems = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration",,48)
For each $objItem in $colItems
  $aIPAddress = $objItem.IPAddress
  For Each $Address in $aIPAddress
      If Len($Address$Address ? EndIf
  Next
Next



If the cable is pulled, no device will have an IPAddress.


Raceeend
(Starting to like KiXtart)
2003-07-23 02:52 PM
Re: Finding NIC status

I want to check if the NIC is disabled. I guess i have to do that with Win32_NetworkAdapter.

Richie19Rich77
(Seasoned Scripter)
2003-07-24 11:44 AM
Re: Finding NIC status

How can you check NIC is disabled, surly you wont get no responce.

How how can you tell the difference between NIC disabled and LAN Cable unplugged.

Unless the laptop has 2 NIC cards ??? [Smile] .

Correct me if I am wrong, just don't sound possible to me.

It would be like trying to write a script to check if a PC has been stolen, by checking it's NIC status [Big Grin]

Thanks

Rich


JochenAdministrator
(KiX Supporter)
2003-07-25 12:23 AM
Re: Finding NIC status

maybe with some snmp scripting ? [Roll Eyes]

Sealeopard
(KiX Master)
2003-07-24 06:22 PM
Re: Finding NIC status

"Disabled" and "Unplugged" should be two different states.

Chris S.
(MM club member)
2003-07-24 06:41 PM
Re: Finding NIC status

That's why I made my suggestion. If the network cable gets unplugged the NIC will lose it's IP Address. I verified this on my PC.

BTW, my example is an exerpt from the Trigger Your Logon Script - WMI discussion.


Raceeend
(Starting to like KiXtart)
2003-07-25 11:24 AM
Re: Finding NIC status

quote:
"Disabled" and "Unplugged" should be two different states.

That's what i think, but for now i don't even get a result when the nic is functioning. Have to search some more.


Sealeopard
(KiX Master)
2003-07-25 05:04 PM
Re: Finding NIC status

Try Win32_NetworkConnection->ConnectionState
quote:
Current state of the network connection.

Values are:
"Connected"
"Error"
"Paused"
"Disconnected"
"Connecting"
"Reconnecting"

from the WMI SDK Documentation


Raceeend
(Starting to like KiXtart)
2003-07-30 03:52 PM
Re: Finding NIC status

It's not working

code:
? @KIX
$i = 0
For Each $Device In WMIQuery("DeviceID","Win32_NetworkAdapter","@WKSTA")
$Status = WMIQuery("ConnectionState","Win32_NetWorkConnection")[$i]
;if $Status = "Connected"
$Description = WMIQuery("Description","Win32_NetworkAdapter","@WKSTA")[$i]
? $Description+Chr(9)+$Status ;+Chr(9)+$i
;EndIf
$i = 1+$i
Next

output: on a Win2k pro station (laptop)
4.20
Infrared Port Connected
Infrared Modem Port Disconnected
RAS Async Adapter Disconnected
WAN Miniport (L2TP) Disconnected
WAN Miniport (PPTP) Connected
Direct Parallel
WAN Miniport (IP)
Intel(R) PRO/100 VE Network Connection
Deterministic Network Enhancer Miniport
Deterministic Network Enhancer Miniport

output: on a Winnt 4.0 Station

4.20
Intel(R) PRO/100 VM Network Connection [Bus 2 Slot 8] Connected

This is weird. That the LAN interface on the laptop doesn't show any state and the one in the NT machine does.

[ 30. July 2003, 15:53: Message edited by: Raceeend ]