问题描述
下面存储的数据集是文本文件,第一个是服务器名称,第二个是日期,第三个是补丁历史记录。
WSUSCL02-2012
Monday,August 10,2020 5:03:08 PM
X Status KB Size Title
- ------ -- ---- -----
2 Accepted KB3172729 10 MB Security Update for Windows Server 2012 R2 (KB...
2 Accepted KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
3 Downloaded KB3172729 10 MB Security Update for Windows Server 2012 R2 (KB...
3 Downloaded KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
4 Installed KB3172729 10 MB Security Update for Windows Server 2012 R2 (KB...
4 Installed KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
WSUSCL01-2012
Monday,2020 5:03:01 PM
X Status KB Size Title
- ------ -- ---- -----
2 Accepted KB2962409 50 MB Update for Windows Server 2012 R2 (KB2962409)
2 Accepted KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
3 Downloaded KB2962409 50 MB Update for Windows Server 2012 R2 (KB2962409)
3 Downloaded KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
4 Installed KB2962409 50 MB Update for Windows Server 2012 R2 (KB2962409)
4 Installed KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
上面是存储在文本文件中的数据集,要求是分析数据并选择servername,date,patch并将该数据放入具有服务器名称,日期和补丁详细信息的自定义Power Shell对象中。请帮我做到这一点
解决方法
使用switch -Regex -File
遍历文本文件中的每一行都可以解决问题。
下面的代码解析了所有污秽,但是您可以注释掉结果中不希望出现的任何属性
$result = switch -Regex -File 'D:\Test\patches.txt' {
'^[-\w]+$' { $server = $_ }
'[AP]M$' { $date = [datetime]::ParseExact($_,'F',[cultureinfo]'en-US') }
'^(\d+)\s+(\w+)\s+(KB\d+)\s+(\d+\s[KM]B)\s+(.+)' {
# create and output an object
[PsCustomObject]@{
Server = $server
Date = $date
X = $matches[1]
Status = $matches[2]
KB = $matches[3]
Size = $matches[4]
Title = $matches[5]
}
}
}
# output on screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path 'D:\Test\patchresults.csv' -NoTypeInformation
使用示例文件输出
Server Date X Status KB Size Title ------ ---- - ------ -- ---- ----- WSUSCL02-2012 10-8-2020 17:03:08 2 Accepted KB3172729 10 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL02-2012 10-8-2020 17:03:08 2 Accepted KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL02-2012 10-8-2020 17:03:08 3 Downloaded KB3172729 10 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL02-2012 10-8-2020 17:03:08 3 Downloaded KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL02-2012 10-8-2020 17:03:08 4 Installed KB3172729 10 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL02-2012 10-8-2020 17:03:08 4 Installed KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL01-2012 10-8-2020 17:03:01 2 Accepted KB2962409 50 MB Update for Windows Server 2012 R2 (KB2962409) WSUSCL01-2012 10-8-2020 17:03:01 2 Accepted KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL01-2012 10-8-2020 17:03:01 3 Downloaded KB2962409 50 MB Update for Windows Server 2012 R2 (KB2962409) WSUSCL01-2012 10-8-2020 17:03:01 3 Downloaded KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB... WSUSCL01-2012 10-8-2020 17:03:01 4 Installed KB2962409 50 MB Update for Windows Server 2012 R2 (KB2962409) WSUSCL01-2012 10-8-2020 17:03:01 4 Installed KB3175024 12 MB Security Update for Windows Server 2012 R2 (KB...
P.S。我的系统是荷兰语,因此显示的默认日期格式为'dd-M-yyyy HH:mm:ss'