gpt4 book ai didi

logging - 在Powershell脚本中使用滚动日志文件收集性能计数器

转载 作者:行者123 更新时间:2023-12-03 01:20:13 29 4
gpt4 key购买 nike

好吧,所以我有一个不错的.ps1脚本,该脚本获取一组计数器并将其写入最大大小为1GB的循环日志文件中。它运作良好,并使每个人都开心。

当前,它的触发方式是通过一个.bat文件运行,该文件在脚本中运行,然后在隐藏的窗口中打开Powershell.exe的实例。这使它运行起来毫不干扰。

但是,我真的不喜欢“循环”日志文件的想法。我正在获取大量信息,在其中一些服务器上,这意味着即使1GB的限制也会相当频繁地出现。我正在运行测试以查看实际花费了多长时间,但是可以预见的是,滚动日志文件可能会更好地工作。

我发现有一个函数需要检查特定文件的大小,并在必要时创建一个新文件,但是我不太确定如何定期(例如每天一次或每小时一次)调用该函数。一个Powershell脚本,基本上是生硬的忘了。

更复杂的是,在这种情况下,我对开始工作和停止工作的实验似乎效果不佳。它根本不会创建日志文件。

码:

# This script tracks performance counters useful for tracking performance on a SQL server in a rolling .csv file 
located at a directory of your choosing. It is written for Powershell v.2

$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 GO HERE;

# 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
get-counter -counter $p -SampleInterval 60 -Continuous | Export-Counter $Folder\SQL_log.csv -Force -FileFormat CSV
-Circular -MaxSize $1GBInBytes
}
Else
{
get-counter -counter $p -SampleInterval 60 -Continuous | Export-Counter $Folder\SQL_log.csv -Force -FileFormat CSV
-Circular -MaxSize $1GBInBytes
}

敏锐的眼睛会注意到缺少滚动功能。这是因为上面的脚本是我的稳定版本,我正在使用的功能在这里:

http://sysbrief.blogspot.com/2011/05/powershell-log-rotation-function.html

有任何想法吗?我不想不必运行单独的Powershell实例来充当侦听器,也不必让用户定期自己运行该函数,但是我几乎对任何想法都持开放态度。

设置Windows作业以定期启动Powershell中的功能并滚动日志文件会更好吗?

最佳答案

您应该能够处理此内联,例如:

$num  = 0
$file = "$Folder\SQL_log_${num}.csv"
Get-Counter -counter $p -SampleInterval 60 -Continuous |
Foreach {
if ((Get-Item $file).Length -gt 900MB) {
$num +=1;$file = "$Folder\SQL_log_${num}.csv"
}
$_
} |
Export-Counter $file -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes

这将连续测试文件大小,当达到限制时,它将更改日志文件的文件名。重要的是,该测试/重命名脚本都不输出任何内容。唯一应沿管道输出的是 $_表示的性能计数器数据。

关于logging - 在Powershell脚本中使用滚动日志文件收集性能计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16795463/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com