【问题标题】:Performance counters not being written by Powershell jobPowershell 作业未写入性能计数器
【发布时间】:2013-04-26 19:59:19
【问题描述】:

我正在设置一个可以从任何 Windows 服务器运行的脚本,以在滚动日志文件中收集一些后台 perfmon 脚本。我可以使用 PerfMon GUI,但我认为这会给我一个同时学习一些 Powershell 的好机会,并且我可以根据我的项目的具体需求将它们全部封装在我自己的简化 GUI 中。

基本上,该脚本会验证 ExecutionPolicy 是否设置为 remotesigned(因此它可以运行),然后定义几个变量以供在以下命令中使用,并概述要抓取的计数器。

之后,它将命令存储为变量,然后将其作为作业启动。问题是我从来没有以我要求的 .csv 文件的形式看到该工作的结果。代码如下:

Set-ExecutionPolicy remotesigned -Force
$Computer = $env:COMPUTERNAME
$1GBInBytes = 1GB
$p = "\\$Computer\Process(System)\% Processor Time",
     "\\$Computer\PhysicalDisk(_Total)\Current Disk Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\% Disk Time",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\% Disk Read Time",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Read Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\% Disk Write Time",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Write Queue Length",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Transfer",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Read",    
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk sec/Write",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Transfers/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Reads/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Writes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Bytes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Read Bytes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Disk Write Bytes/sec",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Transfer",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Read",
     "\\$Computer\PhysicalDisk(0 c:)\Avg. Disk Bytes/Write",
     "\\$Computer\PhysicalDisk(0 c:)\% Idle Time",
     "\\$Computer\PhysicalDisk(0 c:)\Split IO/Sec";

$counter = {get-counter -counter $p -Continuous | Export-Counter  C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes}
Start-job $counter

有什么想法吗?现在,我想将这项工作作为后台工作启动,我将通过单独的 powershell 命令停止它。不过,我只是希望它把所有这些都变成一个 .csv。

【问题讨论】:

    标签: powershell performancecounter powershell-3.0


    【解决方案1】:

    如果你使用的是psv3,修改如下:

    $counter = {get-counter -counter $using:p -Continuous | Export-Counter  C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $using:1GBInBytes}
    

    否则你无法访问 $p 和 $1GBInBytes

    如果您使用的是 v2:

    $counter = {param($p, $1GBInBytes ) get-counter -counter $p -Continuous | Export-Counter  C:\PerfLogs\Storage_BBCRM.csv -Force -FileFormat CSV -Circular -MaxSize $1GBInBytes}
    Start-job $counter -ArgumentList $p, $1GBInBytes
    

    【讨论】:

    • 我正在使用 3,我将测试该解决方案并阅读 $using 更多内容。这是一般规则吗,如果我想使用局部变量,我必须在变量之前指定 $using?
    • 是的,对于invoke-command和start-job等,如果要从该命令的scriptblock访问局部变量,则需要指定$using,否则需要使用argumentlist跨度>
    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多