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

4
  • 1
    Can you post a bit more information - what have you tried so far? What didn't work about it? Commented Aug 12, 2020 at 13:53
  • Watching this video may help you: Sophisitcated Techniques of Plain Text Parsing Commented Aug 12, 2020 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 Commented Aug 12, 2020 at 14:07
  • 2
    Please do not add additional information as comment. Instead edit your question and add it there. Thanks in advance. Commented Aug 12, 2020 at 14:13

1 Answer 1

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'

Sign up to request clarification or add additional context in comments.

4 Comments

Can we have only a single entry o f server name instead of iteration?
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 [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
for the second comments how can i use this existing code to make 2 objects by using some regex '(\D*)\s' '[^-]*'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.