0

I have the following code in use:

$Folder="C:\Perflogs\BBCRMLogs" # Change the bit in the quotation marks to whatever directory you want the log file stored in

$Computer = $env:COMPUTERNAME
$1GBInBytes = 1GB
$p = "LOTS OF COUNTERS";

# If you want to change the performance counters, change the above list. However, these are the recommended counters for a client machine. 

$dir = test-path $Folder 

IF($dir -eq $False) 
{
New-Item $Folder -type directory
$num  = 0
$file = "$Folder\SQL_log_${num}.csv"
Get-Counter -counter $p -SampleInterval 2 -Continuous | 
    Foreach {
        if ((Get-Item $file).Length -gt 1MB) {
            $num +=1;$file = "$Folder\SQL_log_${num}.csv"
        }
        $_
    } | 
    Export-Counter $file -Force -FileFormat CSV
}
Else
{
$num  = 0
$file = "$Folder\SQL_log_${num}.csv"
Get-Counter -counter $p -SampleInterval 2 -Continuous | 
    Foreach {
        if ((Get-Item $file).Length -gt 1MB) {
            $num +=1;$file = "$Folder\SQL_log_${num}.csv"
        }
        $_
    } | 
    Export-Counter $file -Force -FileFormat CSV
}

However, even when ((Get-Item $file).Length -gt 1MB) is TRUE, it doesn't increment the file up. My thought is that the Foreach loop isn't being called during each time the sample is taken, since Get-Counter is just being called once (and then is ongoing). I'm not sure what construct I should be using to make sure that it is passing through that loop. Should I isolate that particular Foreach statement out into another section, rather than relying on it being called during the get-counter? This Powershell script is being called by a Batch file and then the get-counter part runs in the background, collecting information.

Sean Long
  • 2,163
  • 9
  • 30
  • 49

1 Answers1

1

The problem is that the $file variable on Export-Counter is only evaluated once when Export-Counter is executed. Pipe the results of Get-Counter to Foreach-Object and export inside of it (forcing $file to re-evaluate) but that will overwrite the output file in each iteration and unfortunately Export-Counter doesn't have an Append switch.

Off the top of my head you could export to csv using Export-Csv, in v3 it supports appending to the file. That said, you won't get you the same csv structure.

Two more things. In the first execution of the script, the first file was not created yet and then you check for it's length. That gives file not found an error, use the ErrorAction parameter to suppress errors.

You don't need to repeat the code twice. Check if the output directory exists and create it if it doesn't exist, then continue once with the rest of the script.

$Folder = 'D:\temp'
$num  = 0
$file = "$Folder\SQL_log_${num}.csv"

if( !(test-path $folder)) {New-Item $Folder -type directory}

Get-Counter -counter $p -SampleInterval 2 -Continuous | Foreach {

    if ((Get-Item $file -ErrorAction SilentlyContinue ).Length -gt 1mb) 
    {
        $num +=1
        $file = "$Folder\SQL_log_${num}.csv"
    }

    $_

} | Foreach-Object { $_ | Export-Csv $file -Append} 
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Hey Shay, so I tested your code and once 1MB is reached, it does indeed roll over the log file into a new one... however it looks like it will create a new 1MB file each time the counters fire after that. So after a couple days I had 200+ log files. Any ideas for that? I think it might have to do with the -Append switch. After looking at it further, it's probably because it wants to export the entirety of the CSV. Also it looks like it will be storing more and more in memory (powershell ended with 65MB of RAM). – Sean Long Jun 04 '13 at 18:05