使用Powershell收集Perfmon计数器,是否可以插入日期时间而不仅仅是时间?

时间:2013-06-18 17:37:09

标签: datetime powershell

我有一个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

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

我确定在创建.csv后运行Powershell脚本而没有任何干扰导致我想要的datetime格式,所以问题在于我在将它加载到SQL之前清理.csv的方式。