1

我有一个 Powershell 脚本,它收集一系列性能计数器并将它们存储在 SQL 服务器的日志文件中。

目前该日志的输出包括以下列:PDHCSV40EasternDaylightTime240

这会将 MM:SS.ms 推送到该列中,但没有给出小时或日期,这样一小时后收集的任何计数器都变得毫无价值。

有什么方法可以更改 Powershell 脚本中该列的格式?最终,日志存储在 .csv 中,然后我使用调用 LogParser 2.2 的 .bat 文件对其进行清理并输入 SQL,因此我可以使用 SQL 脚本来创建基于 RowNumber 的日期时间。

以下是实际创建日志的代码:

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

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

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

$date = Get-Date -Format 'MM_dd_yyyy HH_mm_ss'
$file = "$Folder\SQL_log_$date.csv"

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

Get-Counter -counter $p -SampleInterval 60 -Continuous | export-counter -Path $File -FileFormat CSV
4

2 回答 2

1

我运行脚本的最后一行,得到日期/月/年等。我错过了什么吗?

PS C:\Documents\ManualScripts> get-counter -counter "\processor(_total)\% processor time" -SampleInterval 5 -Continuous
| Export-Counter -path .\Test-files\counter -FileFormat csv

导出文件的内容:

PS C:\Documents\ManualScripts> get-content .\Test-files\counter
"(PDH-CSV 4.0) (Eastern Daylight Time)(240)","\\win2012-2\processor(_total)\% processor time"
"06/18/2013 14:48:00.742"," "
"06/18/2013 14:48:05.843","9.1628941466886715"
"06/18/2013 14:48:10.910","9.4105840748880283"
"06/18/2013 14:48:15.953","14.770849818884757"
"06/18/2013 14:48:20.993","12.40314193479044"
"06/18/2013 14:48:26.043","22.617568840081649"
"06/18/2013 14:48:31.086","21.901970809646333"
"06/18/2013 14:48:36.203","10.671110542619488"
"06/18/2013 14:48:41.243","17.654344180766689"
"06/18/2013 14:48:46.277","16.714921378494417"
"06/18/2013 14:48:51.318","10.501397411247293"
"06/18/2013 14:48:56.359","10.875155019759863"
于 2013-06-18T18:59:24.453 回答
0

我确定在创建 .csv 之后运行 Powershell 脚本而不受我的任何干扰会产生我想要的日期时间格式,所以问题在于我如何在将 .csv 加载到 SQL 之前对其进行清理。

于 2013-06-18T18:46:51.743 回答