Skip to content

Is there an alternative to PowerShell.AddScript() that preserves file position information? #7530

@alx9r

Description

@alx9r

The parameter to .AddScript() is a string, not a scriptblock, so any file position information that might have been present in the ScriptBlock object never makes it into that method. Is there some other way to get a PowerShell object to invoke a ScriptBlock without losing file position information?

Steps to reproduce

try {
    [powershell]::Create().AddScript({
        throw 'message'
    }).Invoke()
}
catch {
    $_.Exception.InnerException.ErrorRecord.ScriptStackTrace
}

Expected behavior

at <ScriptBlock>, <C:\Users\UserName\Desktop\test.ps1>: line 3

Actual behavior

at <ScriptBlock>, <No file>: line 2

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.4
PSEdition                      Core
GitCommitId                    6.1.0-preview.4
OS                             Microsoft Windows 6.3.9600 
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions