File size: 1,138 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#requires -RunAsAdministrator

param(
    $ETLFileName = '.\PerfViewData.etl',

    [Parameter(Mandatory)]
    [scriptblock]
    $ScriptBlock,

    $LogFileName = '.\perfview.log',
    $PowerShellPath = $(Get-Command -Name pwsh.exe).Source,
    $PerfViewPath = $(Get-Command -Name PerfView.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 $PerfViewPath -ArgumentList $perfViewArgs -PassThru
$process.WaitForExit()

Get-Content $LogFileName | Out-Host