Attività WMI: Rete

Attività WMI per la gestione della rete e ottenere informazioni sulle connessioni e sugli indirizzi IP o MAC. Per altri esempi, vedere TechNet ScriptCenter in https://www.microsoft.com/technet.

Gli esempi di script illustrati in questo argomento ottengono dati solo dal computer locale. Per altre informazioni su come usare lo script per ottenere dati dai computer remoti, vedere Connessione a WMI in un computer remoto.

La procedura seguente descrive come eseguire uno script.

Per eseguire uno script

  1. Copiare il codice e salvarlo in un file con estensione vbs, ad esempio filename.vbs. Assicurarsi che l'editor di testo non aggiunge un'estensione .txt al file.
  2. Aprire una finestra del prompt dei comandi e passare alla directory in cui è stato salvato il file.
  3. Digitare cscript filename.vbs al prompt dei comandi.
  4. Se non è possibile accedere a un registro eventi, verificare se si esegue da un prompt dei comandi con privilegi elevati. Alcuni log eventi, ad esempio il registro eventi di sicurezza, possono essere protetti da controlli di accesso utente (UAC).

Nota

Per impostazione predefinita, cscript visualizza l'output di uno script nella finestra del prompt dei comandi. Poiché gli script WMI possono produrre grandi quantità di output, è possibile reindirizzare l'output a un file. Digitare cscript filename.vbs outfile.txtal prompt dei comandi per reindirizzare l'output dello scriptfilename.vbs> a outfile.txt.

Nella tabella seguente sono elencati gli esempi di script che possono essere usati per ottenere vari tipi di dati dal computer locale.

Ricerca per categorie Classi o metodi WMI
... disabilitare una connessione di rete usando WMI? Se si usa DHCP, usare il Win32_NetworkAdapterConfiguration e il metodo ReleaseDHCPLease per rilasciare l'indirizzo IP. Se non si usa DHCP, non è possibile usare WMI per disabilitare una connessione di rete. Per riabilitare la connessione di rete, usare objNetCard.RenewDHCPLease. È anche possibile rilasciare o rinnovare tutti i lease DHCP usando i metodi ReleaseDHCPLeaseAll e RenewDHCPLeaseAll .
VB
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
PowerShell
$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()
}
... disabilitare o abilitare una scheda di interfaccia di rete?

Usare la classe Win32_NetworkAdapter e i metodi Disable o Enable .

... determinare quale indirizzo IP è stato assegnato a una determinata connessione di rete?

Usare la classe Win32_NetworkAdapter e la proprietà NetConnectionID per determinare l'indirizzo MAC della connessione di rete. Usare quindi la classe Win32_NetworkAdapterConfiguration per trovare l'indirizzo IP associato all'indirizzo MAC.

VB
strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapter " _ & "Where NetConnectionID = " & _ "'Local Area Connection 2'")

Per Ogni objItem in colItems strMACAddress = objItem.MACAddress Avanti

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

VB
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
... determinare l'indirizzo MAC di una scheda di rete?

Utilizzare la classe Win32_NetworkAdapterConfiguration e controllare il valore della proprietà MACAddress .

... determinare l'indirizzo IP di un computer?

Utilizzare la classe Win32_NetworkAdapterConfiguration e controllare il valore della proprietà IPAddress . Viene restituito come matrice, quindi usare un ciclo di For-Each per ottenere il valore.

VB
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
PowerShell
$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"}
... configurare un computer per iniziare a ottenere l'indirizzo IP tramite DHCP?

Usare la classe Win32_NetworkAdapterConfiguration e il metodo EnableDHCP .

VB
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
... assegnare un computer a un indirizzo IP statico?

Utilizzare la classe Win32_NetworkAdapterConfiguration e il metodo EnableStatic .

VB
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
... ottenere informazioni sulle schede di rete senza recuperare anche informazioni sulle cose come le connessioni RAS e VPN?

Usare la classe Win32_NetworkAdapterConfiguration . Nella query WQL usare questa clausola: Where IPEnabled = True.

VB
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
... ping un computer senza usare Ping.exe?

Usare la classe Win32_PingStatus .

Win32_PingStatus può restituire i dati per i computer con indirizzi IPv4 e indirizzi IPv6.

VB
strComputer = "." Set objWMIService = GetObject(_ "winmgmts:\\" & strComputer & "\root\cimv2") Set colPings = objWMIService.ExecQuery _ ("Select * From Win32_PingStatus where Address = '192.168.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

VB
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
  

Attività WMI per script e applicazioni

Esempi di applicazioni WMI C++

TechNet ScriptCenter