forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-PerfviewPS.ps1
More file actions
55 lines (44 loc) · 1.47 KB
/
Invoke-PerfviewPS.ps1
File metadata and controls
55 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#requires -RunAsAdministrator
param(
$ETLFileName = '.\PerfViewData.etl',
[Parameter(Mandatory)]
[scriptblock]
$ScriptBlock,
$LogFileName = '.\perfview.log',
$PowerShellPath = $(Get-Command pwsh.exe).Source)
$EncodedScriptBlock = [System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($ScriptBlock.ToString()))
$perfViewArgs = @(
'/AcceptEula'
'/ThreadTime'
"/LogFile=$LogFileName"
"/DataFile:$ETLFileName"
'/noRundown'
'/Merge'
'/Zip:False'
# GCSampledObjectAllocationHigh is sometimes useful, but quite expensive so not included by default
# '/ClrEvents=default+GCSampledObjectAllocationHigh'
'/Providers:*Microsoft-PowerShell-Runspaces,*Microsoft-PowerShell-CommandDiscovery,*Microsoft-PowerShell-Parser,*Microsoft.Windows.PowerShell'
'run'
"""$PowerShellPath"""
'-NoProfile'
'-EncodedCommand'
$EncodedScriptBlock
)
$process = Start-Process -FilePath (Get-Command PerfView.exe).Source -ArgumentList $perfViewArgs -PassThru
$rs = [runspacefactory]::CreateRunspace($host)
$rs.Open()
$ps = [powershell]::Create()
$ps.Runspace = $rs
$null = $ps.AddCommand("Get-Content").
AddArgument($LogFileName).
AddParameter("Wait").
AddParameter("Tail", 0)
$null = $ps.AddCommand("Out-Host")
# If log file doesn't exist yet, wait a little bit so Get-Content doesn't fail
while (!(Test-Path $LogFileName))
{
Start-Sleep -Seconds 1
}
$null = $ps.BeginInvoke()
$process.WaitForExit()
$ps.Stop()