【问题标题】:PowerShell: trouble importing tab delimited file as arrayPowerShell:将制表符分隔文件导入为数组时遇到问题
【发布时间】:2018-08-11 05:37:42
【问题描述】:

我正在尝试构建一个 PowerShell 脚本来查找上个月的磁带条形码并将其存储在一个变量中。数据是从备份软件生成的制表符分隔的文本文件中读取的。由于某种原因,PowerShell 能够将文件作为数组导入,但是我无法根据数据进行过滤。

我会解释:

我遇到的第一个问题是,导出文件的前 5 行阻止 PowerShell 将其识别为数组:

#List of Media
#Cell Manager: hpdp.domain.local
#Creation Date: 11/08/2018 9:36:09 AM
# Headers
# Medium ID Label   Location    Status  Protection  Used [MB]   Total [MB]  Last Used   Last Used_t Pool    Type

我能够“解决”这个问题的方法是使用记事本删除前四行并从标题行中删除 #。然后文件如下所示:

Medium ID   Label   Location    Status  Protection  Used [MB]   Total [MB]  Last Used   Last Used_t Pool    Type
id1:id2:id3:id5 [hostname] labelname_31 [Library:     5]    Good    13/08/2018 10:01:41 PM  762699.50   762699.50   14/07/2018 10:00:36 PM  1531828836  Tape Drive Pool LTO-Ultrium

然后我运行以下命令将文件作为数组导入:

$Table = Import-Csv $file # "-Delimiter `t" yields the same result


考虑以下结果:

$Table | FT 生成一个看起来像源文件的表格

$Table | FL 生成一个列表,其中所有对象都按应有的方式显示

$Table | select "Label" 列出标签列及其下的所有数据。对任何列执行此操作会生成应有的列和数据。

$Table | Where-Object {"Last Used" -like '*'} 产生与$Table | FL 相同的输出



现在我们正在了解我想要实现的目标以及不起作用的部分:

我想定位并存储一个“上次使用”日期是上个月的 Label 对象。但是,当我运行以下内容时:

$Table | Where-Object {"Last Used" -like '*/07*'}

我没有得到任何结果。

当我运行这个时:

$Table | Where-Object {"Last Used" -is '2/07/2018 7:36:28 PM'}

我得到了很多:

Cannot convert the "2/07/2018 7:36:28 PM" value of type "System.String" to type "System.Type".
At line:1 char:24
+ $Table | Where-Object {"Last Used" -is '2/07/2018 7:36:28 PM'}
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

Cannot convert the "2/07/2018 7:36:28 PM" value of type "System.String" to type "System.Type".
At line:1 char:24
+ $Table | Where-Object {"Last Used" -is '2/07/2018 7:36:28 PM'}
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException

任何人都可以分享他们对我做错了什么的智慧吗?提前致谢!

【问题讨论】:

    标签: arrays powershell


    【解决方案1】:
     $Table | Where-Object {"Last Used" -like '*/07*'}
    

    正在寻找*/07*,正如单引号所暗示的那样。双引号会更好。反斜杠将转义任何奇怪的字符,例如正斜杠,以防它有意义

    $Table | Where-Object {"Last Used" -like "*\/07*"}
    

    我还会指出“最后使用”的位置。 $_ 表示管道输入的对象

    $Table | Where-Object {$_."Last Used" -like "*\/07*"}
    

    最后,将 -is 替换为 -eq ,这是系统类型错误的来源。 (阅读 this 了解更多关于 -is 与 -eq 的信息)-contains 和 -ccontains 也会有所帮助

    $Table | Where-Object {$_."Last Used" -eq '2/07/2018 7:36:28 PM'}
    

    还有另一个提示。避免使用它们增加大量处理的管道。这与上面相同,但要快很多倍。特别是如果打开一个文件。管道等待上一个命令完成而不是方法来完成它的工作。一种方法符合它。跳过不必要的处理。换句话说,管道将强制读取每一行的位置,该方法只会在兼容的行上激活。如果你想测试它,你可以使用measure-command {command to measure here}

    $Table.where({$_."Last Used" -eq '2/07/2018 7:36:28 PM'})
    

    【讨论】:

    • 谢谢 "$Table | Where-Object {$_."Last Used" -like '07'}" 有效!!
    • 我可以在 MM 中构建上个月的变量,如下所示,但是如何执行“where-object -like 'variable'”?我希望 Where-Object 仅查找上个月的行,但月份是从变量派生的: PS > $previousmonth = (Get-Date).AddMonths(-1).ToString('MM') PS > $previousmonth 07 PS >
    • 这是我的答案!!! $LastdayOfLastmonth = (get-date).adddays(-(get-date -format dd)) $LastdayOfLastmonth_SearchString = "*" + $LastdayOfLastmonth.ToString("dd/MM") + "*" $Table | Where-Object {$_."Last Used" -like "$LastdayOfLastmonth_SearchString"}
    • 很高兴您找到了答案。抱歉我没有早点回复。没有什么比斗争更能教会我们。 ?
    • 还有。我会用。 $LastdayOfLastmonth = (get-date).adddays(-(get-date -format dd)); $LastdayOfLastmonth_SearchString = "*" + $LastdayOfLastmonth.ToString("dd/MM") + "*"; $Table.Where({$_."Last Used" -like "$LastdayOfLastmonth_SearchString"}) .where 平均快 4 倍。
    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多