locked
POWERSHELL SCRIPT TO MODIFY log OUTPUT RRS feed

  • Question

  • I have a text file which is generated every 24 hours in the following format 

    ==================================================
    Host Name       : 
    IP Address       : 
    Reply IP Address : 
    Succeed Count   : 2
    Failed Count     : 0
    Consecutive Failed Count: 
    Max Consecutive Failed Count: 
    Max Consecutive Failed Time: 
    % Failed         : 0%
    Last Ping Status : Succeeded
    Last Ping Time   : 6
    Last Ping TTL   : 60
    Average Ping Time : 25
    Description     : Channels:ADR1-Location1-SB
    Last Succeed On : 12/11/2018 9:52:47 AM
    Last Failed On   : 
    Minimum Ping Time : 6
    Maximum Ping Time : 41
    Order           : 1
    ==================================================

    ==================================================
    Host Name       : 
    IP Address       : 
    Reply IP Address : 
    Succeed Count   : 1
    Failed Count     : 1
    Consecutive Failed Count: 1
    Max Consecutive Failed Count: 1
    Max Consecutive Failed Time: 12/11/2018 9:52:17 AM
    % Failed         : 50.00%
    Last Ping Status : Succeeded
    Last Ping Time   : 5
    Last Ping TTL   : 60
    Average Ping Time : 5
    Description     : Channels:ADR2-Location1-SB
    Last Succeed On : 12/11/2018 9:52:47 AM
    Last Failed On   : 12/11/2018 9:52:17 AM
    Minimum Ping Time : 5
    Maximum Ping Time : 5
    Order           : 2
    ==================================================
    Similarly 29 other logs are created for the 29 other IP's. However, when trying to format in the form of an csv file with the left hand column being the header for the csv, I am getting the following errors:

    a) Cannot perform operation because the wildcard path C:\Users\PLahkar\Desktop\Ping_Success\*.csv did not resolve to a file.

    This is the following code :

    $Header = "Host Name,IP Address,Reply IP Address,Succeed Count,Failed Count,Consecutive Failed Count,Max Consecutive Failed Count,Max Consecutive Failed Time,% Failed,Last Ping Status,Last Ping Time,Last Ping TTL,Average Ping Time,Description,Last Succeed On,Last Failed On,Minimum Ping Time,Maximum Ping Time,Order"
    $Header | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_*.csv
    $Files = Get-ChildItem C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_*.txt
    ForEach ($File in $Files) {
        $Text = Get-Content $File.FullName
        $Text = $Text -ireplace '^[^:]*:\s*','' -ireplace '\s*$',',' -ireplace '=',''
        $Text = ($Text | Out-String) -ireplace '\r\n','' | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_*.csv -Append
    }

    The file logs names would be in a numerical order.  

    I would want the logs to be in the following format in csv

    Host Name,IP Address,Reply IP Address,Succeed Count,Failed Count,Consecutive Failed Count,Max Consecutive Failed Count,Max Consecutive Failed Time,% Failed,Last Ping Status,Last Ping Time,Last Ping TTL,Average Ping Time,Description,Last Succeed On,Last Failed On,Minimum Ping Time,Maximum Ping Time,Order

    xxx xxxx xxxx 1 0 0% Succeeded 42 60 42 Channels:ADR1-Location1-SB 12/11/2018 2:09:19 PM 42 42 1

    xxxx xxx xxx 0 1 1 1 12/11/2018 2:09:19 PM 100.00% Request Timeout 0 Channels:ADR2-Location1-SB 12/11/2018 2:09:19 PM 0 0 2

    Tuesday, December 11, 2018 7:10 PM

All replies

  • Replace this line:

      $Header | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_*.csv

    With this one:

      $Header | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_$($File.basename).csv

    Replace this line:

      $Text = ($Text | Out-String) -ireplace '\r\n','' | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_*.csv -Append

    With this one:

      $Text = ($Text | Out-String) -ireplace '\r\n','' | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_$($File.basename).csv -Append


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Tuesday, December 11, 2018 7:22 PM
  • Thanks. This works. However, the header and the output are not appended. Instead, it spits out two diff CSV files(One for the header and one for the IP pings<g class="gr_ gr_293 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="293" id="293">) .</g>

    Also, is there a way to sort the files according to one row for each IP address

    Tuesday, December 11, 2018 7:31 PM
  • Sorry. Your 1st Out-File happens before you know the name of the file you're going to process.

    Move the line:

    $Header | Out-File C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_$($File.basename).csv

    Beneath the line:

    ForEach ($File in $Files) {

    Adding the line "Set-PSDebug -strict" at the top of your script would have, I believe, pointed out the use of the uninitialized variable "$File".


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Tuesday, December 11, 2018 8:14 PM
  • Thanks, Rich, this was perfect. However, the IP's are still not generated in a sequential row along with their stats which would make it easier to go through them. Please, can you help with this? 
    Tuesday, December 11, 2018 8:55 PM
  • You don't produce any sortable output, other than a CSV file, because you're dealing only with simple strings.

    Try this and see if it suits your purpose:

    Get-ChildItem C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_*.txt |ForEach {
        $filebasename = $_.basename
        $write = $false
        $InData = $false
            
        Foreach ($line in (Get-Content $_)) {
            if ($line.Trim().Length -gt 1)     # skip empty lines
            {
                if ($line -match "========*")
                {
                    if ($InData -eq $false)
                    {
                        $InData = $true
                        $p = [ordered]@{}
                    }
                    else
                    {
                        $InData = $false
                        $write = $true
                    }
                }
                else
                {
                    $t=$line -Split ':',2,'SimpleMatch'
                    $p.Add( $t[0].Trim(), $t[1].Trim() )
                }
                if ($write)
                {
                    $write = $false
                    New-Object PSObject -Property $p | Write-Output
                }
            }
        } | sort "IP Address" | Export-Csv C:\Users\PLahkar\Desktop\Ping_Success\Pingsuccess_$($fFilebasename).csv -NoTypeInformation
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, December 12, 2018 2:49 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 24, 2018 3:19 AM