デフォルトゲートウェイをVBScriptで表示させる方法

パソコンのIPアドレスに応じて動作を変化させるスクリプトを作ろうかと思ったのですが、ザックリとしたセグメント毎の変更で問題無かったので、Win32_NetworkAdapterConfigurationを利用して、デフォルトゲートウェイ部分を表示させるスクリプトを作ってみました。

'デフォルトゲートウェイをVBScriptで取得する

Wscript.Echo "このパソコンのデフォルトゲートウェイは " & DefaultIPGateway() & " です。"

Function DefaultIPGateway()
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
	Set colItems = objWMIService.ExecQuery( _
	    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True",,48)
	For Each objItem in colItems
		'デフォルトゲートウェイがNull以外だった場合
	    If isNull(objItem.DefaultIPGateway) = False Then
	        DefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
	        'For文を抜ける
	        Exit For
	    End If
	Next
	Set objWMIService = Nothing
End Function

ちなみにWin32_NetworkAdapterConfigurationの値は以下の方法で取得出来ます。

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapterConfiguration",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_NetworkAdapterConfiguration instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "ArpAlwaysSourceRoute: " & objItem.ArpAlwaysSourceRoute
    Wscript.Echo "ArpUseEtherSNAP: " & objItem.ArpUseEtherSNAP
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "DatabasePath: " & objItem.DatabasePath
    Wscript.Echo "DeadGWDetectEnabled: " & objItem.DeadGWDetectEnabled
    If isNull(objItem.DefaultIPGateway) Then
        Wscript.Echo "DefaultIPGateway: "
    Else
        Wscript.Echo "DefaultIPGateway: " & Join(objItem.DefaultIPGateway, ",")
    End If
    Wscript.Echo "DefaultTOS: " & objItem.DefaultTOS
    Wscript.Echo "DefaultTTL: " & objItem.DefaultTTL
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "DHCPEnabled: " & objItem.DHCPEnabled
    Wscript.Echo "DHCPLeaseExpires: " & objItem.DHCPLeaseExpires
    Wscript.Echo "DHCPLeaseObtained: " & objItem.DHCPLeaseObtained
    Wscript.Echo "DHCPServer: " & objItem.DHCPServer
    Wscript.Echo "DNSDomain: " & objItem.DNSDomain
    If isNull(objItem.DNSDomainSuffixSearchOrder) Then
        Wscript.Echo "DNSDomainSuffixSearchOrder: "
    Else
        Wscript.Echo "DNSDomainSuffixSearchOrder: " & Join(objItem.DNSDomainSuffixSearchOrder, ",")
    End If
    Wscript.Echo "DNSEnabledForWINSResolution: " & objItem.DNSEnabledForWINSResolution
    Wscript.Echo "DNSHostName: " & objItem.DNSHostName
    If isNull(objItem.DNSServerSearchOrder) Then
        Wscript.Echo "DNSServerSearchOrder: "
    Else
        Wscript.Echo "DNSServerSearchOrder: " & Join(objItem.DNSServerSearchOrder, ",")
    End If
    Wscript.Echo "DomainDNSRegistrationEnabled: " & objItem.DomainDNSRegistrationEnabled
    Wscript.Echo "ForwardBufferMemory: " & objItem.ForwardBufferMemory
    Wscript.Echo "FullDNSRegistrationEnabled: " & objItem.FullDNSRegistrationEnabled
    If isNull(objItem.GatewayCostMetric) Then
        Wscript.Echo "GatewayCostMetric: "
    Else
        Wscript.Echo "GatewayCostMetric: " & Join(objItem.GatewayCostMetric, ",")
    End If
    Wscript.Echo "IGMPLevel: " & objItem.IGMPLevel
    Wscript.Echo "Index: " & objItem.Index
    If isNull(objItem.IPAddress) Then
        Wscript.Echo "IPAddress: "
    Else
        Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
    End If
    Wscript.Echo "IPConnectionMetric: " & objItem.IPConnectionMetric
    Wscript.Echo "IPEnabled: " & objItem.IPEnabled
    Wscript.Echo "IPFilterSecurityEnabled: " & objItem.IPFilterSecurityEnabled
    Wscript.Echo "IPPortSecurityEnabled: " & objItem.IPPortSecurityEnabled
    If isNull(objItem.IPSecPermitIPProtocols) Then
        Wscript.Echo "IPSecPermitIPProtocols: "
    Else
        Wscript.Echo "IPSecPermitIPProtocols: " & Join(objItem.IPSecPermitIPProtocols, ",")
    End If
    If isNull(objItem.IPSecPermitTCPPorts) Then
        Wscript.Echo "IPSecPermitTCPPorts: "
    Else
        Wscript.Echo "IPSecPermitTCPPorts: " & Join(objItem.IPSecPermitTCPPorts, ",")
    End If
    If isNull(objItem.IPSecPermitUDPPorts) Then
        Wscript.Echo "IPSecPermitUDPPorts: "
    Else
        Wscript.Echo "IPSecPermitUDPPorts: " & Join(objItem.IPSecPermitUDPPorts, ",")
    End If
    If isNull(objItem.IPSubnet) Then
        Wscript.Echo "IPSubnet: "
    Else
        Wscript.Echo "IPSubnet: " & Join(objItem.IPSubnet, ",")
    End If
    Wscript.Echo "IPUseZeroBroadcast: " & objItem.IPUseZeroBroadcast
    Wscript.Echo "IPXAddress: " & objItem.IPXAddress
    Wscript.Echo "IPXEnabled: " & objItem.IPXEnabled
    If isNull(objItem.IPXFrameType) Then
        Wscript.Echo "IPXFrameType: "
    Else
        Wscript.Echo "IPXFrameType: " & Join(objItem.IPXFrameType, ",")
    End If
    Wscript.Echo "IPXMediaType: " & objItem.IPXMediaType
    If isNull(objItem.IPXNetworkNumber) Then
        Wscript.Echo "IPXNetworkNumber: "
    Else
        Wscript.Echo "IPXNetworkNumber: " & Join(objItem.IPXNetworkNumber, ",")
    End If
    Wscript.Echo "IPXVirtualNetNumber: " & objItem.IPXVirtualNetNumber
    Wscript.Echo "KeepAliveInterval: " & objItem.KeepAliveInterval
    Wscript.Echo "KeepAliveTime: " & objItem.KeepAliveTime
    Wscript.Echo "MACAddress: " & objItem.MACAddress
    Wscript.Echo "MTU: " & objItem.MTU
    Wscript.Echo "NumForwardPackets: " & objItem.NumForwardPackets
    Wscript.Echo "PMTUBHDetectEnabled: " & objItem.PMTUBHDetectEnabled
    Wscript.Echo "PMTUDiscoveryEnabled: " & objItem.PMTUDiscoveryEnabled
    Wscript.Echo "ServiceName: " & objItem.ServiceName
    Wscript.Echo "SettingID: " & objItem.SettingID
    Wscript.Echo "TcpipNetbiosOptions: " & objItem.TcpipNetbiosOptions
    Wscript.Echo "TcpMaxConnectRetransmissions: " & objItem.TcpMaxConnectRetransmissions
    Wscript.Echo "TcpMaxDataRetransmissions: " & objItem.TcpMaxDataRetransmissions
    Wscript.Echo "TcpNumConnections: " & objItem.TcpNumConnections
    Wscript.Echo "TcpUseRFC1122UrgentPointer: " & objItem.TcpUseRFC1122UrgentPointer
    Wscript.Echo "TcpWindowSize: " & objItem.TcpWindowSize
    Wscript.Echo "WINSEnableLMHostsLookup: " & objItem.WINSEnableLMHostsLookup
    Wscript.Echo "WINSHostLookupFile: " & objItem.WINSHostLookupFile
    Wscript.Echo "WINSPrimaryServer: " & objItem.WINSPrimaryServer
    Wscript.Echo "WINSScopeID: " & objItem.WINSScopeID
    Wscript.Echo "WINSSecondaryServer: " & objItem.WINSSecondaryServer
Next

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です