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.