【发布时间】: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