Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

foreach loop issue to display data from multiple servers

$
0
0

 $servers = @("server1","server2","server3","server4","server5","server6","server7","server8")

    foreach($server in $servers)

    {    

        $tgtFolder = "\\$server\LogFiles\foo_pilot\"

        $tgtDate = (Get-Date).AddDays(-1) | Get-Date -Format yyyyMMdd

        Write-Verbose "Target date $tgtDate"

        Get-Content -Path (Join-Path -Path $tgtFolder -ChildPath "spmanager.log$tgtDate")|

        select-string 'Elapsed time for PICKLIST_SERVICEv10.getFilWorkLoad:' -SimpleMatch | 

        Out-File c:\scripts\test.txt

        $input_path = ‘c:\scripts\test.txt’

        $regex = ‘\b\d{1,2}\:\d{1,2}\.\d{1,2}\b’

        select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

        $data = Get-Content -Path c:\scripts\test.txt

        $pattern = "(?'min'\d{2}):(?'sec'\d{2})\.(?'hun'\d{2})"

        $max = $avg = $totalTime = 0.0

        $min = 100.0 # set this to something greater than the largest time

        $count = 0

        foreach ($item in $data)

        {

            if ($item -match $pattern)

            {

                $count++

                $time = $Matches['hun'] / 100 + [int]$Matches['min'] * 60 + $Matches['sec']

                $totalTime += $time

                $max = [Math]::Max($max,$time)

                $min = [Math]::Min($min,$time)

            }

        }

        $avg = $totalTime / $count

        "Max = {0:N2}" -f $max > C:\scripts\test.txt

        "Min = {0:N2}" -f $min >> C:\scripts\test.txt

        "Avg = {0:N2}" -f $avg >> C:\scripts\test.txt

        }

 

This is the script I am trying to run. The intention was to get max ,min, avg values for all the individual servers in one file 

I have the loop working fine and the script is compiling.  The output file is displaying only the details of the last server and is skipping the

first seven ones.  Any help on this. 


Viewing all articles
Browse latest Browse all 6937

Trending Articles