Powershell 脚本自定义对象 [英] Powershell scripting custom object

查看:47
本文介绍了Powershell 脚本自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的数据集存储的是文本文件,第一个是服务器名称,第二个是日期,第三个是补丁历史.

WSUSCL02-20122020 年 8 月 10 日星期一下午 5:03:08X 状态 KB 大小标题- - - - - - - - -2 已接受 KB3172729 10 MB Windows Server 2012 R2 安全更新 (KB...2 已接受 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...3 已下载适用于 Windows Server 2012 R2 的 KB3172729 10 MB 安全更新 (KB...3 已下载适用于 Windows Server 2012 R2 的 KB3175024 12 MB 安全更新 (KB...4 已安装 KB3172729 10 MB Windows Server 2012 R2 安全更新 (KB...4 已安装 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL01-20122020 年 8 月 10 日星期一下午 5:03:01X 状态 KB 大小标题- - - - - - - - -2 已接受 KB2962409 50 MB Windows Server 2012 R2 更新 (KB2962409)2 已接受 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...3 已下载适用于 Windows Server 2012 R2 的 KB2962409 50 MB 更新 (KB2962409)3 已下载适用于 Windows Server 2012 R2 的 KB3175024 12 MB 安全更新 (KB...4 已安装 KB2962409 50 MB 更新,适用于 Windows Server 2012 R2 (KB2962409)4 已安装 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...

以上是存储在文本文件中的数据集,要求是解析数据并选择 servername 、 date 、 patch 并将该数据放入自定义 power shell 对象中,名称为服务器名称、日期、补丁详细信息.请帮我做这个

解决方案

使用 switch -Regex -File 循环遍历文本文件中的每一行应该可以解决问题.

下面的代码解析出所有 filds,但您可以在结果中注释掉您不希望的任何属性

$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+(.+)' {# 创建并输出一个对象[PsCustomObject]@{服务器 = $ 服务器日期 = $dateX = $matches[1]状态 = $matches[2]KB = $matches[3]大小 = $matches[4]标题 = $matches[5]}}}# 屏幕输出$结果 |格式表 -AutoSize# 输出到CSV文件$结果 |导出-Csv -Path 'D:\Test\patchresults.csv' -NoTypeInformation

使用示例文件输出

<预>服务器日期 X 状态 KB 大小 标题------ - - - ------ - ---- -----WSUSCL02-2012 10-8-2020 17:03:08 2 已接受 KB3172729 10 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL02-2012 10-8-2020 17:03:08 2 已接受 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL02-2012 10-8-2020 17:03:08 3 已下载 KB3172729 10 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL02-2012 10-8-2020 17:03:08 3 已下载 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL02-2012 10-8-2020 17:03:08 4 已安装 KB3172729 10 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL02-2012 10-8-2020 17:03:08 4 已安装 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL01-2012 10-8-2020 17:03:01 2 已接受 KB2962409 50 MB Windows Server 2012 R2 更新 (KB2962409)WSUSCL01-2012 10-8-2020 17:03:01 2 已接受 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL01-2012 10-8-2020 17:03:01 3 已下载 KB2962409 50 MB Windows Server 2012 R2 更新 (KB2962409)WSUSCL01-2012 10-8-2020 17:03:01 3 已下载 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...WSUSCL01-2012 10-8-2020 17:03:01 4 已安装 KB2962409 50 MB Windows Server 2012 R2 更新 (KB2962409)WSUSCL01-2012 10-8-2020 17:03:01 4 已安装 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...

P.S.我的系统是荷兰语,所以显示的默认日期格式是'dd-M-yyyy HH:mm:ss'

below data set is stored is text file and first is server name, second one is date and 3rd one is the patch histroy.

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...

Above is the data set stored in a text file and the requirement is to parse data and pick servername , date , patch and put that data in a custom power shell object with the name of sever name, date, patch details. please help me out to do this

解决方案

Using switch -Regex -File to loop over every line in the text file should do the trick.

Below code parses out all filds, but you can comment out any properties you donot wish in the result

$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

Output using your example file

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. my system is Dutch, so the default date format displayed is 'dd-M-yyyy HH:mm:ss'

这篇关于Powershell 脚本自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆