2017-02-27 8 views
0

.Execを使用して、マシンのIP構成を取り戻そうとしています。指定されたコマンドの出力から、私はすべての無線LANおよびイーサネットLAN IPv4アドレスまたは存在しない場合は「メディアが切断されています」を返します。私はかなりユーザーが複数のNICを持つことができるようにこれにアプローチする方法にかなり固執しています。私は出力を得ることができますが、私は必要な情報を格納するためにipconfigの結果を反復する方法がわかりません。VBScriptを使用して変数としてipv4アドレスを返す

Set objShell = CreateObject("WScript.Shell") 
StrIPConfig = "ipconfig /all" 
Set IPConfig = objShell.Exec(StrIPConfig) 

strText = "" 

Do While Not IPConfig.StdOut.AtEndOfStream 
    strText = IPConfig.StdOut.Readline 
    If InStr(strText, "Wireless Network Connection") Then 
     strWLAN = IPConfig.StdOut.Readline +2 
     WScript.Echo strWLAN 
    End If 
Loop 

Do While Not IPConfig.StdOut.AtEndOfStream 
    strText = IPConfig.StdOut.Readline 
    If InStr(strText, "Local Area Connection") Then 
     strLAN = IPConfig.StdOut.Readline +2 
     WScript.Echo strWLAN 
    End If 
Loop 

は、上記のようExecを使用してWMI WindowsバッチファイルやWMI VBScriptまたはVBScriptのを介して行われる必要があります。残念ながら、PowerShellはオプションではありません。代わりに、砲撃の

+1

はの出力を解析するよりも簡単である可能性が高いです'ipconfig'.See http://serverfault.com/a/55437/1440 – Richard

答えて

0

使用WMI(特にWin32_NetworkAdapterConfigurationクラス):

Set wmi = GetObject("winmgmgts://./root/cimv2") 

qry = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True" 

For Each nic In wmi.ExecQuery(qry) 
    For Each addr In nic.IPAddress 
    WScript.Echo addr 
    Next 
Next 
0

私は、VBScriptで包まれたWMIスクリプトを使用して、別のフォーラムからの回答を適応しています。それは現在、しかし、今に適応するのは簡単だろう文字列などの情報を記憶している:あなたは必要な情報だけを返すWMIクエリを実行するために、 `wmic`を使用し

strWLAN = "" 
strLAN = "" 
strComputer = "." 

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set IPAdapterSet = objWMIService.ExecQuery("Select AdapterType, NetConnectionID, MACAddress from Win32_NetworkAdapter WHERE NetConnectionID LIKE 'Wireless network%'") 


For Each IPConfig in IPAdapterSet 
Set IPConfigSet = objWMIService.ExecQuery("Select IPEnabled, Description, Caption, IPAddress, MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='True' AND MACAddress = '" & IPConfig.MACAddress & "' ") 
    For Each IPConf in IPConfigSet 
    if NOT strWLAN="" then 
     strWLAN = strWLAN & vbCrLf 
    end if 
    strWLAN = strWLAN & " " & IPConfig.NetConnectionID & " " 
    If Not IsNull(IPConf.IPAddress) Then 
    For i = LBound(IPConf.IPAddress) to UBound(IPConf.IPAddress) 
     If Not ((Instr(IPConf.IPAddress(i), ":") > 0) or (Instr(IPConf.IPAddress(i), "none") > 0)) Then 
    if i=0 then 
     strWLAN = strWLAN & " " & IPConf.IPAddress(i) 
    else 
     strWLAN = strWLAN & ", " & IPConf.IPAddress(i) 
    end if 
     End If 
    Next 
    End If 
    next 
Next 



Set objWMIService2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set IPAdapterSet2 = objWMIService2.ExecQuery("Select AdapterType, NetConnectionID, MACAddress from Win32_NetworkAdapter WHERE NetConnectionID LIKE 'Local Area Connection%'") 


For Each IPConfig in IPAdapterSet2 
Set IPConfigSet = objWMIService.ExecQuery("Select IPEnabled, Description, Caption, IPAddress, MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='True' AND MACAddress = '" & IPConfig.MACAddress & "' ") 
    For Each IPConf in IPConfigSet 
    if NOT strLAN="" then 
     strLAN = strLAN & vbCrLf 
    end if 
    strLAN = strLAN & " " & IPConfig.NetConnectionID & " " 
    If Not IsNull(IPConf.IPAddress) Then 
    For i = LBound(IPConf.IPAddress) to UBound(IPConf.IPAddress) 
     If Not ((Instr(IPConf.IPAddress(i), ":") > 0) or (Instr(IPConf.IPAddress(i), "none") > 0)) Then 
    if i=0 then 
     strLAN = strLAN & " " & IPConf.IPAddress(i) 
    else 
     strLAN = strLAN & ", " & IPConf.IPAddress(i) 
    end if 
     End If 
    Next 
    End If 
    next 
Next 


wscript.Echo strWLAN 
wscript.Echo strLAN 
関連する問題