Powershell,多行txt文件变量
Powershell, multi line txt file variables
我有一个文本文件,其中主机名在各行中:
IT4524
IT5135
IT5688
... etc
我可以导入这些并列出每一行:
$hostname[1]
IT4524
我需要通过命令将每一(和所有)单独的行通过管道传输。我怎样才能做到这一点而不必写:
If "\$hostname[1]\c$\test.txt{
# // File exists
}Else{
# // File does not exist
}
If "\$hostname[2]\c$\test.txt{
# // File exists
}Else{
# // File does not exist
}
...等
最终结果是检查每个主机名 C: 是否有另一个 PS
输出的 txt 文件
希望我已经解释得够清楚了
您可以使用 Get-Content cmdlet, iterate over it using foreach-object and check whether the file exists using Test-Path:
读取主机文件
$hostFile = 'c:\hostfile.txt'
get-content $hostFile | foreach {
if (Test-Path "\$_\c$\test.txt")
{
Write-Host "exists"
}
else
{
Write-Host "doesnt exist"
}
}
我有一个文本文件,其中主机名在各行中:
IT4524
IT5135
IT5688
... etc
我可以导入这些并列出每一行:
$hostname[1]
IT4524
我需要通过命令将每一(和所有)单独的行通过管道传输。我怎样才能做到这一点而不必写:
If "\$hostname[1]\c$\test.txt{
# // File exists
}Else{
# // File does not exist
}
If "\$hostname[2]\c$\test.txt{
# // File exists
}Else{
# // File does not exist
}
...等
最终结果是检查每个主机名 C: 是否有另一个 PS
输出的 txt 文件希望我已经解释得够清楚了
您可以使用 Get-Content cmdlet, iterate over it using foreach-object and check whether the file exists using Test-Path:
读取主机文件$hostFile = 'c:\hostfile.txt'
get-content $hostFile | foreach {
if (Test-Path "\$_\c$\test.txt")
{
Write-Host "exists"
}
else
{
Write-Host "doesnt exist"
}
}