次のコードを使用しています。
$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)
isTRUE
の場合でも、ファイルをインクリメントしません。私の考えではForeach
、サンプルが取得されるたびにループが呼び出されるわけではありません。これは、Get-Counter が 1 回だけ呼び出されている (そして進行中) ためです。そのループを通過していることを確認するためにどの構造を使用する必要があるかわかりません。Foreach
実行中に呼び出されることに依存するのではなく、その特定のステートメントを別のセクションに分離する必要がありget-counter
ますか? この Powershell スクリプトはバッチ ファイルによって呼び出され、そのget-counter
部分がバックグラウンドで実行され、情報が収集されます。