【问题标题】:Use 2 arrays in one loop在一个循环中使用 2 个数组
【发布时间】:2015-07-06 18:41:06
【问题描述】:

我想在一个循环中使用 2 个数组,但每次都找不到怎么做?

$hosts = "1.1.1.1,2.2.2.2,3.3.3.3"
$vmotionIPs = "1.2.3.4,5.6.7.8,7.8.9.0"
foreach ($host in $hosts) ($vmotionIP in $vmotionIPs)
  New-VMHostNetworkAdapter -VMHost $host-VirtualSwitch myvSwitch `
    -PortGroup VMotion -IP $vmotionIP -SubnetMask 255.255.255.0 `
    -VMotionEnabled $true

我知道上面的语法是错误的,但我只是希望它在这里传达了我的目标。

【问题讨论】:

    标签: powershell powercli


    【解决方案1】:

    最直接的方法是使用哈希表:

    $hosts = @{
        "1.1.1.1" = "1.2.3.4" # Here 1.1.1.1 is the name and 1.2.3.4 is the value
        "2.2.2.2" = "5.6.7.8"
        "3.3.3.3" = "7.8.9.0"
    }
    
    # Now we can iterate the hashtable using GetEnumerator() method.
    foreach ($hostaddr in $hosts.GetEnumerator()) { # $host is a reserved name
        New-VMHostNetworkAdapter -VMHost $hostaddr.Name -VirtualSwitch myvSwitch `
            -PortGroup VMotion -IP $$hostaddr.Value -SubnetMask 255.255.255.0 `
            -VMotionEnabled $true
    }
    

    【讨论】:

      【解决方案2】:

      首先,您的数组不是数组。它们只是字符串。要成为数组,您需要将它们指定为:

      $hosts = "1.1.1.1","2.2.2.2","3.3.3.3";
      $vmotionIPs = "1.2.3.4","5.6.7.8","7.8.9.0";
      

      其次,$host 是保留变量。你应该避免使用它。

      第三,我假设您希望第一个主机使用第一个 vmotionIP,第二个主机使用第二个 vmotionIP,等等。

      因此,执行此操作的标准方法是:

      $hosts = "1.1.1.1","2.2.2.2","3.3.3.3";
      $vmotionIPs = "1.2.3.4","5.6.7.8","7.8.9.0";
      
      for ($i = 0; $i -lt $hosts.Count; $i++) {
          New-VMHostNetworkAdapter -VMHost $hosts[$i] `
              -VirtualSwitch myvSwitch `
              -PortGroup VMotion `
              -IP $vmotionIPs[$i] `
              -SubnetMask 255.255.255.0 `
              -VMotionEnabled $true;
      }
      

      或者您可以使用@AlexanderObersht 描述的哈希表方法。但是,此方法对您的代码的改变最少。

      【讨论】:

        【解决方案3】:

        感谢您的信息。你建议的对我有用的其他脚本,但我最终使用以下方法实现了这一点。 首先我生成了一系列这样的ip地址

        $fixed = $host1.Split('.')[0..2]
        $last = [int]($host.Split('.')[3])
        $max = Read-Host "Maximum number of hosts that you want to configure?"
        $max_hosts = $max - 1
        $hosts = 
        $last..($last + $max_hosts) | %{
        [string]::Join('.',$fixed) + "." + $_
        }
        

        然后我做了

        $vMotion1_ip1 = Read-Host "the 1st vmotion ip of the 1st host?"
        $fixed = $vMotion1_ip1.Split('.')[0..2]
        $last = [int]($vMotion1_ip1.Split('.')[3])
        $max_hosts = $max - 1
        $vMotions = 
        $last..($last + $max_hosts) | %{
        [string]::Join('.',$fixed) + "." + $_
        }
        $first = [string]::Join('.',$fixed) + "." + $_
        
        foreach ($vmhost in $vMotions) {write-host "$vmhost has the following      network ("$first$(($last++))", "255.255.255.0")"}
        

        不完全是这样,而是沿着这条路走。

        【讨论】:

          【解决方案4】:

          谢谢大家的回答。我最终改用 do while 。这允许我们同时循环任意数量的数组,或者在一个循环中包含多个数组。

          $hosts  = @("1.1.1.1","2.2.2.2","3.3.3.3")
          $vmotionIPs  = @("1.2.3.4","5.6.7.8","7.8.9.0")
          [int]$n = 0
          do
          {
          $vmhost = $hosts[$n]
          $vmotionIP = $vmotionIPs[$n]
          New-VMHostNetworkAdapter -VMHost $vmhost-VirtualSwitch myvSwitch -PortGroup VMotion -IP $vmotionIP -SubnetMask 255.255.255.0 -VMotionEnabled $true
          $n++
          } while ($n -lt $hosts.count)
          

          【讨论】:

            猜你喜欢
            • 2021-11-25
            • 2019-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多