| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [CmdletBinding()] |
| param |
| ( |
| [ValidateNotNullOrEmpty()] |
| [string] $Path = $PSScriptRoot, |
|
|
| [switch] $Unregister |
| ) |
| Set-StrictMode -Version 3.0 |
| $ErrorActionPreference = 'Stop' |
|
|
| function Start-NativeExecution([scriptblock]$sb, [switch]$IgnoreExitcode) |
| { |
| $backupEAP = $script:ErrorActionPreference |
| $script:ErrorActionPreference = "Continue" |
| try |
| { |
| & $sb |
| |
| |
| if ($LASTEXITCODE -ne 0 -and -not $IgnoreExitcode) |
| { |
| throw "Execution of {$sb} failed with exit code $LASTEXITCODE" |
| } |
| } |
| finally |
| { |
| $script:ErrorActionPreference = $backupEAP |
| } |
| } |
|
|
| function Test-Elevated |
| { |
| [CmdletBinding()] |
| [OutputType([bool])] |
| Param() |
|
|
| |
| |
| |
| return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544") |
| } |
| $IsWindowsOs = $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase) -or $IsWindows |
|
|
| if (-not $IsWindowsOs) |
| { |
| throw 'This script must be run on Windows.' |
| } |
|
|
| if (-not (Test-Elevated)) |
| { |
| throw 'This script must be run from an elevated process.' |
| } |
|
|
| $manifest = Get-Item -Path (Join-Path -Path $Path -ChildPath 'PowerShell.Core.Instrumentation.man') |
| $binary = Get-Item -Path (Join-Path -Path $Path -ChildPath 'PowerShell.Core.Instrumentation.dll') |
|
|
| $files = @($manifest, $binary) |
| foreach ($file in $files) |
| { |
| if (-not (Test-Path -Path $file)) |
| { |
| throw "Could not find $($file.Name) at $Path" |
| } |
| } |
|
|
| [string] $command = 'wevtutil um "{0}"' -f $manifest.FullName |
|
|
| |
| |
| Write-Verbose "unregister the manifest, if present: $command" |
| Start-NativeExecution {Invoke-Expression $command} $true |
|
|
| if (-not $Unregister) |
| { |
| $command = 'wevtutil.exe im "{0}" /rf:"{1}" /mf:"{1}"' -f $manifest.FullName, $binary.FullName |
| Write-Verbose -Message "Register the manifest: $command" |
| Start-NativeExecution { Invoke-Expression $command } |
| } |
|
|