2017-10-26 5 views
1

フォアグラウンドのシステムに「負荷」を適用しながら、PowerShellバックグラウンドジョブからパフォーマンスデータを収集しようとしています。 Get-Counter/Export-Counterスクリプトを-ComputerNameパラメータなしでバックグラウンドジョブとして実行すると、ローカルコンピュータからのパフォーマンスデータを含む出力ファイルが期待通りに作成されます。Powershellのバックグラウンドジョブ - 'ComputerName'パラメータを使用してExport-Counter/Get-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.)" 

しかし、私は-ComputerNameパラメーターを指定してスクリプトを実行すると、バックグラウンドジョブとして、輸出-Counterコマンドレットは、任意の出力を生成することはありません。

# 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.)" 

パフォーマンスデータを名前付きコンピュータから取得するために必要なことを教えてください。 ありがとうございました!

+0

対象のコンピュータにディレクトリ構造が存在しますか?おそらくエラーを投げているでしょう。ジョブからジョブの出力を取得してみてください。 – TheIncorrigible1

+0

'Stop-Job'は使用しないでください。 [Receive-Job -Waitを使用](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/receive-job?view=powershell-5.1) –

+0

展開するあなたの提案@BaconBits: '$ j =スタート - ジョブ - 名前" PerfMon01 "-ScriptBlock $ sb | Receive-Job -Wait' – TheIncorrigible1

答えて

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)" 
関連する問題