2017-02-27 3 views
0

.Exec을 사용하여 시스템의 IP 구성을 취소하려고합니다. 주어진 명령 출력에서 ​​모든 무선 및 이더넷 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 

는 WMI Windows 배치 파일 또는 WMI 또는 VBScript VBScript를 위와 같이 Exec를 사용을 통해 수행해야합니다. PowerShell은 불행히도 옵션이 아닙니다. 대신 포격의

+1

이의 출력을 구문 분석하는 것보다 쉬울 것입니다 : 현재 그러나 지금 적응할 수있을만큼 쉽게 찾아 볼 수있는 문자열로 정보를 저장한다 'ipconfig' 예 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 
관련 문제