Loops 一个循环中的两个变量

Loops 一个循环中的两个变量,loops,powershell,foreach,powershell-5.0,Loops,Powershell,Foreach,Powershell 5.0,我目前正在编写一个脚本,它应该根据主机名和ip查询机器 $IP = (get-content "C:\Users\me\Desktop\PowerShell\ips.txt") $hostname = (get-content "C:\Users\me\Desktop\PowerShell\hostnames.txt") 现在我需要将$IP和$hostname插入一个字符串中 write-host "This is my $IP, this is my $hostname" 到目前为止,我尝

我目前正在编写一个脚本,它应该根据主机名和ip查询机器

$IP = (get-content "C:\Users\me\Desktop\PowerShell\ips.txt")
$hostname = (get-content "C:\Users\me\Desktop\PowerShell\hostnames.txt")
现在我需要将
$IP
$hostname
插入一个字符串中

write-host "This is my $IP, this is my $hostname"
到目前为止,我尝试使用for循环并在每个循环中递增,但不是从txt文件中只插入一个,而是全部插入

for ($i=0; $i -lt 1; $i++ )

如何实现我的循环从一个文件中取一行,从另一个文件中取一行?

假设
$IP
是一个IP数组,其长度与
$hostname
相同:

$IP = (get-content "C:\Users\me\Desktop\PowerShell\ips.txt")
$hostname = (get-content "C:\Users\me\Desktop\PowerShell\hostnames.txt")

for ($i=0; $i -lt $IP.Length; $i++ )
{
    write-host "This is my $($IP[$i]), this is my $($hostname[$i])"
}

Get Content
cmdlet返回字符串数组,因此您必须按索引访问当前行注意:还必须使用子表达式使用
$()
插入字符串

这个问题做的更多,但你所寻找的设置是相同的。