Powershell背景作业 - 如何使用'ComputerName'参数从Export-Counter / Get-Counter获取输出?

时间:2017-10-26 16:16:39

标签: powershell performance-testing background-process

我正在尝试从PowerShell后台作业收集性能数据,同时将'load'应用于前台系统。 如果我在没有-ComputerName参数的情况下运行我的Get-Counter / Export-Counter脚本作为后台作业,它会按照预期从本地计算机创建一个包含性能数据的输出文件。

# Background job, No ComputerName

$scriptBlockStr = "Get-Counter -Counter ""\Memory\Available MBytes""     -SampleInterval 2 -MaxSamples 3 | Export-Counter -Force -FileFormat CSV -Path $PSScriptRoot\MinPerfTest.csv"
$sb = [scriptblock]::Create($ScriptBlockStr)

$j = Start-Job -Name "PerfMon01" -ScriptBlock $sb
Start-Sleep -Seconds 10
Stop-Job $j.Id

Write-Host "See  $PSScriptRoot\MinPerfTest.csv."

如果我包含-ComputerName参数,并在前台运行脚本块,它会使用指定计算机的性能数据创建输出文件。

# Foreground job, With ComputerName

$scriptBlockStr = "Get-Counter -Counter ""\Memory\Available MBytes"" -ComputerName ""\\CPQDEV.fpx.com"" -SampleInterval 2 -MaxSamples 3 | Export-Counter -Force -FileFormat CSV -Path $PSScriptRoot\MinPerfTest.csv"
$sb = [scriptblock]::Create($ScriptBlockStr)

& $sb

Write-Host "See  $PSScriptRoot\MinPerfTest.csv. (Wait! It can take a while.)"

但是如果我使用-Co​​mputerName参数运行脚本,作为后台作业,Export-Counter cmdlet永远不会产生任何输出。

# Background job, With ComputerName

$scriptBlockStr = "Get-Counter -Counter ""\Memory\Available MBytes"" -ComputerName ""\\CPQDEV.fpx.com"" -SampleInterval 2 -MaxSamples 3 | Export-Counter -Force -FileFormat CSV -Path $PSScriptRoot\MinPerfTest.csv"
$sb = [scriptblock]::Create($ScriptBlockStr)

$j = Start-Job -Name "PerfMon01" -ScriptBlock $sb
Start-Sleep -Seconds 10
Stop-Job $j.Id

Write-Host "See  $PSScriptRoot\MinPerfTest.csv. (Wait! It could take a while, if it works at all.)"

您能告诉我从命名计算机获取性能数据需要做些什么吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

Reference

您的问题在于远程作业。这是一个更容易的建议(来自msdn):

$SB = [ScriptBlock]::Create('Get-Counter -Counter "Memory\Available MBytes" -SampleInterval 2 -MaxSamples 3')
Invoke-Command -ComputerName \\CPQDEV.fpx.com -ScriptBlock $SB -AsJob |
  Receive-Job -Wait |
  Export-Counter -Force -FileFormat CSV -Path "$PSScriptRoot\MinPerfTest.csv"

Write-Host "See  $PSScriptRoot\MinPerfTest.csv. (may not work)"