Windowsの接続名("ローカル エリア接続"とか)はいい加減!
サーバにはNIC(Network Interface Card)が複数付いているのが当たり前である。WindowsのNICには、通常 "ローカル エリア接続"、"ローカル エリア接続 2"…という具合に自動的に連番の接続名(NetConnectionID)が付けられるが、この名前付けはどうもランダムに行われているようで、まったく同じ構成のサーバでも、違う名前が付いたりすることがよくある。
一方、NICのデバイス名(デバイス マネージャーやコントロールパネルで見えるやつ)はブレることがまずない。
例えば最近のHPのサーバで、NICがNC382iの場合、デバイス名は1番ポートから順に
- HP NC382i DP Multifunction Gigabit Server Adapter
- HP NC382i DP Multifunction Gigabit Server Adapter #2
- HP NC382i DP Multifunction Gigabit Server Adapter #3
- …
どのNICがどの接続名になっているのかを知るにはコントロールパネルを見ればいい。デバイス名と接続名が両方表示される。
![]() |
Figure.1 コンパネで接続名とデバイス名を見る |
しかし、同じ構成のサーバを何十台、何百台と構築する場合、いちいち目視で確認などしていられない。
(実際には本番環境のサーバはチーミングを組む事が多いが、ここでは横においておく)。
VBScriptを使って、デバイス名をキーに接続名を調べてみる
以下は、WMIを使ってデバイス名から接続名を調べるVBScriptである。
引数にデバイス名を与えると、対応する接続名が表示される。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ' GetNetConnectionIDfromAdapterName.vbs ' Get Network Adapter Name and NetConnectionID ' 2011.12.17 dsp74118 option explicit Function GetNetConnectionID(adapterName) dim computer dim wmiService dim netAdapter dim netAdapters dim targetConnectionID computer = "." GetNetConnectionID = "Not Found" set wmiService = GetObject( "winmgmts:\\" & computer & _ "\root\CIMV2" ) set netAdapters = wmiService.ExecQuery( _ "SELECT * FROM Win32_NetworkAdapter" ) For Each netAdapter in netAdapters if netAdapter.Name = adapterName then GetNetConnectionID = _ netAdapter.NetConnectionID exit for end if Next End Function ' main dim adapterName dim connectionID adapterName = WScript.Arguments.Item(0) connectionID = GetNetConnectionID(adapterName) WScript.Echo adapterName & " = " & connectionID |
![]() |
Figure.2 実行例 |
これで任意のNICの接続名を取得することに成功したので、後は
1 2 3 4 | Dim wshShell set wshShell = WScript.CreateObject( "Wscript.Shell" ) wshShell.run( "netsh -c interface set interface name=" "" & _ connectionID & "" " newname=" "ServiceLan" "" ) |
1 2 3 | wshShell.run( "netsh interface ip set address " "" & _ connectionID & "" " static " & _ "192.168.0.108 255.255.255.0 192.168.0.1 1" ) |
実はPowerShellだと…
PowerShellにはGet-WmiObjectというコマンドがあって、同じ事がもう少し簡単に(かつカッコよく)できたりする。
PowerShellでIPアドレスを変更してみる
1 2 3 4 5 6 | $nicconf = Get-WmiObject win32_networkadapterconfiguration | Where-Object {$_.Description -eq "デバイス名" } $nicconf .EnableStatic( "192.168.0.108" , "255.255.255.0" ) $nicconf .SetGateways( "192.168.0.1" ) $DNSServers = "192.168.0.1" , "8.8.8.8" $nicconf .SetDNSServerSearchOrder( $DNSServers ) |
PowerShellで接続名を変更してみる
1 2 3 4 | $nic = Get-WmiObject Win32_NetworkAdapter | Where-Object {$_.Name -eq "デバイス名" } $nic .NetConnectionID = "ServiceLan" $nic .Put() |
0 コメント:
コメントを投稿