我目前正在研究一个脚本,该脚本应按主机名和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是一个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返回一个字符串数组,因此您必须按索引访问当前行。注意:您还必须使用子表达式来$()
对字符串进行插值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句