找回密码
 加入
搜索
查看: 3021|回复: 2

[系统综合] wmi修改固定ip(成另一个固定ip)失败的原因?

[复制链接]
发表于 2015-6-29 14:21:44 | 显示全部楼层 |阅读模式
acn有这个代码,多次测试后发现:
如果原来是dhcp分配的,用这代码修改成固定ip,成功率很高,几乎很少失败。
如果是固定ip,修改成另一个固定ip,则时常失败。
官网有个帖子里提到这个现象,但没有深入解释。
请知道的达人帮忙解释下。
官网的covaks说:
Make sure you are already on DHCP before running this, or some things will fail.

_https://www.autoitscript.com/for ... &comment=456197
$objWMI = ObjGet("winmgmts:\\.\root\cimv2")
$collection = $objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")


For $obj In $collection
    If $obj.DHCPEnabled = -1 Then
        $DHCPStatus = "TRUE"
    Else
        $DHCPStatus = "FALSE"
    EndIf
    
    $IP = $obj.IPAddress
    $Subnet = $obj.IPSubnet
    $Gateway = $obj.DefaultIPGateway
    $DNS = $obj.DNSServerSearchOrder
    

    MsgBox(1, "", "IP Address: " & $IP[0] & @CRLF & _
            "Subnet: " & $Subnet[0] & @CRLF & _
            "Gateway: " & $Gateway[0] & @CRLF & _
            "First DNS: " & $DNS[0] & @CRLF & _
            "DHCP Enabled: " & $DHCPStatus)

Next


MsgBox(1,"","Setting new information")

$IP[0] = "10.0.0.5"
$Subnet[0] = "255.0.0.0"
$Gateway[0] = "10.0.0.1"
Redim $DNS[2]
$DNS[0] = "4.2.2.1"
$DNS[1] = "4.2.2.2"

For $obj in $collection

    $err = $obj.EnableStatic($IP,$Subnet)
    $err2 = $obj.SetGateways($Gateway)
    $err3 = $obj.SetDNSServerSearchOrder($DNS)
    
    If $err = 1 Then
        MsgBox(1,"","Unable to set static IP until reboot")
    ElseIf $err > 1 Then
        MsgBox(1,"","Unable to set IP")
    EndIf
    
    ;etc err2, err3..
    
Next
发表于 2015-7-3 16:27:43 | 显示全部楼层
VB6的代码,自用可行,供参考:
Function ChangeIP(ip As String, NM As String, GW As String, MDNS As String, SDNS As String) As String
    '返回值说明:返回一个设置的中文说明.
    Dim strComputer, objWMIService, colNetAdapters, strIPAddress, strSubnetMask
    Dim strGateway, strGatewaymetric, strDNS, objNetAdapter, errEnable, errGateways, errDNS
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery("Select   *   from   Win32_NetworkAdapterConfiguration   where   IPEnabled=TRUE")
    strIPAddress = Array(ip)       'ip地址
    strSubnetMask = Array(NM)       '子网掩码
    strGateway = Array(GW)       '网关
'    MsgBox TypeName(strGateway)
    strDNS = Array(MDNS, SDNS)         '主DNS各备DNS
    
    strGatewaymetric = Array(1)
        
    For Each objNetAdapter In colNetAdapters
        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
        errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
        errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS) 'DNS为空的话,改为errDNS = objNetAdapter.SetDNSServerSearchOrder()
        If errEnable = 0 And errGateways = 0 And errDNS = 0 Then
            ChangeIP = "设置成功"
        Else
            If errEnable = 0 Then
                ChangeIP = "IP地址和子网掩码设置成功,   "
            Else
                ChangeIP = "IP地址或子网掩码设置失败,   "
            End If
            If errGateways = 0 Then
                ChangeIP = ChangeIP & "默认网关设置成功,   "
            Else
                ChangeIP = ChangeIP & "默认网关设置失败,   "
            End If
            If errDNS = 0 Then
                ChangeIP = ChangeIP & "DNS设置成功"
            Else
                ChangeIP = ChangeIP & "DNS设置失败"
            End If
        End If
    Next
    
  End Function
 楼主| 发表于 2015-7-4 09:25:37 | 显示全部楼层
在研究vb,刚开始。
看代码都是调用wmi
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-16 06:50 , Processed in 0.072397 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表