Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何停止在ForEach中多次遍历整个列表?_Powershell_Powercli - Fatal编程技术网

Powershell 如何停止在ForEach中多次遍历整个列表?

Powershell 如何停止在ForEach中多次遍历整个列表?,powershell,powercli,Powershell,Powercli,如何将列表中的1个IP地址应用于另一个列表中的1个服务器? 然后移动到下一个IP并将其应用到下一个服务器 servers.txt看起来像: server1 server2 server3 10.1.140.80 10.1.140.81 10.1.140.83 ip.txt看起来像: server1 server2 server3 10.1.140.80 10.1.140.81 10.1.140.83 我只想看一下名单然后申请 10.1.140.80 to server1 10.1.140

如何将列表中的1个IP地址应用于另一个列表中的1个服务器? 然后移动到下一个IP并将其应用到下一个服务器

servers.txt看起来像:

server1
server2
server3
10.1.140.80
10.1.140.81
10.1.140.83
ip.txt看起来像:

server1
server2
server3
10.1.140.80
10.1.140.81
10.1.140.83
我只想看一下名单然后申请

10.1.140.80 to server1
10.1.140.81 to server2
10.1.140.83 to server3
相反,我的脚本将所有3个IP地址应用于每个服务器。 我不想一遍又一遍地浏览所有的IP地址

如何正确地遍历列表并更正此问题

$computers = "$PSScriptRoot\servers.txt"
$iplist        = gc "$PSScriptRoot\ip.txt"

function changeip {
    get-content $computers | % {
        ForEach($ip in $iplist) {

            # Set IP address
            $remotecmd1 = 'New-NetIPAddress -InterfaceIndex 2 -IPAddress $ip -PrefixLength 24 -DefaultGateway 10.1.140.1'

            # Set DNS Servers  -  Make sure you specify the server's network adapter name at -InterfaceAlias
            $remotecmd2 = 'Set-DnsClientServerAddress -InterfaceAlias "EthernetName" -ServerAddresses 10.1.140.5, 10.1.140.6'

            Invoke-VMScript -VM $_ -ScriptText $remotecmd1 -GuestUser Administrator -GuestPassword PASSWORD -ScriptType PowerShell
            Invoke-VMScript -VM $_ -ScriptText $remotecmd2 -GuestUser Administrator -GuestPassword PASSWORD -ScriptType PowerShell

        }
    }
}

changeip

使用Get-Content cmdlt将两个文件内容放置到一个数组中,然后按数组位置提取各个值。您可能需要一些逻辑来检查数组大小是否匹配,如果不匹配,则需要自定义处理。在上面的示例中,基本上是将for-each循环放在另一个foreach循环中,该循环给出了您看到的行为

$computers = GC "C:\server.txt"
$iplist   = GC "C:\ip.txt"

for ($i = 0; $i -lt $iplist.Count ; $i++) {
    Write-host  ("{0}  -  {1}" -f $computers[$i],$iplist[$i])
}
或者,如果您已经习惯于使用foreach逻辑来实现一个列表,而不是使用for循环进行基本的迭代,那么您可以在foreach循环中添加一个计数器。然后可以查找已解析的iplist数组的数组索引。但它基本上也在做同样的事情

$computers = "C:\server.txt"
$iplist   = GC "C:\ip.txt"

get-content $computers | % {$counter = 0} {
Write-host  ("{0}  -  {1}" -f $_,$iplist[$counter])
$counter++
}
为了清楚起见,请注意这一行:

“获取内容$computers |%”

%实际上是ForEach对象的别名,这就是为什么要在看到的ForEach输出中获取ForEach

$computers = GC "C:\server.txt"
$iplist   = GC "C:\ip.txt"

for ($i = 0; $i -lt $iplist.Count ; $i++) {
    Write-host  ("{0}  -  {1}" -f $computers[$i],$iplist[$i])
}