I have powershell code in my C# application that works great. It executes a script using the code snippet below.
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.AddScript(@"D:\C.ps1");
Collection<PSObject> output = powerShell.Invoke();
}
Like I said, it works. However, recently I wanted to add a Windows Form to the script. The very first thing I did was add the following line to the top of the script and nothing else. Without doing anything else, I just ran the C# app that executes the previously working script to make sure that loading of the assembly does what it's supposed too.
[void][System.Reflection.Assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
Immediately I get the strange following exception.
System.Exception {System.EntryPointNotFoundException}
Unable to find an entry point named 'AmsiNotifyOperation' in DLL 'amsi.dll'
I've search online for days and found nothing like this. I can't even get started adding my new script code because of this exception for the assembly load line.
Oh, very important, When I run that script with that new line from the Powershell ISE app, the command line, or several other powershell script tools I have. THE PROBLEM DOESN'T HAPPEN.
Anyone got any ideas?
Here is the nuget packages that the code is using, and the stack trace.
Nuget packages:
System.Management.Automation
Microsoft.Powershell.SDK
Stack: Source "System.Management.Automation" at System.Management.Automation.AmsiUtils.AmsiNativeMethods.AmsiNotifyOperation(IntPtr amsiContext, IntPtr buffer, UInt32 length, String contentName, AMSI_RESULT& result) at System.Management.Automation.AmsiUtils.WinReportContent(String name, String content)
TargetSite {Int32 AmsiNotifyOperation(IntPtr, IntPtr, UInt32, System.String, AMSI_RESULT ByRef)}