【问题标题】:Powershell scripting custom objectPowershell 脚本自定义对象
【发布时间】:2020-08-12 13:09:23
【问题描述】:

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

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 对象中,名称为服务器名称、日期、补丁详细信息。请帮我做这个

【问题讨论】:

  • 您能否发布更多信息 - 到目前为止您尝试了什么?有什么不好的地方?
  • 观看此视频可能会对您有所帮助:Sophisitcated Techniques of Plain Text Parsing
  • $data=获取内容 C:\data1.log $alldata=$data | Foreach { if ($_ -ilike "") {写入输出 $_} $server=$alldata | foreach { if ($_ -ilike "*WSUS") {写入输出 $_} } $Patch = $alldata | foreach { if ($_ -inotlike "WSUS") {写入输出 $_} } } $serverdetail = $server | foreach { [PSCustomObject]@{ SERVER = $_ } } 通过这种方式只有我得到了服务器详细信息并能够放入自定义对象
  • 请不要添加其他信息作为评论。而是编辑您的问题并将其添加到那里。提前致谢。

标签: powershell scripting pscustomobject


【解决方案1】:

使用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

使用您的示例文件输出

服务器日期 X 状态 KB 大小 标题 ------ ---- - ------ -- ---- ----- WSUSCL02-2012 10-8-2020 17:03:08 2 已接受 KB3172729 适用于 Windows Server 2012 R2 的 10 MB 安全更新 (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 适用于 Windows Server 2012 R2 的 50 MB 更新 (KB2962409) WSUSCL01-2012 10-8-2020 17:03:01 2 已接受 KB3175024 适用于 Windows Server 2012 R2 的 12 MB 安全更新 (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 适用于 Windows Server 2012 R2 的 50 MB 更新 (KB2962409) WSUSCL01-2012 10-8-2020 17:03:01 4 已安装 KB3175024 12 MB Windows Server 2012 R2 安全更新 (KB...

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

【讨论】:

  • 我们可以只有一个服务器名称条目而不是迭代吗?
  • AUTOM01-AYEHU1:无可用更新 AUTOM01-AYEHU2:无可用更新 AUTOM01-AYEHU3:无可用更新 AUTOM01-AYEHU4:无可用更新 AUTOM01-AYEHU5:无可用更新 AUTOM01-AYEHU6:无可用更新@Theo,在这个数据集上我有类似的要求,但无法获得需要 2 个名为(SERVERNAME,STATUS)的自定义对象并将数据放入其中的结果
  • @prabhat [1] 对于您的第一条评论:是的,您可以像 $servers = $result | Group-Object Server 一样将服务器上的 $result 数组分组。 [2] 至于您的第二条评论:这几乎不可读,但如果我理解正确,您可以根据该数据执行以下操作:$result = $data -replace ':', '=' | ConvertFrom-StringData
  • 对于第二个 cmets,我如何使用这个现有代码通过使用一些正则表达式来制作 2 个对象 '(\D*)\s' '[^-]*'
猜你喜欢
  • 2016-10-08
  • 2022-11-11
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多