0

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

prabhat
  • 1
  • 3
  • 1
    Can you post a bit more information - what have you tried so far? What didn't work about it? – mclayton Aug 12 '20 at 13:53
  • Watching this video may help you: [Sophisitcated Techniques of Plain Text Parsing](https://youtu.be/Hkzd8spCfCU) – Olaf Aug 12 '20 at 13:58
  • $data=Get-Content C:\data1.log $alldata=$data | Foreach { if ($_ -ilike "*") {Write-Output $_} $server=$alldata | foreach { if ($_ -ilike "*WSUS*") {Write-Output $_} } $Patch = $alldata | foreach { if ($_ -inotlike "*WSUS*") {Write-Output $_} } } $serverdetail = $server | foreach { [PSCustomObject]@{ SERVER = $_ } } through that way only i got server detail and able to put in custom object – prabhat Aug 12 '20 at 14:07
  • 2
    Please do not add additional information as comment. Instead edit your question and add it there. Thanks in advance. – Olaf Aug 12 '20 at 14:13

1 Answers1

2

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'

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Can we have only a single entry o f server name instead of iteration? – prabhat Aug 12 '20 at 15:08
  • AUTOM01-AYEHU1:No Updates Available AUTOM01-AYEHU2:No Updates Available AUTOM01-AYEHU3:No Updates Available AUTOM01-AYEHU4:No Updates Available AUTOM01-AYEHU5:No Updates Available AUTOM01-AYEHU6:No Updates Available @Theo that on this data set i have simmilar requirement but not able to get the result like need 2 custom object with the name (SERVERNAME, STATUS) and put data into it – prabhat Aug 13 '20 at 09:34
  • @prabhat [1] For your first comment: yes, you can group the `$result` array on the server like `$servers = $result | Group-Object Server`. [2] As for your second comment: this is barely readable, but if I understand correctly, on that data you can do: `$result = $data -replace ':', '=' | ConvertFrom-StringData` – Theo Aug 13 '20 at 10:27
  • for the second comments how can i use this existing code to make 2 objects by using some regex '(\D*)\s' '[^-]*' – prabhat Aug 13 '20 at 11:00