一个循环中的两个变量
Two variables in one loop
我目前正在编写一个脚本,该脚本应该通过主机名和 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 循环并在每个循环中递增 I,但不是只从我的 txt 文件中插入一个,而是全部插入。
for ($i=0; $i -lt 1; $i++ )
我怎样才能实现我的循环从一个文件中获取一行而从另一个文件中获取一行?
假设 $IP
是与 $hostname
长度相同的 IP 数组:
$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 returns 一个字符串数组,因此您必须通过 index 访问当前行。 注意: 您还必须使用 子表达式 使用 $()
来插入字符串。
我目前正在编写一个脚本,该脚本应该通过主机名和 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 循环并在每个循环中递增 I,但不是只从我的 txt 文件中插入一个,而是全部插入。
for ($i=0; $i -lt 1; $i++ )
我怎样才能实现我的循环从一个文件中获取一行而从另一个文件中获取一行?
假设 $IP
是与 $hostname
长度相同的 IP 数组:
$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 returns 一个字符串数组,因此您必须通过 index 访问当前行。 注意: 您还必须使用 子表达式 使用 $()
来插入字符串。