我正在使用以下代码:
$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
}
但是,即使((Get-Item $file).Length -gt 1MB)
is TRUE
,它也不会增加文件。我的想法是,Foreach
在每次采样时都不会调用循环,因为 Get-Counter 只是被调用一次(然后是持续的)。我不确定我应该使用什么构造来确保它通过那个循环。我是否应该将该特定Foreach
语句隔离到另一个部分,而不是依赖它在get-counter
? 此 Powershell 脚本由批处理文件调用,然后该get-counter
部分在后台运行,收集信息。