【问题标题】:Powershell, multi line txt file variablesPowershell,多行txt文件变量
【发布时间】:2016-02-10 20:23:08
【问题描述】:

我有一个包含主机名的文本文件:

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 文件

希望我已经解释得够清楚了

【问题讨论】:

    标签: powershell windows-server-2012


    【解决方案1】:

    您可以使用Get-Content cmdlet 读取主机文件,使用foreach-object 对其进行迭代,并使用Test-Path 检查该文件是否存在:

    $hostFile = 'c:\hostfile.txt'
    get-content $hostFile | foreach {
      if (Test-Path "\\$_\c$\test.txt")
      {
        Write-Host "exists"
      }
      else
      {
        Write-Host "doesnt exist"
      }
    }
    

    【讨论】:

    • 这是完美的。谢谢!
    猜你喜欢
    • 2019-10-11
    • 2021-08-22
    • 2017-06-14
    • 2018-02-08
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多