WMI tasks for networking manage and obtain information about connections and IP or MAC addresses. For other examples, see the TechNet ScriptCenter at http://www.microsoft.com/technet.
The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.
The following procedure describes how to run a script.
To run a script
Note By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript scriptfile.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.
The following table lists script examples that can be used to obtain various types of data from the local computer.
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colNetCards = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration " _ & "Where IPEnabled = True") For Each objNetCard in colNetCards objNetCard.ReleaseDHCPLease() Next
Windows 2000: The NetConnectionID property is not available. See the following example for a way to get the IP address on earlier systems.
strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapter " _ & "Where NetConnectionID = " & _ "'Local Area Connection 2'") For Each objItem in colItems strMACAddress = objItem.MACAddress Next Set colItems = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapterConfiguration") For Each objItem in colItems If objItem.MACAddress = strMACAddress Then For Each strIPAddress in objItem.IPAddress Wscript.Echo "IP Address: " & strIPAddress Next End If Next
Windows 2000: You can get the IP address by using the Win32_NetworkAdapterSetting association class:
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colNics = objWMIService.ExecQuery _ ("Select * From Win32_NetworkAdapter " _ & "Where NetConnectionID = " & _ "'Local Area Connection'") For Each objNic in colNics Set colNicConfigs = objWMIService.ExecQuery _ ("ASSOCIATORS OF " _ & "{Win32_NetworkAdapter.DeviceID='" & _ objNic.DeviceID & "'}" & _ " WHERE AssocClass=Win32_NetworkAdapterSetting") For Each objNicConfig In colNicConfigs For Each strIPAddress in objNicConfig.IPAddress Wscript.Echo "IP Address: " & strIPAddress Next Next Next
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set IPConfigSet = objWMIService.ExecQuery _ ("Select IPAddress from Win32_NetworkAdapterConfiguration ") For Each IPConfig in IPConfigSet If Not IsNull(IPConfig.IPAddress) Then For i=LBound(IPConfig.IPAddress) _ to UBound(IPConfig.IPAddress) WScript.Echo IPConfig.IPAddress(i) Next End If Next
strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration " _ & "where IPEnabled=TRUE") For Each objNetAdapter In colNetAdapters errEnable = objNetAdapter.EnableDHCP() Next
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration " _ & "where IPEnabled=TRUE") strIPAddress = Array("192.168.1.141") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.1.100") strGatewayMetric = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic( _ strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(_ strGateway, strGatewaymetric) Next
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set IPConfigSet = objWMIService.ExecQuery _ ("Select IPAddress from Win32_NetworkAdapterConfiguration" _ & " where IPEnabled=TRUE") For Each IPConfig in IPConfigSet If Not IsNull(IPConfig.IPAddress) Then For i=LBound(IPConfig.IPAddress) _ to UBound(IPConfig.IPAddress) WScript.Echo IPConfig.IPAddress(i) Next End If Next
Starting with Windows Vista, Win32_PingStatus can return data for computers that have both IPv4 addresses and IPv6 addresses.
Windows Server 2003 and Windows XP: Win32_PingStatus returns data only for computers running IPv4. For more information, see IPv6 and IPv4 Support in WMI.
Windows 2000: Win32_PingStatus is not available. In earlier operating systems, you can determine if a computer is online by using the method illustrated in the following example.
strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") Set colPings = objWMIService.ExecQuery _ ("Select * From Win32_PingStatus where Address = '192.168.1.1'") For Each objStatus in colPings If IsNull(objStatus.StatusCode) _ or objStatus.StatusCode<>0 Then WScript.Echo "Computer did not respond." Else Wscript.Echo "Computer responded." End If Next
Windows 2000: You can determine if a computer is online by using a Windows Script Host (WSH) WSHShell object.
strComputer = "client1" Set objShell = CreateObject("WScript.Shell") Set objScriptExec = objShell.Exec( _ "ping -n 2 -w 1000 " & strComputer) strPingResults = LCase(objScriptExec.StdOut.ReadAll) If InStr(strPingResults, "reply from") Then If InStr(strPingResults, "destination net unreachable") Then WScript.Echo strComputer & "did not respond to ping." Else WScript.Echo strComputer & " responded to ping." End If Else WScript.Echo strComputer & " did not respond to ping." End If
Send comments about this topic to Microsoft
Build date: 1/8/2010
Build type: MSDN
<# .SYNOPSIS Gets and displays the IP address of a computer .DESCRIPTION This script uses Win32_NetworkAdapterConfiguration to obtain, then display, a system's IP addresses. NB: this only works on XP or later versions of Windows. .NOTES File Name : Get-IPAddress.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK Script postesd to: http://www.pshscripts.blogspot.com/ MSDN Sample at: http://msdn.microsoft.com/en-us/library/aa394590(VS.85).aspx .EXAMPLE [ps] c:\foo> .\Get-IPAddress.ps1 IP Address : 10.10.1.115 IP Address : fe80::3953:f67b:2f1c:1323 IP Address : 10.10.1.120 IP Address : fe80::d8ed:afe2:2a97:a596 4 IP addresses found on this system #> ### # Start of Script ## # Get Networking Adapter Configuration $Computer = "." $IPconfigset = Get-WmiObject Win32_NetworkAdapterConfiguration # Iterate and get IP address $count = 0 foreach ($IPConfig in $IPConfigSet) { if ($Ipconfig.IPaddress) { foreach ($addr in $Ipconfig.Ipaddress) { "IP Address : {0}" -f $addr; $count++ } } } if ($count -eq 0) {"No IP addresses found"} else {"$Count IP addresses found on this system"} #End of Script
<# .SYNOPSIS Disables active network cards .DESCRIPTION This script looks at each network card that is currently IP enabled, and disables it by releasing the DHCP Lease. To re-enable the network interrace, you just run IPCONFIG /RENEW. This script is an MSDN Sample, rewritten using PowerShell .NOTES File Name : Disable-NetworkCard.ps1 Author : Thomas Lee - tfl@psp.co.uk Requires : PowerShell V2 CTP3 .LINK PowerShell script posted to: http://www.pshscripts.blogspot.com/ Original MSDN Sample at: http://msdn.microsoft.com/en-us/library/aa394595(VS.85).aspx .EXAMPLE PSH [C:\foo]: .\Disable-NetworkCard.ps1 Releasing lease on: [00000006] Broadcom NetXtreme Gigabit Ethernet Releasing lease on: [00000012] Microsoft Virtual Network switch Adapte #> ### # Starting Script ### $Computer = "." $net = Get-WMIObject -class Win32_NetworkAdapterConfiguration -ComputerName $computer $netenabled = $net | where {$_.IPenabled} foreach ($NetCard in $netenabled) { "Releasing lease on: {0}" -f $netcard.caption $netcard.ReleaseDHCPLease() } # End of Script