Windows 脚本从 DHCP 更改为静态 IP 寻址

Windows 脚本从 DHCP 更改为静态 IP 寻址

我需要一个 (WSH) 脚本,客户端可以在自己的机器上执行该脚本,并且麻烦最少,不需要我拥有任何特殊权限。该脚本应获取以太网适配器的现有 IP 地址、子网掩码和默认网关,并将它们设置为静态参数。我还需要一个脚本来撤消此操作。我已经弄清楚了如何使用 netsh 命令在命令行上手动设置参数,即

netsh.exe interface ip set address {Interface} static {IP} {SUB} {DEF} 1

但是我怀疑这可以通过 WSH 脚本自动完成。客户端是 XP/Vista/Windows 7。实现此目的的最佳方法是什么?

答案1

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

n = 1

For Each objAdapter in colAdapters 
If (objAdapter.DHCPEnabled) Then
    If Not IsNull(objAdapter.IPAddress) Then
   For i = 0 To UBound(objAdapter.IPAddress)
    IPAddress = objAdapter.IPAddress(i)
   Next
End If

If Not IsNull(objAdapter.IPSubnet) Then
   For i = 0 To UBound(objAdapter.IPSubnet)
    SubNet = objAdapter.IPSubnet(i)
   Next
End If

If Not IsNull(objAdapter.DefaultIPGateway) Then
   For i = 0 To UBound(objAdapter.DefaultIPGateway)
      GateWay = objAdapter.DefaultIPGateway(i)
   Next
End If


If Not IsNull(objAdapter.DNSServerSearchOrder) Then
   For i = 0 To UBound(objAdapter.DNSServerSearchOrder)

   Next
End If


If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
   For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
      WScript.Echo "    DNS suffix search list: " & _
          objAdapter.DNSDomainSuffixSearchOrder(i)
   Next
End If



objAdapter.DHCPEnabled = false
  If Not IsNull(objAdapter.DHCPLeaseObtained) Then
     utcLeaseObtained = objAdapter.DHCPLeaseObtained
  Else
     strLeaseObtained = ""
  End If


  If Not IsNull(objAdapter.DHCPLeaseExpires) Then
     utcLeaseExpires = objAdapter.DHCPLeaseExpires

  Else
     strLeaseExpires = ""
  End If
  WScript.Echo "    DHCP lease expires:  " & strLeaseExpires



n = n + 1


'netsh interface ip set address "Local Area Connection" static objAdapter.IPAddress(i) objAdapter.IPSubnet(i) objAdapter.DefaultIPGateway(i) 1

Dim WshShell, oExec, A
Set WshShell = CreateObject("WScript.Shell")

WScript.Echo "  IP address:             " & IPAddress
WScript.Echo "  Subnet:                 " & Subnet
WScript.Echo "  Default gateway:        " & Gateway

A = "netsh interface ip set address " & Chr(34) & "Local Area Connection" & Chr(34) & " static " & IPAddress & " " & Subnet & " " & Gateway & " 1"
wscript.echo A
Set oExec = WshShell.Exec(A)


  WScript.Echo "before runnig this script on the network take out all wscripts enjoy LCpl Worsnop"
End If
next

相关内容