Powershell 脚本自定义对象
Powershell scripting custom object
下面的数据集存储的是文本文件,第一个是服务器名称,第二个是日期,第三个是补丁历史。
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, August 10, 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'
下面的数据集存储的是文本文件,第一个是服务器名称,第二个是日期,第三个是补丁历史。
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, August 10, 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'