Re: Win32_NetworkConnection returns empty

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 10/26/04


Date: Tue, 26 Oct 2004 17:04:23 +0200

Dharani Babu S wrote:

> Hi all
> I am trying to find out whether a system is connected to network or not . So
> I thought ConnectionState property of Win32_NetworkConnection will give me
> the status of the system . But when I executed the ExecQuery( "select * from
> Win32_NetworkConnection " or "select ConnectionState from
> Win32_NetworkConnection ") it returns with no values . It does not return
> NULL also . Could anyone point tome the problem ? and is
> Win32_NetworkConnection the right class to find out the presence of a system
> in a network ?
Hi

Note that Win32_NetworkConnection only represents mapped drives, and
not if your system is connected to a network or not.

Some alternatives maybe:

Windows XP and beyond:

'--------------------8<----------------------
sComputerName = "." ' Use "." for local computer
Set oWMIService = GetObject("winmgmts:\\" & sComputerName & "\root\cimv2")

For Each oAdapter In oWMIService.InstancesOf("Win32_NetworkAdapter")
   If oAdapter.NetConnectionStatus = 2 Then
     WScript.Echo "Network connection """ & oAdapter.NetConnectionID _
                & """ is connected"
   End If
Next
'--------------------8<----------------------

Windows 2000 and beyond:

'--------------------8<----------------------
sComputerName = "." ' Use "." for local computer

Set oWMIService = GetObject("winmgmts:\\" & sComputerName & "\root\wmi")
Set oNicCollection = oWMIService.InstancesOf("MSNdis_MediaConnectStatus")

For Each oNic In oNicCollection
   If oNic.NdisMediaConnectStatus = 0 Then
     sNicStatus = "connected"
   Elseif oNic.NdisMediaConnectStatus = 1 Then
     sNicStatus = "disconnected"
   Else
     sNicStatus = "unknown"
   End If

   WScript.Echo "Status of " & oNic.InstanceName & ": " & sNicStatus
Next
'--------------------8<----------------------

-- 
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx


Relevant Pages

  • Win32_NetworkConnection returns empty
    ... I am trying to find out whether a system is connected to network or not. ... I thought ConnectionState property of Win32_NetworkConnection will give me ... Dharani Babu S ...
    (microsoft.public.win32.programmer.wmi)
  • Re: SCAN.EXE - McAfee AntiVirus Software
    ... I have two quick questions about McAfee AntiVirus's SCAN.EXE: ... small network is providing a tiny bit of resistance. ... wide variety of scripting tasks on all network ... > McAfee workstations and for McAfee Configuration Management. ...
    (microsoft.public.security.virus)
  • Re: restrict software installation
    ... Preventing users from executing stuff on their home directory and in ... Is there some similar principle in Windows? ... emphasis on 'minimize network transfers'. ... Perhaps you can control this via some scripting stuff? ...
    (Focus-Microsoft)
  • Re: 0x800a0046
    ... I noticed just recently that i can not use windows update manually after ... > newsgroup because my dialup network settings had vanished and a guy ... > Notice that the logon account is set to a NT/User account under the RPC ... > torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway ...
    (microsoft.public.windowsupdate)
  • Re: 0x800a0046
    ... I take no credit for this I was going though the network ... Notice that the logon account is set to a NT/User account under the RPC ... -- torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.windowsupdate)