Skip to content

Capture ScriptBlock source code from the topmost frame #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

willhcr
Copy link
Contributor

@willhcr willhcr commented Oct 16, 2024

As a fallback for environments where PowerShell is executed directly from .NET as a ScriptBlock and there is no .ps1 file available, such as in Azure Automation workers.

Ensure StackTraceString matches InvocationInfo when ScriptName is blank.

Changes licensed under MIT terms

…s a fallback

Ensure StackTraceString matches InvocationInfo when ScriptName is blank
@bruno-garcia bruno-garcia requested a review from vaind October 20, 2024 05:49
@vaind
Copy link
Collaborator

vaind commented Oct 21, 2024

Thanks for the PR. Is there a way we can reproduce this locally? I'd like to see how it behaves in Sentry & also because we should have a test case to avoid future regressions.

@willhcr
Copy link
Contributor Author

willhcr commented Oct 21, 2024

Can call a script hosted from c# with this snippet. But this doesn't appear to work the same as Azure Automation's Hybrid Worker (which I cannot find the source for on github). This requires the Microsoft.PowerShell.5.1.ReferenceAssemblies NuGet package as well. But at least, this results in the same issue I observed with Azure Automation, it runs but no event arrives in Sentry.

using System.IO;
using System.Management.Automation;

namespace HostedPowershell
{
  internal class Program
  {
    public static void Main(string[] args)
    {
      var ps = PowerShell.Create();
      var script = File.ReadAllText(@"Basic-Test.ps1");
      ps.AddScript(script);
      var result = ps.Invoke();
      
      foreach (var item in result)
      {
        System.Console.WriteLine(item);
      }
      
      foreach (var error in ps.Streams.Error)
      {
        System.Console.WriteLine(error);
      }
      
      foreach (var debug in ps.Streams.Debug)
      {
        System.Console.WriteLine(debug);
      }
    }
  }
}

@vaind
Copy link
Collaborator

vaind commented Oct 23, 2024

Getting this working for various ways the command is executed was a bit more work than originally anticipated but the improvements were done in #65 and should be part of v0.2.0 release. Please give it a try and feel free to reopen if the new version doesn't solve your issue.

@vaind vaind closed this Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants