【问题标题】:Powershell get-content where-objectPowershell 获取内容位置对象
【发布时间】:2016-10-19 22:36:53
【问题描述】:

早上,我是 Windows PowerShell 世界的新手,想知道是否有人可以就以下内容提出建议。 我正在为某个文本字符串搜索一个 txt 文件。

Get-Content -Path "filepath" | Where-Object {
  $_ -like '*string*'
} | Out-File c:\output.txt

这对我来说没问题,但我想更聪明一点,只为那一天拉回字符串。 我尝试使用以下

Get-Content -Path "filepath" | Where-Object {
  $_ -like Get-Date -DisplayHint Date -UFormat "%d/%m/%y" + '*string*'
} | Out-File c:\output.txt

文本文件中的行总是以该格式的日期开头,但是我需要检查同一行中的字符串吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您需要将Get-Date 放在括号中:

    Get-Content -Path "filepath" | Where-Object {
      $_ -like (Get-Date -DisplayHint Date -UFormat "%d/%m/%y") + '*string*'
    } | Out-File c:\output.txt
    

    【讨论】:

    • 非常感谢。这很有效,但是,我现在需要做的是负 1 天,因为结果集是昨天的。
    【解决方案2】:

    所以,在尝试了几次之后,我注意到我正在查询的文件具有 MM/dd/yyyy 格式的日期格式,所以这就是我没有找到任何东西的原因。

    另外,我还需要寻找前一天,所以添加了一点。

    稍微调整一下,现在可以使用了

    get-content -path "文本文件的位置" | where-object {$_ -like ("{0:MM/dd/yyyy}" -f (get-date).AddDays(-1)) + 'Sqlserver'} |输出文件名

    感谢大家的帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 2021-10-26
      • 2021-03-13
      • 1970-01-01
      相关资源
      最近更新 更多