代码之家  ›  专栏  ›  技术社区  ›  Andrew J. Brehm

win32_networkadapterconfiguration.enableStatic()能否用于设置多个IP地址?

  •  1
  • Andrew J. Brehm  · 技术社区  · 15 年前

    我在使用WMI的VisualBasic程序中遇到此问题,但可以在PowerShell中确认。显然,尽管使用了两个参数IP地址和作为数组的子网掩码,但enableStatic()方法只能用于设置一个IP地址。

    即。

    $a=get-wmiobject win32_networkadapterconfiguration -computername myserver
    

    这使我得到“myserver”上所有网络适配器的数组。在选择了一个特定的签名之后(本例中为$A=$A[14]),我可以运行具有此签名的$A.EnableStatic()。

    System.Management.ManagementBaseObject EnableStatic(System.String[] IPAddress, System.String[] SubnetMask)
    

    我认为这意味着我可以设置如下几个IP地址:

    $ips="192.168.1.42","192.168.1.43"
    $a.EnableStatic($ips,"255.255.255.0")
    

    但是这个呼叫失败了。但是,此调用有效:

    $a.EnableStatic($ips[0],"255.255.255.0")
    

    在我看来,enableStatic()确实采用了两个字符串而不是两个字符串数组作为参数。

    在VisualBasic中,它更复杂,必须传递数组,但该方法似乎只考虑每个数组的第一个元素。

    我又困惑了吗?还是这里有什么逻辑?

    3 回复  |  直到 13 年前
        1
  •  2
  •   James    14 年前

    我通过使用一组IP和一组匹配的子网掩码使它工作。下面是一个A类专用子网的示例。

    $range = 2..254
    $DNS = "8.8.8.8","8.8.4.4"
    $gateway = "10.0.0.1"
    $registerDns = $true
    $IPs = @()
    $mask = @()
    foreach ($end in $range) {
        $IPs += "10.0.0.$end"
        $mask += "255.0.0.0"
    }
    
    $netcon = "Local Area Connection"
    $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $NetCon}).InterfaceIndex
    $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
    
    $NetInterface.EnableStatic($ips, $mask)
    $NetInterface.SetGateways($gateway)
    $NetInterface.SetDNSServerSearchOrder($dns)
    $NetInterface.SetDynamicDNSRegistration($registerDns)
    
        2
  •  1
  •   x0n    15 年前

    尝试使用演员表:

    $a.EnableStatic([string[]]$ips,"255.255.255.0") 
    

    $ips实际上不是字符串数组;它是对象数组。有时PowerShell的活页夹会与数组有点混淆,因为其中有消除歧义的微妙之处,比第一次遇到未经训练的眼睛要复杂得多。

    -奥辛

        3
  •  0
  •   ndnshift    13 年前

    要使调用成功,需要有一个与netmask匹配的字符串数组….

    所以EX:

    $ip = "10.10.10.10"
    $ip += "10.10.10.11"
    $ip += "10.10.10.12"
    $mask = "255.255.255.0"
    $mask += "255.255.255.0
    $mask += "255.255.255.0
    
    $nic.enablestatic($ip,$mask)
    

    这就是为什么第二篇文章中的例子…